Working with Images in Python?


One of the most popular and considered as default library of python for image processing is Pillow. Pillow is an updated version of the Python Image Library or PIL and supports a range of simple and advanced image manipulation functionality. It is also the basis for simple image support in other Python libraries such as sciPy and Matplotlib.

Installing Pillow

Before we start, we need python and pillow. Incase of Linux, pillow will probably be there already, since major flavour of linux including Fedora, Debian/Ubuntu and ArchLinux includes Pillow in packages that previously contained PIL.

The easiest way to install it is to use pip:

pip install pillow

How to load and display images

First we need a test image to demonstrate some important features of using the python Pillow library.

I’ve used the statue_of_unity photo as a sample image. Download the image and save it in your current working directory.

#Load and show an image with Pillow
from PIL import Image

#Load the image
img = Image.open('statue_of_unity.jpg')

#Get basic details about the image
print(img.format)
print(img.mode)
print(img.size)

#show the image
img.show()

Result

JPEG
RGB
(400, 260)

Above the image is loaded directely using the open() function on Image class. This returns an image object that contains the pixel data for the image as well as details about the image.

The format property on the image will report the image format(e.g png, jpeg), the mode will report the pixel channel format (e.g. CMYK or RGB) and the size will report the dimensions of the image in pixels (e.g. 400*260)

The show() function will display the image using operating systems default application.

Convert an image to grayscale

To convert an image to grayscale, display it and then save it is very easy, just do the following:

#Import required library
from PIL import Image
#Read an image & convert it to gray-scale
image = Image.open('statue_of_unity.jpg').convert('L')
#Display image
image.show()
#Save image
image.save('statue_of_unity_gs.jpg')

Result

After running above program, a file “statue_of_unity_gs.jpg” is created in your current working directory.

Convert to another image type

Converting an image of one type (jpeg) to another(say, png) is also very easy.

from PIL import Image

image = Image.open('statue_of_unity.jpg')
image.save('statue_of_unity.png')


A new image file is created and save in our default directory.

Resize an image

The size(dimensions) of our current image file is 400 * 260px. Incase we want to resize it, and make it of size 440 * 600px, can be done by:

from PIL import Image


image = Image.open('statue_of_unity.jpg')
newImage = image.resize((440, 600))
newImage.save('statue_of_unity_440&600.jpg')


A new file ‘statue_of_unit_440*600.jpg’ of size 440 *600px is created and save in your current working directory.




As you can see, this enlarges our original image into desired dimensions rather than cropping it, which you may not want.

Incase you want to crop the existing image, you can do it using,

image.crop(box=None)

Rotate an image

Below program loads an image, rotates it 45 degrees and display it using an external viewer.


from PIL import Image

image = Image.open('statue_of_unity.jpg')
image.rotate(45).show()



Create thumbnails

Below program will create 128*128 thumbnails of all jpeg images in your current working directory.

from PIL import Image
import glob, os

size = 128, 128

for infile in glob.glob("*.jpg"):
file, ext = os.path.splitext(infile)
image = Image.open(infile)
image.thumbnail(size, Image.ANTIALIAS)
image.save(file + ".thumbnail", "JPEG")


Result

Will return the thumbnails of all jpeg file in my current directory(c:\python\python361) including the ‘statue_of_unity.jpg’ image.

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements