Python Image Processing With Pillow

The Pillow Python Imaging Library is ideal for image processing. Typically, it’s used for archival and batch processing applications. Of course, you’re free to use it for anything else you can think of. You can use the library to:

  • Create thumbnails
  • Convert between file formats,
  • Print images
  • Fet a histogram (ideal for automatic contrast enhancement)
  • Rotate images
  • Apply filters like blur

Installing the image processing package

To install Pillow, which is a fork and continuation of the original Python Imaging Library, use the pip install command:

pip3 install Pillow

After this, you can import the module which is called PIL, or you can import parts of the module with the from PIL import ..... syntax.

Processing images

Pillow offers several filters which become available after importing ImageFilter. For example, to blur an image, use:

from PIL import Image, ImageFilter

im ="kittens.jpg")
blurred = im.filter(ImageFilter.BLUR)

Other filters include SHARPEN, SMOOTH, and EDGE_ENHANCE. For a complete list of filters, check the reference docs on ImageFilter.

To rotate an image by 180 degrees:

rotated_image = im.rotate(180) 

And finally, to save the results of your hard work:"rotated.jpg")

Displaying images

Besides image processing, this library can also be used to display images on the screen. Here’s some example code to display a file called kittens.jpg:

from PIL import Image

im ="kittens.jpg")
print(im.format, im.size, im.mode)
# JPEG (1920, 1357) RGB

In the following animated gif, I demonstrate how to use Pillow right from IPython:

Demonstration of python image processing using Pillow
Showing some kittens using Pillow from IPython

Further reading

The library has much more to offer. To learn everything about Python image processing using Pillow, it’s best to head over to the official tutorial!

Please share this article with others!

Python Courses

Are you enjoying this free tutorial? Please also have a look at my premium courses. They offer a superior user experience with small, easy-to-digest lessons and topics, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

The Python Fundamentals Course For Beginners
Now for $49 (from $59)

Python Fundamentals I is a course for beginners that will get you started with Python in no time. Learn all the essentials, test your progress with quizzes and assignments, and bring it all together with the final course project!

Python Course for Beginners

Modules, Packages, And Virtual Environments
Now for $49 (from $59)

Python Fundamentals II covers creating your own modules and packages, using virtual environments and Python package managers to make your life as a programmer easier. Advance your productivity as a Python programmer!

Python Fundamentals 2

Leave a Comment

Share to...