Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Python Image Processing with Pillow: Comprehensive Guide

Python Image Processing with Pillow: Comprehensive Guide

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:

Table of contents

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

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 = Image.open("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_image.save("rotated.jpg")

Displaying images

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

from PIL import Image

im = Image.open("kittens.jpg")
im.show()
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:

Python Image Processing with Pillow: Comprehensive Guide

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!


Python

  1. Master Python’s str.count(): How to Count Characters & Substrings with Examples
  2. Python round() Function Explained with Practical Examples
  3. Mastering Python's map() Function: Syntax, Examples, and Best Practices
  4. Python timeit() – Measuring Execution Time with Practical Examples
  5. Python Counter in collections – Efficient Counting, Updating, and Arithmetic Operations
  6. Python list.count(): Expert Guide with Practical Examples
  7. Python Module Importing – A Practical Guide with Examples
  8. Master Emoji Handling in Python: Convert, Display, and Use Unicode Emoticons
  9. Python XML Processing: A Guide to Interoperable Data Management
  10. Master Python Extension Programming with C: Build Fast, Native Modules