What is the basic usage of Pillow in Python?

Nov 26, 2025

Leave a message

Pillow, a well - known Python Imaging Library (PIL), is a powerful tool for image processing. In this blog, as a Pillow supplier, I'll introduce the basic usage of Pillow in Python, which can be extremely useful for various applications such as web development, data analysis, and digital art creation.

Installation

Before we start using Pillow, we need to install it. You can use pip, the Python package installer, to install Pillow. Open your terminal or command prompt and run the following command:

pip install pillow

This command will download and install the latest version of Pillow in your Python environment.

Opening and Displaying an Image

One of the most fundamental operations in image processing is opening an existing image. Pillow makes this task very straightforward. Here is an example code:

from PIL import Image

# Open an image file
image = Image.open('example.jpg')

# Display the image
image.show()

In this code, we first import the Image module from the PIL library. Then we use the open method to open an image file named example.jpg. Finally, the show method is used to display the image. This method will open the image in your default image viewer.

Image Information

Once an image is opened, we can access various information about the image, such as its size, format, and mode.

from PIL import Image

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

# Get the size of the image
width, height = image.size

# Get the format of the image
image_format = image.format

# Get the mode of the image
image_mode = image.mode

print(f"Width: {width}, Height: {height}")
print(f"Format: {image_format}")
print(f"Mode: {image_mode}")

The size attribute returns a tuple containing the width and height of the image. The format attribute tells us the file format of the image (e.g., JPEG, PNG), and the mode attribute indicates the color mode of the image (e.g., RGB, RGBA).

Resizing an Image

Resizing an image is a common operation, especially when dealing with different screen sizes or optimizing images for web use. Pillow provides the resize method to achieve this.

from PIL import Image

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

# Resize the image
new_size = (300, 200)
resized_image = image.resize(new_size)

# Save the resized image
resized_image.save('resized_example.jpg')

In this code, we define a new size as a tuple (width, height) and use the resize method to create a new resized image. Then we save the resized image using the save method.

Memory Foam PillowHousehold Pillow

Cropping an Image

Cropping allows us to select a specific region of an image. The crop method in Pillow takes a tuple of four coordinates (left, top, right, bottom) to define the cropping area.

from PIL import Image

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

# Define the cropping area
crop_area = (100, 100, 300, 300)
cropped_image = image.crop(crop_area)

# Save the cropped image
cropped_image.save('cropped_example.jpg')

The coordinates in the crop_area tuple specify the left - most, top - most, right - most, and bottom - most positions of the cropping rectangle.

Rotating an Image

Rotating an image can add a new perspective or correct the orientation. Pillow provides the rotate method to rotate an image by a specified angle.

from PIL import Image

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

# Rotate the image by 90 degrees
rotated_image = image.rotate(90)

# Save the rotated image
rotated_image.save('rotated_example.jpg')

The rotate method takes an angle in degrees as an argument. Positive angles represent counter - clockwise rotation.

Color Manipulation

Pillow also allows us to manipulate the color of an image. For example, we can convert an image to grayscale.

from PIL import Image

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

# Convert the image to grayscale
grayscale_image = image.convert('L')

# Save the grayscale image
grayscale_image.save('grayscale_example.jpg')

The convert method is used to change the color mode of the image. The 'L' mode represents grayscale.

Working with Multiple Images

Sometimes, we need to combine multiple images. For example, we can create a collage by pasting one image onto another.

from PIL import Image

# Open two images
background = Image.open('background.jpg')
foreground = Image.open('foreground.jpg')

# Calculate the position to paste the foreground image
position = (100, 100)

# Paste the foreground image onto the background
background.paste(foreground, position)

# Save the combined image
background.save('combined.jpg')

The paste method takes the image to be pasted and the position as arguments.

Memory Foam Pillow and Household Pillow

As a Pillow supplier, we also offer a wide range of physical pillows, including Memory Foam Pillow and Household Pillow. These pillows are designed to provide comfort and support for a good night's sleep.

Conclusion

In conclusion, Pillow in Python is a versatile library that offers a wide range of image processing capabilities. From basic operations like opening and displaying images to more advanced tasks such as color manipulation and combining multiple images, Pillow can meet the needs of various applications. Whether you are a developer working on a web project or an artist creating digital art, Pillow can be a valuable tool in your toolkit.

If you are interested in our Python - related services or our physical pillow products, please feel free to contact us for procurement and further discussion. We are looking forward to serving you and helping you achieve your goals.

References

  • Pillow official documentation.
  • Python official documentation.

Send Inquiry