Pillow Python Image Library
Pillow is a library for Python that allows you to manipulate images. It is a fork of the Python Imaging Library (PIL), but has been updated to support Python 3 and add new features.
Here is a basic tutorial on how to use Pillow to load and display an image:
# Import the Image module from the PIL package
from PIL import Image
# Open an image file
with Image.open('image.jpg') as im:
# Display the image
im.show()
You can also use Pillow to perform other operations on images, such as resizing, cropping, and converting between image formats. For example:
# Import the Image module from the PIL package
from PIL import Image
# Open an image file
with Image.open('image.jpg') as im:
# Resize the image
im = im.resize((200, 200))
# Crop the image
im = im.crop((0, 0, 100, 100))
# Convert the image to grayscale
im = im.convert('L')
# Save the image
im.save('image_modified.jpg')
Here are some more examples of using Pillow to manipulate images:
Rotating an image
To rotate an image, you can use the rotate()
method, which takes an angle in degrees as an argument. Positive angles rotate the image clockwise, and negative angles rotate the image counterclockwise.
# Import the Image module from the PIL package
from PIL import Image
# Open an image file
with Image.open('image.jpg') as im:
# Rotate the image by 45 degrees
im = im.rotate(45)
# Save the rotated image
im.save('image_rotated.jpg')
Adding text to an image
To add text to an image, you can use the ImageDraw
module from Pillow. This module allows you to draw text, lines, and other shapes on an image.
# Import the Image and ImageDraw modules from the PIL package
from PIL import Image, ImageDraw
# Open an image file
with Image.open('image.jpg') as im:
# Create an ImageDraw object
draw = ImageDraw.Draw(im)
# Draw a string at the specified coordinates
draw.text((10, 10), 'Hello, World!', fill=(255, 255, 255))
# Save the image with the text added
im.save('image_with_text.jpg')
Converting between image formats
To convert an image to a different format, you can use the save()
method and specify the desired format as the file extension.
# Import the Image module from the PIL package
from PIL import Image
# Open an image file
with Image.open('image.jpg') as im:
# Convert the image to PNG format and save it
im.save('image.png')
You can also use the convert()
method to convert the image to a different mode, such as grayscale or RGB.
# Import the Image module from the PIL package
from PIL import Image
# Open an image file
with Image.open('image.jpg') as im:
# Convert the image to grayscale
im = im.convert('L')
# Save the grayscale image
im.save('image_grayscale.jpg')
Merging images
To merge two images together, you can use the alpha_composite()
method from the Image
module. This method takes another image as an argument and combines the two images using alpha compositing.
# Import the Image module from the PIL package
from PIL import Image
# Open the first image
with Image.open('image1.jpg') as im1:
# Open the second image
with Image.open('image2.jpg') as im2:
# Merge the two images using alpha compositing
im = Image.alpha_composite(im1, im2)
# Save the merged image
im.save('image_merged.jpg')
Applying image filters
Pillow provides a number of built-in image filters that you can apply to an image using the filter()
method. For example, to apply the BLUR
filter to an image:
# Import the Image and ImageFilter modules from the PIL package
from PIL import Image, ImageFilter
# Open an image file
with Image.open('image.jpg') as im:
# Apply the BLUR filter to the image
im = im.filter(ImageFilter.BLUR)
# Save the filtered image
im.save('image_blurred.jpg')
Manipulating image pixels
You can also manipulate the pixels of an image directly using the load()
method and the putpixel()
method. The load()
method loads the pixel data of the image into memory, and the putpixel()
method allows you to set the value of a specific pixel.
# Import the Image module from the PIL package
from PIL import Image
# Open an image file
with Image.open('image.jpg') as im:
# Load the pixel data
pixels = im.load()
# Set the value of a specific pixel
pixels[10, 10] = (255, 0, 0)
# Save the modified image
im.save('image_modified.jpg')
Drawing shapes on an image
To draw shapes on an image, you can use the draw_polygon()
method of the ImageDraw
module. This method takes a list of points as an argument, and connects the points to form a polygon. You can also specify the fill color of the polygon using the fill
argument.
# Import the Image and ImageDraw modules from the PIL package
from PIL import Image, ImageDraw
# Open an image file
with Image.open('image.jpg') as im:
# Create an ImageDraw object
draw = ImageDraw.Draw(im)
# Define the points of the polygon
points = [(10, 10), (20, 20), (30, 10)]
# Draw the polygon on the image
draw.polygon(points, fill=(255, 0, 0))
# Save the image with the polygon added
im.save('image_with_polygon.jpg')
You can also use the rectangle()
method to draw a rectangle on the image, and the line()
method to draw a line.
# Import the Image and ImageDraw modules from the PIL package
from PIL import Image, ImageDraw
# Open an image file
with Image.open('image.jpg') as im:
# Create an ImageDraw object
draw = ImageDraw.Draw(im)
# Draw a rectangle on the image
draw.rectangle((10, 10, 30, 30), fill=(255, 0, 0))
# Draw a line on the image
draw.line((40, 40, 60, 60), fill=(0, 255, 0), width=5)
# Save the image with the shapes added
im.save('image_with_shapes.jpg')
Converting an image to ASCII art
To convert an image to ASCII art, you can use the getdata()
method to access the pixel data of the image, and then iterate over the pixels and map each pixel value to a corresponding ASCII character.
# Import the Image module from the PIL package
from PIL import Image
# Open an image file
with Image.open('image.jpg') as im:
# Convert the image to grayscale
im = im.convert('L')
# Get the pixel data
pixels = list(im.getdata())
# Map each pixel value to a corresponding ASCII character
chars = '@B%8WM#*oahkbdpwmwmz&O'
ascii_image = '\n'.join([ ''.join([chars[p // 26] for p in row]) for row in pixels ])
print(ascii_image)
Applying image transformations
To apply transformations to an image, such as scaling, rotating, or shearing, you can use the affine()
method of the Image
module. This method takes a 3x3 transformation matrix as an argument, and applies the transformation to the image.
# Import the Image module from the PIL package
from PIL import Image
# Open an image file
with Image.open('image.jpg') as im:
# Scale the image by a factor of 2
scale_matrix = (2, 0, 0, 0, 2, 0)
im = im.transform(im.size, Image.AFFINE, scale_matrix)
# Save the transformed image
im.save('image_scaled.jpg')
# Import the Image module from the PIL package
from PIL import Image
# Open an image file
with Image.open('image.jpg') as im:
# Rotate the image by 45 degrees
rotate_matrix = (0.7071067811865475, 0.7071067811865475, -0.7071067811865475, 0.7071067811865475)
im = im.transform(im.size, Image.AFFINE, rotate_matrix)
# Save the transformed image
im.save('image_rotated.jpg')
# Import the Image module from the PIL package
from PIL import Image
# Open an image file
with Image.open('image.jpg') as im:
# Shear the image horizontally
shear_matrix = (1, 0, 0, 0.5, 1, 0)
im = im.transform(im.size, Image.AFFINE, shear_matrix)
# Save the transformed image
im.save('image_sheared.jpg')
Manipulating image metadata
To access and modify the metadata of an image, such as the EXIF data or the image tags, you can use the _getexif()
and _getexif()
methods of the Image
module.
# Import the Image module from the PIL package
from PIL import Image
# Open an image file
with Image.open('image.jpg') as im:
# Get the EXIF data
exif_data = im._getexif()
# Print the EXIF data
print(exif_data)
# Import the Image module from the PIL package
from PIL import Image
# Open an image file
with Image.open('image.jpg') as im:
# Set the image tag
im.tag[0x010F] = 'Example Tag'
# Save the modified image
im.save('image_modified.jpg')
I hope these examples give you some ideas for more advanced image manipulation using Pillow.
Here are some pros and cons of using Pillow for image manipulation in Python:
Pros
Pillow is an open-source library, which means it is freely available and can be modified and distributed by anyone.
Pillow is built on top of the Python Imaging Library (PIL), which has been around for many years and has a large user base. This means that Pillow is well-tested and has a lot of support and documentation available.
Pillow is compatible with Python 3, which makes it a good choice for modern Python projects.
Pillow provides a wide range of image manipulation features, including loading and saving images, resizing, cropping, converting between image formats, and applying image filters.
Cons
Pillow is a large library, which means it may take longer to install and import compared to smaller libraries.
Pillow is built on top of the Python Imaging Library (PIL), which has not been actively maintained since 2011. This means that some features may be outdated or not fully supported.
Pillow is a high-level library, which means that it may not provide as much control over low-level image manipulation tasks, such as directly accessing and modifying pixel data.
Overall, Pillow is a powerful and widely-used library for image manipulation in Python. It is a good choice for many projects, but you may want to consider other libraries if you need more control over low-level image manipulation tasks or if you are working on a project with strict size or performance constraints.You can find more information and examples in the Pillow documentation: https://pillow.readthedocs.io/en/stable/
Leave a Comment