The Python Imaging Libray (PIL) is a library that allows Python Programmers to open, save, and manipulate images. PIL mostly deals with image formats TIFF, JPG, BMP, PNG, and GIF, but there are even ways for a common user to add their own formats to the Library. The Python Imaging Library greatly speeds up programming time if the Python program deals with images at all.
To open an image with PIL, the library module merely needs to be defined, and then the Open command needs to be issued. To create an image from scratch, the “new” command must be used. Saving an image is as simple as using the “save” command. (Use “save” after modifying or creating an image).
#start out python code with the import of the Image Library #The Image and ImageDraw libraries are part of PIL import Image, ImageDraw #now open the mypicture.bmp image, assign the image to the variable "bmp" bmp = Image.open("mypicture.bmp") #create a new image using the "new" command newbmp = Image.new("RGB", (128, 128), "White") #save the new image newbmp.save("mypicture2.bmp") #now, display both of the images bmp.show() newbmp.show() # To do more than just a block of color, there are simple commands to write whatever you want. # This is where the beauty of PIL comes into play. # To draw a red line on the black image that was just created draw = ImageDraw.Draw(newbmp) draw.line((0, 0, 100, 100), fill=rgb(255,0,0)) del draw # And to save and see the recent handiwork newbmp.save("mypicturewithline.bmp") newbmp.show()
Using PIL, you can also grab a single pixel and change it. Let’s see hows that’s done…
import Image, ImageDraw bmp = Image.open("mypicture.bmp") draw = ImageDraw.Draw(bmp ) draw.point((64,64), fill=None) del draw bmp.show()
To be able to work with PIL, you only need to know a few functions. New and open will get you an image to work with, save will write your image to the disk, and show will display what you’ve done so far. A bunch of other functions exist for modifying your image once you have it open. You can draft, convert (from one file type to another), crop (cut the image to a new size), and thumbnail and resize (excellent for automatic blogging or news sites).
How to add a watermark to your images using Python Imaging Library
One method involves putting one image on top of another. You will need an image of your transparent watermark handy, and then you’ll need the image you want to put it on. Then it’s just a simple process of “pasting” your watermark on the image. Let’s see how easy this process is using PIL.
import Image, ImageDraw bmp= Image.open("mypicture.bmp") mywatermark = Image.open("mywatermark.bmp") bmp.paste(mywatermark, (0,0,100,100)) bmp.show()
Another water mark method just writes a text onto your image. You’ll need a little more doing, but it’s still really easy. (You will need a font file handy, just so you know.) In the middle of this function, you will notice a while loop. What we are doing here is testing the text size against the size of the image we want to watermark. We want the text to be a big as possible, and still remain inside the image. A little dynamic code goes a long way.
import Image, ImageDraw, ImageFont bmp = Image.open("mypicture.bmp") mywatermark = Image.new("RGBA", (bmp.size[0], bmp.size[1])) drawing = ImageDraw.ImageDraw(mywatermark, "RGBA") size = 0 while True: size += 1 testfont = ImageFont.truetype(FONT, size) testtextw, testtexth = testfont.getsize("My Watermark Text") if testtextw+testtexth/3 > mywatermark.size[0]: break font = nextfont textw, texth = testtextw, testtexth drawing.setfont(font) drawing.text(((mywatermark.size[0]-textw)/2, (mywatermark.size[1]-texth)/2), "My Watermark Text") bmp.paste(mywatermark, None, mywatermark) bmp.show()
Nearly any image manipulation is incredibly simple when you use the Python Imaging Library. This is a very well thought out program that is freely available for you to use.