In last post I was writing about PIL, also known as Python Imaging Library, this library can be used to manipulate images quite easy. PIL hasn't seen any development since 2009. Therefore, the kind users of this site suggested to take a look at Pillow. This article well tell you how to use Pillow.
What is Pillow?
Pillow is a fork of PIL (Python Image Library), started and maintained by Alex Clark and Contributors. It was based on the PIL code, and then evolved to a better, modern and more friendly version of PIL. It adds support for opening, manipulating, and saving many different image file formats. A lot of things work the same way as the original PIL.
Download and Installing Pillow
Before we start to use the Pillow, we must first download and install it. Pillow is available for Windows, Mac OS X and Linux. The most recent version is "2.2.1" and is supported by python 2.6 and above. To install Pillow on Windows machines you can use easy_install:
To install Pillow on Linux machines simply use:
sudo pip install Pillow
To install Pillow on Mac OS X I had to first install XCode and then install the the prerequisites is via Homebrew. After Homebrew was installed, I ran:
$ brew install libtiff libjpeg webp littlecms
$ sudo pip install Pillow
Please let me know if you know an easier way to do this on Mac.
Verify that Pillow is installed
To verify that Pillow is installed, open up a Terminal and type in the following line:
$ python Python 2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from PIL import Image
If the system comes back with a ">>>", the Pillow modules are properly installed.
File formatsBefore we start using the Pillow module, let us mention some of the filetypes that is supported. BMP EPS GIF IM JPEG MSP PCX PNG PPM TIFF WebP ICO PSD PDF Some of the file types, you only have the possibility to read, and others you can only write. To see the full list of supported file types, and more information about them, take a look at the handbook for Pillow.
How to use Pillow to manipulate an imageSince we are going to work with images, let us first download one. If you already have a picture to use, go ahead and skip this step. In our example we will use a standard test image called "Lenna" or "Lena". That standard test image is used in many image processing experiment. Just go here and download the picture. If you click on the image, it will save it as an 512×512px image.
Let us look at the possible uses for this library. The basic functions are found in the Image module. You can create instances of this class in several ways either by loading images from files, processing other images, or creating images from scratch. Import the Pillow modules you want to use.
from PIL import Image
You can then access functions as usual, e.g.
myimage = Image.open(filename) myimage.load()
Load an Image
To load an image from your computer, you can use use "open" method to identify the file, and then load the identified file using myfile.load(). Once the image is loaded, you can do a number of things with it. I often use the try/except block when dealing with files. To load our image using try/except:
from PIL import Image, ImageFilter try: original = Image.open("Lenna.png") except: print "Unable to load image"
When we read files from disk using the open() function, we don't have to know the format of the file to. The library automatically determines the format based on the contents of the file. Now when you have an Image object, you can use the available attributes to examine the file. For example, if you want to see the size of the image, you can call the "format" attribute.
print "The size of the Image is: " print(original.format, original.size, original.mode)
The "size" attribute is a 2-tuple containing width and height (in pixels). Common "modes" are "L" for greyscale images, "RGB" for true color images, and "CMYK" for pre-press images. The output of above should give you this:
The size of the Image is: ('PNG', (512, 512), 'RGB')
Blur an Image
This example will load an image from the hard drive and blurs it. [source]
# Import the modules from PIL import Image, ImageFilter try: # Load an image from the hard drive original = Image.open("Lenna.png") # Blur the image blurred = original.filter(ImageFilter.BLUR) # Display both images original.show() blurred.show() # save the new image blurred.save("blurred.png") except: print "Unable to load image"
The blurred image should look like this:
To display the image, we used the "show()" methods. If you don't see anything, you could try installing ImageMagick first and run the example again.
A very common thing to do is creating thumbnails for images. Thumbnails are reduced-size versions of pictures but still contains all of the most important aspects of an image.
from PIL import Image size = (128, 128) saved = "lenna.jpeg" try: im = Image.open("Lenna.png") except: print "Unable to load image" im.thumbnail(size) im.save(saved) im.show()
The result of our program, showing the thumbnail:
Filters in Pillow
The Pillow module provides the following set of predefined image enhancement filters:
BLUR CONTOUR DETAIL EDGE_ENHANCE EDGE_ENHANCE_MORE EMBOSS FIND_EDGES SMOOTH SMOOTH_MORE SHARPEN
In our last example for today, we will show how you can apply the "contour" filter to your image. The code below will take our image and apply
from PIL import Image, ImageFilter im = Image.open("Lenna.png") im = im.filter(ImageFilter.CONTOUR) im.save("lenna" + ".jpg") im.show()
Our image with the "contour" filter applied:
I enjoyed to try out Pillow, and I will write more posts about it in the future.
Recommended Python Training – DataCamp
For Python training, our top recommendation is DataCamp.
Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.
Datacamp has beginner to advanced Python training that programmers of all levels benefit from.
Read more about: