SoFunction
Updated on 2025-04-08

Detailed explanation of common functions of Pillow in python

Common features of Pillow

Pillow is a powerful image processing library that provides rich features to process and manipulate images. Here are some commonly used functions and their sample code:

1. Open and save images

Pillow can easily open and save image files in various formats.

Sample code

from PIL import Image
# Open the imageimg = ("")
# Show image()
# Save the image("")

2. Crop the image

Crop a specific area of ​​the image.

Sample code

Python Copy

from PIL import Image
# Open the imageimg = ("")
# Crop the image (top left corner coordinates, bottom right corner coordinates)cropped_img = ((50, 50, 200, 200))
# Show cropped imagecropped_img.show()

3. Resize the image

Resize the image.

Sample code

from PIL import Image
# Open the imageimg = ("")
# Resize imageresized_img = ((300, 300))
# Show resized imagesresized_img.show()

4. Rotate the image

Rotate the image.

Sample code

from PIL import Image
# Open the imageimg = ("")
# Rotate the imagerotated_img = (90)
# Show the rotated imagerotated_img.show()

5. Flip the image

Flip the image horizontally or vertically.

Sample code

from PIL import Image
# Open the imageimg = ("")
# Horizontal flipflipped_img = (Image.FLIP_LEFT_RIGHT)
# Show the flipped imageflipped_img.show()

6. Image filtering

Apply various filters, such as blur, sharpening, etc.

Sample code

from PIL import Image, ImageFilter
# Open the imageimg = ("")
# Apply a blur filterblurred_img = ()
# Show blurred imageblurred_img.show()

7. Image synthesis

Synthesize multiple images together.

Sample code

from PIL import Image
# Open two imagesimg1 = ("")
img2 = ("")
# Resize the image to matchimg2 = ()
# Synthesize imagescombined_img = (img1, img2, alpha=0.5)
# Show the composite imagecombined_img.show()

8. Add text

Add text to the image.

Sample code

from PIL import Image, ImageDraw, ImageFont
# Open the imageimg = ("")
# Create a drawing objectdraw = (img)
# Specify font and sizefont = ("", 36)
# Add text to the image((50, 50), "Hello, Pillow!", font=font, fill=(255, 0, 0))
# Show image()

9. Adjust image mode

Convert the image to different modes, such as grayscale, RGB, etc.

Sample code

from PIL import Image
# Open the imageimg = ("")
# Convert to grayscale imagegray_img = ("L")
# Show grayscale imagesgray_img.show()

10. Create thumbnails

Generate thumbnails of the image.

Sample code

from PIL import Image
# Open the imageimg = ("")
# Create thumbnails((128, 128))
# Show thumbnails()

Pillow provides a wealth of features for processing and manipulating images. With the above example code, you can easily implement the image opening, saving, cropping, resizing, rotating, flipping, filtering, compositing, adding text, adjusting modes, and creating thumbnails. Hopefully these features and sample code will help you better use Pillow for image processing.

Here is the article about what are the commonly used functions of Pillow in python? That’s all for the article. For more related python Pillow function content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!