Create image with border and caption using Python script with a little help from Wand and ImageMagick

My goal was to create a script that generates images that have different sizes and colors, that have a definable border and variable image caption and file name. Simplified version is below. You can adjust to your own liking.

from wand.color import Color
from wand.image import Image
from wand.image import Font
import datetime
# directory for saving file
export_dir = "/tmp/images/generated/"
# get the date to add to caption and file name
today = datetime.date.today()
todaydate = today.strftime('%Y-%m-%d')
# some variables : these can be obtained from arrays, lists, anywhere
file_text = "deployment_test"
image_text = "Deployment Test"
image_width = 320
image_height = 200
font_size = 24
#function to create the image
def create_image_caption(widthsize,heightsize,backgroundcolor,borderwidth,bordercolor,image_caption,text_color,font_size):
    with Image(width=widthsize-2*borderwidth,height=heightsize-2*borderwidth,background=Color(backgroundcolor)) as image:
        image.border(Color(bordercolor),borderwidth,borderwidth)
        font=Font(path='/Library/Fonts/Verdana.ttf',size=font_size,color=Color(text_color))
        image.caption(image_caption,left=0,top=0,width=widthsize-10,height=heightsize-5,font=font,gravity='center')
        image.save(filename=export_dir+str(widthsize)+"x"+str(heightsize)+"_"+backgroundcolor+"_"+todaydate+"_"+file_text+".jpg")
# set the caption text
caption_text = str(image_width)+"x"+str(image_height)+"\n"+todaydate+"\n"+image_text
# create the image
create_image_caption(image_width,image_height,'green',1,'red',caption_text,'white',24)

Some explanations:

  • This worked for me with Python 2.7
  • The script depends on Wand : a ctypes-based simple ImageMagick binding for Python. Make sure to import wand
  • Set the export dir to your desired value
  • image width/height, file text and image text are just some variables I set, choose and define your own as desired
  • create_image_caption is the function that creates the image. It contains:
    • image.border(Color(bordercolor),borderwidth,borderwidth) : that creates the image with the border
    • font=Font(path=’/Library/Fonts/Verdana.ttf’,size=font_size,color=Color(text_color)) : specifies the font and its location, note that the directory I used is for a MAC
    • image.caption(image_caption,left=0,top=0,width=widthsize-10,height=heightsize-5,font=font,gravity=’center’) : sets the caption location and text
    • image.save(filename=export_dir+str(widthsize)+”x”+str(heightsize)+”_”+backgroundcolor+”_”+todaydate+”_”+file_text+”.jpg”) : specifies where to save the image and what to name it
  • caption_text = str(image_width)+”x”+str(image_height)+”\n”+todaydate+”\n”+image_text : just concatenates the string to show as caption
  • create_image_caption(image_width,image_height,’green’,1,’red’,caption_text,’white’,24) : this line calls the function with all the desired variables
  • Run and have fun
  • In the script that I use on a regular basis, image sizes, colors, caption text and such are dynamically inserted so after running it I have 40-50 images ready for various testing purposes. When someone uses them, it is obvious when they were created, their size and for what purpose (ie deployment test)