Python pillow
Swarooprani Manoor
Mentored 2K+ Students | 20 Years of Experience in Education & Mentorship | Passionate About Python Coding
Welcome to the world of Python Pillow, the fun way to play with pictures in Python!
The Pillow library in Python is a powerful library for image processing tasks. It allows you to open, manipulate, and save many different image file formats. With Pillow, you can perform various operations on images, such as resizing, cropping, rotating, applying filters, and much more.
But why is the name, pillow?
The library is named "Pillow" as an homage to the Python Imaging Library (PIL), which was the original library for image processing in Python. PIL provided basic image processing capabilities but became outdated and less maintained over time. "Pillow" was created as a fork of PIL to address its limitations and continue its development.
The name "Pillow" was chosen as it suggests a comfortable and supportive framework for working with images in Python. It also retains the association with the original PIL library while indicating improvements and modernization.
So, essentially, Pillow is the "new and improved" version of PIL, maintaining compatibility with the original while adding new features and improvements.
let's start with some basic tasks and gradually explore further features of Pillow. We'll begin with loading, displaying, and saving images, and then move on to resizing, cropping, rotating, and applying filters.
1. Loading and Displaying an Image
from PIL import Image
# Open an image file
image = Image.open("example.jpg")
# Display the image
image.show()
2. Saving an Image
# Save the image to a new file
image.save("example_copy.jpg")
3. Resizing an Image
领英推荐
# Resize the image to 200x200 pixels
resized_image = image.resize((200, 200))
# Display the resized image
resized_image.show()
4. Cropping an Image
# Crop a 100x100 region from the image (left, upper, right, lower)
cropped_image = image.crop((100, 100, 300, 300))
# Display the cropped image
cropped_image.show()
5. Rotating an Image
# Rotate the image by 90 degrees clockwise
rotated_image = image.rotate(90)
# Display the rotated image
rotated_image.show()
6. Applying Filters
from PIL import ImageFilter
# Apply a blur filter
blurred_image = image.filter(ImageFilter.BLUR)
# Display the blurred image
blurred_image.show()
7. Adding Text to an Image
from PIL import ImageDraw, ImageFont
# Create a drawing object
draw = ImageDraw.Draw(image)
# Specify font and text to draw
font = ImageFont.truetype("arial.ttf", 40)
text = "Hello, Pillow!"
# Draw the text on the image
draw.text((10, 10), text, fill="white", font=font)
# Display the image with text
image.show()
These are some basic operations you can perform with Pillow. You can combine these techniques to achieve more complex image processing tasks.
So go ahead, take a nap, snap some pics, and Pillow will be there to turn them into pure magic!
Happy imaging!