My requirements were to create many different images containing different text and of different sizes for each.

The most recommended way to auto-generate new images seemed to be using ImageMagick. And, I chose Python to practice it and send ImageMagick the commands for creating the different images.

Prerequisites:

  • Python 2.7+ is installed and in PATH. (Python 3+ might also work with no changes)

In the following steps, I’m using Windows, but other OSes should be very similar.

1. Download ImageMagick

  1. Go to ImageMagick home page, then the Download section.
  2. Find an appropriate version for your OS, download, install.

Sidenote: For Windows 64-bit, I chose “ImageMagick-6.9.1-10-portable-Q16-x64.zip”. Note: It is the “portable” version so that there is no installer required. Many people on Windows have trouble running ImageMagick because it’s main command is convert and that contradicts with a command that Windows already has. So, for simplicity purposes for this quick requirement, I just put the portable version on my Desktop and used a hardcoded path, which you will see in the next section.

2. Create scripts

Here’s a quick ImageMagick script that can be run on the command line to generate a simple image with black background and yellow text.

// In the ImageMagick directory that was downloaded.
// xc is setting background color
./convert.exe -size 1000x250 xc:black -fill yellow -pointsize 36 -gravity center -draw "text 0,0 'my text'" myoutput.png

Here’s full sample Python code that I used to create my custom simple images. Though ImageMagick is definitely a lot more powerful than what I’m just using it for.

# Use this script to auto-generate images.
# Adjust settings as needed
# Currently set to create ten images at three different 
# sizes that will have the text 1-10.
import os
import subprocess

def createImage(text, sizePx, outputDir, outputName):
    """ Create square PNG image with white text and transparent background.
    The specified font must already exist on the local computer.
    """
    if not os.path.exists(outputDir):
        os.makedirs(outputDir)
    subprocess.call(["C:\Users\Danial\Desktop\ImageMagick-6.9.1-10-portable-Q16-x64\convert.exe",
                     "-size", sizePx + "x" + sizePx,
                     "-background", "#000000",
                     "-gravity", "center",
                     #"-pointsize", "50", # Don't specify so "best size" is used.
                     #"+antialias", # Uncomment to NOT use antialiasing
                     "-fill", "white",
                     "-font", "C:\Windows\Fonts\Roboto\Roboto-Regular.ttf",
                     "caption:" + text, 
                     outputDir + outputName + ".png"])

for x in xrange(1, 11):
    createImage(str(x), str(24), "./mdpi/", "ic_stat_" + str(x))
    createImage(str(x), str(48), "./hdpi/", "ic_stat_" + str(x))
    createImage(str(x), str(96), "./xhdpi/", "ic_stat_" + str(x))

When the above code is added to a file like create_images.py, you can run it by either double-click or via command line python create_images.py.

Sidenotes:

  • I have a lot more notes in my ImageMagick Cheat Sheet on GitHub.
  • To export as a different image format, just change the extension in the ImageMagick command.
  • Python’s subprocess.Popen can be used for even more options in regards to how the commands are run.
  • There is a Python library for ImageMagick called “PythonMagick”, but I did not use that.
  • You can add ImageMagick to PATH so that the convert command doesn’t have to be hardcoded.
Now a quick word from a tech company who helps support me and this site: