
To begin working with images in your Python applications, the first thing you need to do is load your image from the disk. This is typically done using libraries like Pillow, which is a fork of the original PIL (Python Imaging Library). It’s user-friendly and allows for a variety of image formats. Let’s see how you can get started.
First, make sure you have Pillow installed in your environment. You can do this using pip:
pip install Pillow
Once Pillow is installed, you can load an image from your disk with just a few lines of code. Here’s a simple example:
from PIL import Image # Load an image from the specified path image_path = 'path/to/your/image.jpg' image = Image.open(image_path) # Display the image image.show()
This code snippet does two main things: it opens the image file and then displays it. The Image.open() function takes the path of the image as a parameter and returns an image object. After that, calling show() on the image object opens the default image viewer on your system, showing you the image right away.
If you encounter any issues with the file path, ensure that it’s correctly specified. Sometimes, the problem could be as simple as a typo in the file path or the image not being in the expected directory. A common practice is to use absolute paths instead of relative ones to avoid these headaches.
Now that we’ve got our image loaded, let’s talk about saving your masterpiece. This is surprisingly easy…
Anker Phone Charger, 65W 3-Port Fast Compact Foldable USB C Charger Block, Type C Charger Fast Charging for MacBook Pro/Air, iPad Pro, Galaxy S20, Dell XPS 13, Note 20/10+, iPhone 17 Series, and More
Now retrieving the price.
(as of June 5, 2026 03:24 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Saving your masterpiece is surprisingly easy
To save an image after you’ve made some modifications or created a new masterpiece, you can use the save() method provided by the Pillow library. It’s as straightforward as loading the image. You simply specify the filename you want to save it as, and optionally, the format if it differs from the original.
Here’s an example of how to save an image:
# Assuming 'image' is the Image object you want to save save_path = 'path/to/save/your/image.png' image.save(save_path)
In the above code, we save the image as a PNG file. Pillow can handle various formats, including JPEG, BMP, GIF, and TIFF. You can specify the format explicitly if you prefer:
image.save('path/to/save/your/image.jpg', format='JPEG')
This can be useful when you want to ensure the saved image is in the correct format regardless of the file extension. When saving images, be aware of potential quality loss, especially
So how do you actually see the darn thing
When it comes to viewing images in your application, the Pillow library has made it quite simple. You can display your image using the built-in method we just touched on, show(), but sometimes you might want to integrate the image display directly into a graphical user interface (GUI). This is where libraries like Tkinter come into play.
Using Tkinter, you can create a window and display the image within it. First, ensure you have Tkinter available in your Python installation, which usually comes with standard Python distributions. Here’s how you can display your image using Tkinter:
from tkinter import Tk, Label from PIL import ImageTk # Create a Tkinter window root = Tk() # Load the image image_path = 'path/to/your/image.jpg' image = Image.open(image_path) photo = ImageTk.PhotoImage(image) # Create a label to display the image label = Label(root, image=photo) label.pack() # Run the application root.mainloop()
This code initializes a Tkinter window, loads the image, and then creates a label widget to display that image. The ImageTk.PhotoImage() function converts the Pillow image into a format that Tkinter can understand. Finally, the mainloop() method starts the GUI event loop, allowing you to interact with the window.
One important thing to note is that the image must be kept in memory; if the reference to the image object is lost, the image will not display. It’s common to run into issues with images disappearing when you close the window or when the reference is out of scope.
Now that you can display images, let’s move on to a few common manipulations you’ll actually use. You might find yourself needing to resize images, rotate them, or apply filters. These operations are straightforward with the Pillow library. For instance, resizing an image can be done using the resize() method:
# Resize the image to 800x600 pixels new_size = (800, 600) resized_image = image.resize(new_size) # Display the resized image resized_image.show()
This snippet demonstrates how to resize an image to a specified width and height. The resize() method returns a new image object with the specified dimensions. Keep in mind that resizing can lead to distortion if the aspect ratio is not maintained, so it’s often a good idea to calculate the new dimensions based on the original aspect ratio.
Another frequently used operation is rotating an image. You can achieve this with the rotate() method, which allows you to specify the angle of rotation:
# Rotate the image by 90 degrees rotated_image = image.rotate(90) # Display the rotated image rotated_image.show()
In this example, the image is rotated 90 degrees clockwise. The rotate() method also accepts an optional parameter to specify whether to expand the output image to fit the new dimensions. By default, it will crop the image, which is often not desired.
As you continue to manipulate your images, you might want to apply filters for effects. Pillow comes with a few built-in filters, such as Gaussian blur:
from PIL import ImageFilter # Apply a Gaussian blur filter blurred_image = image.filter(ImageFilter.GaussianBlur(radius=5)) # Show the blurred image blurred_image.show()
This code snippet applies a Gaussian blur with a specified radius to the image. The filter() method takes a filter object as an argument, allowing for a range of effects to be applied to your images. There are several other filters provided by Pillow, including sharpen, edge enhance, and more, which you can explore in the documentation.
With these foundational operations under your belt, you’ll find yourself well-equipped to handle many common image manipulation tasks in your Python applications. As you delve deeper, you’ll discover additional functionalities offered by Pillow that can enhance your image processing capabilities…
A few common manipulations you’ll actually use
Beyond filters and rotations, another fundamental tool you’ll reach for constantly is cropping. You might need to isolate a face, create a thumbnail, or just trim away the boring parts of a photograph. Pillow handles this with a simple crop() method that takes a box tuple.
The box is defined by a 4-element tuple specifying the left, upper, right, and lower pixel coordinates. Remember that Pillow’s coordinate system has (0,0) in the top-left corner. So, to cut out a 250×250 pixel area starting from the coordinate (100, 100), you would do this:
# Define the box to crop # The format is (left, upper, right, lower) box = (100, 100, 350, 350) cropped_image = image.crop(box) cropped_image.show()
Another incredibly common task is converting an image to grayscale. This isn’t just for artistic effect; it’s often a required preprocessing step for many computer vision algorithms and can significantly reduce the amount of data you have to process. The convert() method is what you need, and the mode 'L' stands for luminance.
# Convert the image to grayscale
grayscale_image = image.convert('L')
grayscale_image.show()
But what if your image is just a little too dark, or the contrast is flat? You don’t need to fire up Photoshop for simple adjustments. The ImageEnhance module in Pillow gives you programmatic control over these properties. You create an “enhancer” object for your image, then call its enhance() method with a floating-point factor. A factor of 1.0 gives you the original image back, while values greater than 1.0 increase the effect and values less than 1.0 decrease it.
from PIL import ImageEnhance # Adjust brightness enhancer = ImageEnhance.Brightness(image) brighter_image = enhancer.enhance(1.5) # Increase brightness by 50% brighter_image.show() # Adjust contrast enhancer = ImageEnhance.Contrast(image) contrasted_image = enhancer.enhance(2.0) # Double the contrast contrasted_image.show()
Finally, let’s look at compositing, or pasting one image onto another. This is perfect for adding a watermark or creating simple collages. The paste() method takes the image to be pasted and a coordinate for the top-left corner where it should be placed. If the image you’re pasting has an alpha channel (transparency), you can use that same image as the third argument, the mask, to ensure transparent areas are handled correctly.
# Assume 'logo_image' is a PNG with a transparent background # Create a copy to avoid modifying the original image image_with_logo = image.copy() # Position for the logo (e.g., bottom right corner) position = (image_with_logo.width - logo_image.width, image_with_logo.height - logo_image.height) # Paste the logo using its alpha channel as a mask image_with_logo.paste(logo_image, position, logo_image) image_with_logo.show()
