Pillow Python Tutorial – Complete Guide

Welcome to this comprehensive tutorial on the Python Imaging Library (PIL), famously known as Pillow. This powerful library will open up countless possibilities for game developers, aspiring coders, and data scientists. Whether you are starting on your coding journey or flexing your experienced dev muscles, this tutorial will serve as a valuable guide.

What is Pillow in Python?

Pillow is an open-source Python imaging library that supports a wide range of image processing capabilities. It is an extended and maintained version of the Python Imaging Library (PIL), which adds some user-friendly features and supports a spectrum of contemporary image file formats.

With the Pillow library, you can create, manipulate, and transform images in your Python programs. It is a powerful tool for developers, especially in creating engaging game mechanics, dynamic content, and performing complex image analysis.

From simple tasks such as displaying, resizing, and cropping images to applying filters, adding text, or even creating animations, Pillow’s range of functionalities is vast. You no longer have to rely on extensive graphic software if you need quick or automated image handling directly in your Python code.

Now that we’ve learned the what, why, and what of the Pillow library, let’s dive into some code examples and bring this library to life.

CTA Small Image

Getting Started with Pillow

The first step you need in your journey to master Pillow is to install the library. Simply run pip install Pillow in your terminal or command prompt.

pip install Pillow

Opening, Rotating and Displaying an Image

This is a basic task in the Pillow library. Let’s learn how to open, rotate, and display an image.

from PIL import Image

image = Image.open('example.jpg')

rotated_image = image.rotate(45)


The above code opens the image, rotates it 45 degrees, and displays it on your screen.

Converting Images to Grayscale

Converting images to grayscale is a common task when dealing with image processing. You can easily do this using the Pillow library:

from PIL import Image

image = Image.open('example.jpg')

gray_image = image.convert('L')


By converting the image to ‘L’ mode, we transform it from a colored image to a grayscale one.

Resizing Images

Resizing images is a common task especially when dealing with large datasets of images. Here’s how you can accomplish this with Pillow:

from PIL import Image

image = Image.open('example.jpg')

resized_image = image.resize((1920, 1080))


The above code resizes the image to a 1920×1080 size. Alternately, you can also print resized_image.size to confirm.

Cropping Images

Let’s learn how to crop an image using the Pillow library. This functionality is very handy when you need to extract a specific portion of an image.

from PIL import Image

image = Image.open('example.jpg')

cropped_image = image.crop((100, 100, 400, 400))


This code snippet crops the image from the coordinates (100,100) to (400,400).

Adding Text to Images

You can easily overlay text onto your images with Pillow. It provides a great way to dynamically display information on top of visuals. Here’s an example:

from PIL import Image, ImageDraw, ImageFont

image = Image.open('example.jpg')

draw = ImageDraw.Draw(image)

font = ImageFont.truetype('arial.ttf', size=45)

draw.text((50, 50), 'Hello, Zenva!', fill='rgb(255, 255, 255)', font=font)


This code overlays the text “Hello, Zenva!” on the image at coordinates (50,50).

Applying a Filter

Pillow also allows you to apply various image-enhancing filters to your image. Here’s how you can apply the BLUR filter:

from PIL import Image, ImageFilter

image = Image.open('example.jpg')

blurred_image = image.filter(ImageFilter.BLUR)


Run the code, and you’ll see a blurred version of your image.

Combining Images

When you want to merge different images into one, Pillow has you covered with the Image.alpha_composite() function. Check out this example:

from PIL import Image

image_1 = Image.open('example.jpg')
image_2 = Image.open('overlay.png')

combined_image = Image.alpha_composite(image_1, image_2)


This snippet overlays image_2 on top of image_1, assuming both images have the same dimensions.

Creating Thumbnails

Creating thumbnails is an easy task with Pillow. Here’s how:

from PIL import Image

image = Image.open('example.jpg')

image.thumbnail((90, 90))

image.save('thumbnail.jpg', 'JPEG')


This code reduces the size of your image to a thumbnail size and saves it with a new name.

Getting Pixel Information

Want to get more technical and access individual pixel information? Pillow can do that too! Here’s how:

from PIL import Image

image = Image.open('example.jpg')

print(image.getpixel((100, 200)))

By using the getpixel() function, you can get the color information of a specified pixel. In the above code, we get the color information of the pixel at (100,200).

From these examples, you can see how the Pillow library’s comprehensive features make Python’s imaging capabilities more potent. Learning and mastering Pillow can give you a lot more control and flexibility when working with images in your code. Experience the boundless creativity Pillow’s imaging operations unlock for you!

Where to go next with your Python Journey?

What a journey it has been! From discovering the vast capabilities of the Pillow library to creating, manipulating, and enhancing images using Python, we hope you found this tutorial valuable. As the saying goes, every ending is a new beginning.

Enhance your knowledge, coding prowess, and garner real-world programming experiences with Zenva’s Python Mini-Degree. Here’s why you should consider it:

  • Comprehensive Coverage: This collection of courses offers a 360° insight into Python programming. You’ll dabble in coding fundamentals, sophisticated algorithms, game and app development, and beyond.
  • Project-based Learning: Each course is designed so you can learn by making. Create your very own games and real-world applications with Python.
  • Flexible Learning: Schedule doesn’t fit fixed classroom lessons? No worries! Zenva’s Python Mini-Degree is flexible – you learn at your pace.
  • Career Opportunities: Python is in high demand in today’s job market, especially in fields like data science and AI. The knowledge gained in these courses is your stepping stone to a bright career future.

And guess what? The Python Mini-Degree isn’t all we offer. For the passionate and the curious who love to learn, we have a wide range of courses that’ll fit your needs perfectly! Check out our complete Python course collection to continue your education. With Zenva, you can go from beginner to professional — the sky’s the limit!


Images act as a creative medium to portray information, entertain, and engage viewers. Having the ability to manipulate and modify images using Python gives you an incredible tool in your development arsenal. We’ve learned the power of the Pillow library in Python, and it’s now up to you to take this knowledge, apply it, and create stunning visual content in your Python projects.

Remember, the journey of learning and creation never ends. Let’s continue to challenge ourselves, discover new sets of skills, and redefine our possibilities with Python. It’s time to take your Python knowledge to the next level with us at Zenva. Make sure to check out our comprehensive Python Mini-Degree, and happy coding!

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.