[Image-SIG] How to add a frame + caption to another image?

Dave LeCompte dave at leviathangames.com
Mon Feb 19 17:35:00 CET 2007


Matthew Wilson wrote:
> Later on, I'd like to dynamically add any arbitrary text to the bottom of
> the image.  I suspect this is straightforward also.

I recently needed to add attributions to a directoryful of images:

    im=Image.open(fn).convert("RGBA")
    font=ImageFont.truetype("arial.ttf", 15)
    sz=font.getsize(att)
    labelImage=Image.new("RGBA", sz, (0,0,0,0))
    labelDraw=ImageDraw.Draw(labelImage)
    labelDraw.text((0,0),att,fill=(250,250,0), font=font)
    del labelDraw
    labelImage=labelImage.rotate(270)

    # n.b. use the label image's alpha channel as the mask for the paste
    im.paste(labelImage,(0,0),labelImage)
    im.save(os.path.join(ATT_DIR,fn))

This code draws text in yellow vertically down the image from the upper 
left. In my actual script, I position the text a little more pleasingly.

The only interesting thing that's going on there is that if you omit the 
"mask" argument to the "paste" method call:

    im.paste(labelImage,(0,0))

you end up overwriting the image with transparent pixels, which is probably 
not what you're interested in.



The subject suggests you're also interested in adding a frame around an 
image - I'd probably do this something like so:

    framedImage=Image.new("RGB", framedSize, frameColor)
    framedImage.paste(srcImage, offset)



Hope this helps,
Dave LeCompte 



More information about the Image-SIG mailing list