Python provides a lot of useful tools and libraries to handle images.

If you want to modify an image you can use the PIL (Python Image Library) to apply filters, draw a line, draw a rect, draw an oval, draw an image, or draw a text on the image.

In the following few lines of code I’m going to show to you how to add a copyright mark onto your own images.

The file we want to mark is the following:

original

# Importing the needed library

import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

# Loading Fonts….
# Note the following line works on Ubuntu 12.04
# On other operating systems you should set the correct path
# To the font you want to use.
font = ImageFont.truetype("/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf",25)

# Opening the file gg.png
imageFile = "gg.png"
im1=Image.open(imageFile)

# Drawing the text on the picture
draw = ImageDraw.Draw(im1)
draw.text((0, 0),"copyright/left xappsoftware.com",(255,255,0),font=font)
draw = ImageDraw.Draw(im1)

# Save the image with a new name
im1.save("marked_image.png")

The program is really simple and self explanatory:

We load the PIL library

We load the font we want to use (this can be different on different operating systems)
We load the image we want to modify
We draw the string onto the image
We save back the image with a different name.
After running this program the file will become as follows:

 

Screen Shot 2012-12-27 at 22.59.47

Gg1