Skip to content

Python Image Processing - PIL (pillow) Tutorial

Python Tutorials

Contents

  1. Introduction
  2. Image Processing
  3. What is PIL?
  4. PIL vs Pillow
  5. PIL Tutorials
    1. How to install PIL
    2. Common PIL modules
  6. Resources
  7. Python Library Packaging
  8. Summary

What is Image Processing?

Image processing is the computational transformation of images. This usually involves working with computer languages to work on image as a 2 dimensional signal through its pixel composition. Image processing also is a branch of signal processing.

Image Editing

Image editing can be the same outcomes of changing, altering, editing, enhancing the image but it usually implies doing those things through gui of image editing software and not as a vector or signal.

Image Processing vs Image Manipulation

Image manipulation, image processing and image editing can all be used interchangeably depending on the context however, one scenario image manipulation differs from image editing is that image manipulation can refer to more substantial changes such as moving objects, removing people, adding object, changing scene etc while image editing can be more appropriate for image enhancing, resizing, rotating type operations.

What is PIL (pillow)?

PIL is an imaging library for Python programming language.

Script is a piece of code that’s used to automate system oriented tasks. When you write code that carries out a practical task without needing compiling this can be called scripting.

PIL has very powerful image processing capabilities and it can handle many types of images (BMP, DIB, EPS, GIF, ICO, IM, JPEG, JPG, JPEG 2000, MSP, PCX, PNG, PPM, SGI, TGA, TIFF, WebP, XBM just to name a few formats) and image modes such as RGB, RGBA, B&W and monochrome.

PIL also works on practically all operating systems including, WindowsLinux, and MacOSX.

PIL v.s. pillow

Pillow is a fork of PIL, Python Imaging Library. PIL was released in late 2009 and maintained until 2011. In 2011 PIL development stopped. Luckily PIL fell on a soft place since pillow library picked up from where it’s left and it continues to exist as pillow today.

This can create some terminology confusion but it’s safe to say that pillow and PIL refer to the same thing in most situations today as the terms became somewhat synonymous.

How to Install PIL (pillow)

pip install Pillow
or
pip3 install Pillow

Commonly used PIL (pillow) Modules

  • Image: Most fundamental image operations can be done with it such as:
    • opening an image, showing an image, saving an image,
    • managing alpha transparency channels,
    • converting between image modes,
    • merging, blending and combining images,
    • copying and pasting images,
    • creating new images or image layers,
    • reading from and writing to image pixels
    • Cropping, rotating, resizing, flipping images
    • Transposing images
  • ImageEnhance: Can be used for image enhancement operations regarding; color, contrast, brightness and sharpness via these classes:
    • PIL.ImageEnhance._Enhance
    • PIL.ImageEnhance.Color
    • PIL.ImageEnhance.Contrast
    • PIL.ImageEnhance.Brightness
    • PIL.ImageEnhance.Sharpness
  • ImageDraw: Useful to draw pretty much anything on an image.
  • ImageFont: Used to create a font wrapper which can be used to draw text on images (supports bitmap and truetype fonts)
  • ImageGrab: Useful to get contents of screen or from the clipboard. Similar function to the printscreen key on keyboard.
  • ImageFilter: Includes common useful filters that can be applied to images via many different means such as pixel thresholds, based on minimum pixels, maximum pixels, median pixels etc. Furthermore, these filter objects can be applied via a number of methods such as gaussianblur, boxblur, unsharpmax etc.
    • BLUR,
    • CONTOUR,
    • DETAIL,
    • EDGE_ENHANCE,
    • EMBOSS,
    • FIND_EDGES,
    • SHARPEN,
    • SMOOTH
  • ImageCms: Image color profile management.
  • ImageStat: Getting global statistics of images at pixel level such as; mean, minimum, maximum, count, sum, variance, standard deviation of pixels at each band.
  • ImageColor: Color table conversion operations

PIL has even more modules but most people will probably use top 6 modules listed here in most cases.

An Image from PIL tutorials

Resources

  • PIL has a documentation that’s quite extensive which can be found here.
  • If you’d like to support your learning with watching a video, here is a relatively short Youtube tutorial (~15mins) explaining most basic operations in pillow very well and clearly, video below.

Summary

We have learned some digital image basics in this Python tutorial. Before understanding the fundamentals of digital images it’s harder to carry out image processing operations with coding. We have also clarified some of the digital imaging terms such as image processing and image manipulation.

Once you understand each pixel consists of numerical data it becomes a breeze to write Python code to do all kinds of imaging operations.

Then, we have explained how to edit images in Python using PIL (pillow) library through PIL tutorials with Python.

We have also seen other image editing tutorials such as how to resize images as a batch, how to tell different pixels between images etc.