PIL is the Python Imaging Library. It allows you to programmatically edit images in python.
And now for some examples! To play with PIL, you first need a picture of a cute puppy (kittens work, too).
Get started by loading and showing an image with PIL. The im.show() method should display the image in your operating system's default image viewer.
>>> import Image >>> im = Image.open("louie.jpg") >>> im.show()
Now, discover some information about this image! We can get it's size in pixels as a tuple (width, height) as well as the bit-depth and the number of layers.
>>> im.size (640, 396) >>> im.bits 8 >>> im.layers 3 >>> im.mode 'RGB'
Resize an image:
>>> smaller = img.resize((213, 132)) >>> smaller.show()
Rotate an image:
>>> rotated = img.rotate(45) >>> rotated.show()
Save a copy of your rotated image!
>>> rotated.save("rotated.jpg")
Digital images are essentially a grid of color values for each pixel. Values will often range from 0 (black) to 255 (white). For example, if you had a 2x2 image of a checkerboard, the data might look something like this:
0 255 255 0
But, that would be just a black and white image! Color images often have multiple layers of this sort of data. These layers are also called bands.
Our sample image has three layers/bands. You can call the im.getbands() method to get some information about the bands in your image. This one has Red, Green, and Blue layers. You can think of each layer as a separate, black and white photo
>>> im.layers 3 >> im.getbands() ('R', 'G', 'B')
PIL gives you access to all of the pixel values as one giant list, using the im.getdata() method. Note that each pixel is actually the value for all three layers; Red, Green, and Blue.
>>> pixels = list(im.getdata()) >>> pixels[0] (109, 98, 92)
You can modify this data by altering the pixel values, then loading them back into a new image! Lets just increase the redness of each pixel.
>>> redder_pixels = [] # A new list for a new image >>> for r, g, b in pixels: new_red = r + int(r * .5) # 50% redder new_data = (new_red, g, b) redder_pixels.append(new_data) >>> >>> # Create a new image of the same mode and size >>> redim = Image.new(im.mode, im.size) >>> >>> # put our new pixel data in the new image >>> redim.putdata(redder_pixels) >>> redim.show()
PIL also includes several tools for filtering images (available in the ImageFilter module), and includes a number of pre-defined filters. Common filter techniques include Blurring, Sharpening, and finding edges.
Let's first Blur an image
>>> import ImageFilter >>> >>> blurred = im.filter(ImageFilter.BLUR) >>> blurred.show()
Or sharpen it
>>> sharpened = im.filter(ImageFilter.SHARPEN) >>> sharpened.show()
Or find the edges
>>> edged = im.filter(ImageFilter.EDGE_ENHANCE) >>> edged.show()
The pictures used in this tutorial are available under a Creative Commons license, as is the original photo of Louie by bradmontgomery.
hi can i merge two images into 1?
i try this
from PIL import Image
import sys
if not len(sys.argv) > 3:
raise SystemExit("Usage: %s src1 [src2] .. dest" % sys.argv[0])
images = map(Image.open, sys.argv[1:-1])
w = sum(i.size[0] for i in images)
mh = max(i.size[1] for i in images)
result = Image.merge("RGBA", (w, mh))
x = 0
for i in images:
result.paste(i, (x, 0))
x += i.size[0]
result.save(sys.argv[-1])