[PYTHON] Generate image text together

Recently, CSS browser support has improved, and even plain text has sufficient expressive power, so I think it is better not to image text in Web content.

However, depending on the project, it may be unavoidable that you want to make all the text information managed in the database into images.

You can handle it one by one with Adobe products, but I made a command line tool because it would quickly overwhelm me and I would be faint if the color or size change occurred later.

https://github.com/koji0330/imageTextGenerator

I just call the ImageMagick command by specifying the font size and font in the JSON file, but ImageMagick is very convenient. I use python to read the JSON file and execute the command, but since it is a simple process, I think that any language can be used.

{
    "sets": [
        {
            "font": "/Library/Fonts/Hiragino Mincho Pro W6.otf",
            "size": "23",
            "color": "#000",
            "convert": [
                {
                    "label": "Title image 1",
                    "imgpath": "img/txt_title1.png "
                }, {
                    "label": "Title image 2",
                    "imgpath": "img/txt_title2.png "
                }
            ]
        }, {
            "size": "29",
            "font": "/Library/Fonts/Hiragino Kakugo Pro W6.otf",
            "color": "#EA5304",
            "convert": [
                {
                    "label": "0123456789",
                    "imgpath": "img/num_0123456789.png "
                }, {
                    "label": "9876543210",
                    "imgpath": "img/num_9876543210.png ",
                    "color": "#f33"
                }
            ]
        }
    ]
}
#!/usr/bin/python
# -*- encoding: utf-8 -*-

import json, argparse, commands

parser = argparse.ArgumentParser(
	description='Generate image text from json file.'
)
parser.add_argument(
	'conf_file',
	nargs='?',
	default='config.json',
	help='config file'
)

args = parser.parse_args()

# Load config file
f = open(args.conf_file, "rU")
loaded = json.load(f)
f.close()

for set in loaded['sets']:
	font = set.get('font', '');
	size = set.get('size', '30');
	label = set.get('label', 'undefined');
	background = set.get('background', 'None');
	color = set.get('color', '#000');
	imgpath = set.get('imgpath', 'undefined.png');
	for convert in set['convert']:
		font = convert.get('font', font);
		size = convert.get('size', size);
		label = convert.get('label', label);
		background = convert.get('background', background);
		color = convert.get('color', color);
		imgpath = convert.get('imgpath', imgpath);
		conv_cmd = u'convert -background %s -fill "%s" -font "%s" -pointsize %s label:"%s" %s' % (background, color, font, size, label, imgpath)
		print conv_cmd
		out = commands.getoutput(conv_cmd.encode('utf_8'))
		print out
print

If there are too many requirements for imaging, it may be possible to build an imaging API on the same server.

Recommended Posts

Generate image text together
Generate a vertical image of a novel from text data
Generate a Docker image using Fabric
Automatically generate collage from image list
Add lines and text on the image
Try to generate an image with aliasing