Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
Python Land
  • Python Tutorial
  • Shop
  • Blog
  • Login / Register
  • Contact
0
Home » Tips & Tricks » Python Image Processing With Pillow
Pillow Logo

Python Image Processing With Pillow

April 9, 2022

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

Table of Contents

  • 1 Installing the image processing package
  • 2 Processing images
  • 3 Displaying images
  • 4 Further reading

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 = 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 the 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:

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!

Get certified with our courses

Learn Python properly through small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

  • Sale Product on sale
    The Python Course for Beginners
    Beginners Python Course (2024)
    € 59.00 Original price was: € 59.00.€ 39.00Current price is: € 39.00.
  • Sale Product on sale
    Computer Fundamentals
    Files, Folders, And The Command Line (2024)
    € 39.00 Original price was: € 39.00.€ 19.00Current price is: € 19.00.
  • Sale Product on sale
    Modules, Packages, And Virtual Environments (2024)
    Modules, Packages, And Virtual Environments (2024)
    € 59.00 Original price was: € 59.00.€ 39.00Current price is: € 39.00.

Leave a Comment Cancel reply

You must be logged in to post a comment.

Subscribe to my newsletter for Python news, tips, and tricks!

Footer subscribe

  • Home
  • Python Courses
  • Privacy Policy
  • About Us
  • Contact us
©2025 Python Land - All rights reserved
Python Land is not affiliated with Python.org or the Python Software Foundation
  • Free Tutorial
    • Install Python
      • Online Python Interpreter
      • How To Open Python on Windows, Mac, Linux
    • Introduction to Python
      • The Python REPL
      • Python Variable
      • Python String
      • Python Print function
      • Python Booleans
      • For-loop and While-loop
      • Python Functions
      • Your First Python Program
      • Python Comments
    • Creating Python Programs
      • The Best Python IDE
      • Installing VSCode
      • VSCode Python Extensions
      • VSCode GUI Tour
      • Python in VSCode: Running and Debugging
    • Classes and Objects in Python
      • Python Constructor
      • Python Inheritance
    • Structure Your Project
      • Python Modules And Importing
      • Python Packages
    • Python Data Types
      • Python Integer
      • Python Float
      • Python Tuple
      • Python List
      • Python Set
      • Python Dictionary
    • Language Deep Dives
      • Python Try Except (Exceptions)
      • Python Functions: Advanced Concepts
      • List Comprehension
      • Python Iterator
      • Python Range
      • Python Docstrings
      • Python pass (Do Nothing)
    • Interaction with the OS
      • Python Read And Write File
      • Python Subprocess: Run External Commands
    • Venvs / Package Management
      • Working With Python’s venv
      • Pip install: the Python package manager
      • Python Poetry: Package and venv Management Made Easy
      • Python Pipenv: Another Package Manager
      • Pipx: Safely Install Packages Globally
    • Python Concurrency
      • The Python GIL (Global Interpreter Lock)
      • Setting the Baseline
      • Python Threading
      • Python Multiprocessing
    • Data Processing with Python
      • Working With JSON
        • JMESPath Python: JSON Query Language
      • Python YAML: How to Load, Read, and Write YAML
      • Python CSV: Read And Write CSV Files
    • Migrating From Python 2 to 3
      • Check Python Version On The Command-Line
      • How to Migrate To Python 3
      • Python 3 Advantages
    • Using The Unix Shell
      • Basic Linux Commands
      • Bash Scripts
      • Using the Find Command in Linux
      • Unix Pipes
      • Process and Highlight JSON with jq
      • Using the Bash History
      • Bash Multiprocessing
    • Deployment
      • PyInstaller: Create An Executable From Python Code
      • How To Use Docker To Containerize Your Python Project
      • Automatically Build and Deploy Your Python Application with CI/CD
      • Guerrilla Scale Your Web Application
    • Data Science
      • Jupyter Notebook
      • NumPy: Getting Started Tutorial
    • Python Learning Resources
  • Python Courses
  • Blog
  • Login / Register