Embed a photo caption

Contents
Related files

The importance of captions and branding

Installing Pillow

http://pillow.readthedocs.org/en/latest/installation.html

Need version 2.9 to get the multiline_text feature.

Simple demonstration

Using the invasion drawing by Captain Creekmore from D-day invasion of France.:

curl -s \
  https://upload.wikimedia.org/wikipedia/en/thumb/c/ca/Creekmore-gliders.jpg/640px-Creekmore-gliders.jpg \
  -o /tmp/gliders.jpg
python imagecreditor_basic.py /tmp/gliders.jpg "A depiction of D-Day by Captain Raymond Creekmore"

The resulting image:

Creekmore Gliders

Dealing with overflow

python imagecreditor_basic.py /tmp/gliders.jpg \
"A depiction of D-Day by Captain Raymond Creekmore
 Source: National Archives:Black and White and Color Photographs 
    of U.S. Air Force and Predecessor Agencies Activities,
    Facilities, and Personnel - World War II"

Creekmore Gliders overflow caption

Using multiline_text

Replace:

    capdraw.text(
        (CAPTION_LEFT_PADDING, img_height + CAPTION_TOP_PADDING),
        text, TEXT_COLOR)

with:

    capdraw.multiline_text(
        (CAPTION_LEFT_PADDING, img_height + CAPTION_TOP_PADDING),
        text, TEXT_COLOR)

cutoff image

Finding text size

from PIL import Image
from PIL import ImageDraw
draw = ImageDraw.Draw(Image.new('RGB', (0,0)))
txt = "A depiction of D-Day by Captain Raymond Creekmore\nSource: National Archives:Black and White and Color Photographs\nof U.S. Air Force and Predecessor Agencies Activities,\nFacilities, and Personnel - World War II"
draw.multiline_textsize(txt)
# (378, 44)

Changing fonts

from PIL import ImageFont
hfont = ImageFont.truetype("/System/Library/Fonts/HelveticaNeue.dfont", 14)
draw.multiline_textsize(txt, font = hfont)
# (452, 67)
gfont = ImageFont.truetype("/Library/Fonts/GillSans.ttc", 14)
draw.multiline_textsize(txt, font = gfont)
# (387, 63)