[Tutor] Using Python to keep peace in the house (a little success story)

Terry Carroll carroll at tjc.com
Sun May 29 10:32:28 CEST 2005


My wife and I have a disagreement over the use of our digital camera.  She 
likes to have the date printed on the image.  I hate that, I like the 
image to be pristine.

Our camera has the option to put the date on the image, but we can't agree 
on whether to enable it.

Well, Python to the rescue.  Since I learned about Python Imaging Library
doing a couple of the Python Challenges, I decided to find whether I could
add the date to the image after the fact.  It turned out to be pretty
easy.  I had a proof-of-concept done in about 10 minutes, and a finished
project a couple hours later.  I now have a routine called "Imprint" that
adds any arbitrary text to a JPG file; and one called "GetFileDate" that
gets the date of the file (using the EXIF info stored with the image, if
it exists).

Now I can write a little program to loop through all the undated images
(which keep me happy) and make copies with the date, (to keep her happy).

Here's my solution, in case anyone is interested:


==========================================================
import Image, ImageDraw, ImageFont

def GetFileDate(file):
    """
    Returns the date associated with a file.
    For JPEG files, it will use the EXIF data, if available
    """
    try:
        import EXIF
        # EXIF.py from http://home.cfl.rr.com/genecash/digital_camera.html
        f = open(file, "rb")
        tags = EXIF.process_file(f)
        f.close()
        return str(tags['Image DateTime'])
    except (KeyError, ImportError):
        # EXIF not installed or no EXIF date available
        import os.path, time
        return time.ctime(os.path.getmtime(file))

def ReduceOpacity(im, opacity):
    """
    Returns an image with reduced opacity.
    Taken from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879
    """

    import ImageEnhance
    assert opacity >= 0 and opacity <= 1
    if im.mode != 'RGBA':
        im = im.convert('RGBA')
    else:
        im = im.copy()
    alpha = im.split()[3]
    alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
    im.putalpha(alpha)
    return im

def Imprint(im, inputtext, font=None, color=None, opacity=.6, margin=(30,30)):
    """
    imprints a PIL image with the indicated text in lower-right corner
    """
    if im.mode != "RGBA":
        im = im.convert("RGBA")
    textlayer = Image.new("RGBA", im.size, (0,0,0,0))
    textdraw = ImageDraw.Draw(textlayer)
    textsize = textdraw.textsize(inputtext, font=font)
    textpos = [im.size[i]-textsize[i]-margin[i] for i in [0,1]]
    textdraw.text(textpos, inputtext, font=font, fill=color)
    if opacity != 1:
        textlayer = ReduceOpacity(textlayer,opacity)
    return Image.composite(textlayer, im, textlayer)

def test():
    font=ImageFont.truetype("Arial.ttf", 40)
    imagefile = "DSCN0214.JPG"
    datetext = GetFileDate(imagefile)
    print datetext
    im = Image.open(imagefile)
    im0 = Imprint(im, datetext, font=font, opacity=0.5, color=(255,255,255))
    im0.save("DSCN0214-dated.jpg", "JPEG")

if __name__ == "__main__":
    test()

       
==========================================================




More information about the Tutor mailing list