search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
Opening and Displaying an Image
Basic Image Manipulations
Applying Filters and Effects
Saving Images
Home Backend Development Python Tutorial How to perform image processing with the Pillow library in Python

How to perform image processing with the Pillow library in Python

Nov 21, 2025 am 02:51 AM

Pillow enables easy image processing in Python. First, use Image.open() to load an image and .show() to display it. Basic manipulations include resizing with .resize(), cropping via .crop(), rotating with .rotate(), and mode conversion using .convert(). Apply filters like BLUR or SHARPEN from ImageFilter module. Save results with .save(), specifying format and quality. Always handle exceptions and verify image modes during operations.

How to perform image processing with the Pillow library in Python

The Pillow library, a fork of the original PIL (Python Imaging Library), is one of the most popular tools for image processing in Python. It allows you to open, manipulate, and save various image file formats easily. Below are common operations you can perform using Pillow.

Opening and Displaying an Image

To begin working with an image, you first need to load it into your script.

  • Use Image.open() to load an image from a file path.
  • Call .show() to display the image using your system’s default viewer.

Example:

from PIL import Image
img = Image.open("example.jpg")
img.show()

Basic Image Manipulations

Pillow supports several basic transformations such as resizing, rotating, cropping, and converting image modes.

  • Resize: Use .resize((width, height)) to change dimensions.
  • Crop: Use .crop((left, top, right, bottom)) to extract a portion.
  • Rotate: Use .rotate(angle) to turn the image.
  • Convert mode: Use .convert("L") for grayscale or "RGB"/"RGBA" for color adjustments.

Example:

resized_img = img.resize((800, 600))
cropped_img = img.crop((100, 100, 400, 400))
rotated_img = img.rotate(45)

Applying Filters and Effects

Pillow provides built-in filters through the ImageFilter module.

  • Import ImageFilter to access blur, contour, sharpen, and more.
  • Apply filters using .filter(ImageFilter.BLUR).

Example:

from PIL import ImageFilter
blurred_img = img.filter(ImageFilter.BLUR)
sharpened_img = img.filter(ImageFilter.SHARPEN)

Saving Images

After manipulation, use .save() to write the image to a file.

  • Specify the output path and optionally the format (e.g., JPEG, PNG).
  • You can also adjust quality settings for JPEGs.

Example:

rotated_img.save("rotated_example.jpg", "JPEG", quality=90)

Basically, Pillow gives you straightforward methods to handle everyday image tasks. With just a few lines, you can transform images for web use, data preparation, or visual effects. Just remember to handle exceptions for missing files and check image modes when combining or filtering.

The above is the detailed content of How to perform image processing with the Pillow library in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solve the error of multidict build failure when installing Python package Solve the error of multidict build failure when installing Python package Mar 08, 2026 am 02:51 AM

When installing libraries that depend on multidict in Python, such as aiohttp or discord.py, users may encounter the error "ERROR: Could not build wheels for multidict". This is usually due to the lack of the necessary C/C compiler or build tools, preventing pip from successfully compiling multidict's C extension from source. This article will provide a series of solutions, including installing system build tools, managing Python versions, and using virtual environments, to help developers effectively solve this problem.

How to use the Python zip function_Parallel traversal of multiple sequences and dictionary construction How to use the Python zip function_Parallel traversal of multiple sequences and dictionary construction Mar 13, 2026 am 11:54 AM

The essence of zip is zipper pairing, which packs multiple iterable objects into tuples by position and does not automatically unpack the dictionary. When passing in a dictionary, its keys are traversed by default. You need to explicitly use the keys()/values()/items() view to correctly participate in parallel traversal.

How to find the sum of 5 numbers using Python's for loop How to find the sum of 5 numbers using Python's for loop Mar 10, 2026 pm 12:48 PM

This article explains in detail how to use a for loop to read 5 integers from user input and add them up, provide a concise and readable standard writing method, and compare efficient alternatives to built-in functions.

How to draw a histogram in Python_Multi-dimensional classification data comparison and stacked histogram color mapping implementation How to draw a histogram in Python_Multi-dimensional classification data comparison and stacked histogram color mapping implementation Mar 13, 2026 pm 12:18 PM

Multi-dimensional classification histograms need to manually calculate the x position and call plt.bar hierarchically; when stacking, bottom must be used to accumulate height, and xticks and ylim must be explicitly set (bottom=0); avoid mixing stacked=True and seaborn, and colors should be dynamically generated and strictly match the layer sequence.

How Python manages dependencies_Comparison between pip and poetry How Python manages dependencies_Comparison between pip and poetry Mar 12, 2026 pm 04:21 PM

pip is suitable for simple projects, which only install packages and do not isolate the environment; poetry is a modern tool that automatically manages dependencies, virtual environments and version locking. Use pip requirements.txt for small projects, and poetry is recommended for medium and large projects. The two cannot be mixed in the same project.

Python set intersection optimization_large data volume set operation skills Python set intersection optimization_large data volume set operation skills Mar 13, 2026 pm 12:36 PM

The key to optimizing Python set intersection performance is to use the minimum set as the left operand, avoid implicit conversion, block processing and cache incremental updates. Priority should be given to using min(...,key=len) to select the smallest set, disabling multi-parameter intersection(), using frozenset or bloom filters to reduce memory, and using lru_cache to cache results in high-frequency scenarios.

How to store sparse matrices in Python_Dictionary coordinate storage and use of scipy.sparse How to store sparse matrices in Python_Dictionary coordinate storage and use of scipy.sparse Mar 12, 2026 pm 05:48 PM

Use scipy.sparse.coo_matrix instead of a dictionary because the bottom layer uses row/col/data three-array to efficiently support operations; the structure needs to be deduplicated, converted to csr/csc and then calculated; save_npz is preferred for saving; operations such as slicing must use csr/csc format.

How to run a Python script_Detailed explanation of various ways to run a Python script and command line operations How to run a Python script_Detailed explanation of various ways to run a Python script and command line operations Apr 03, 2026 pm 01:51 PM

To run a Python script, make sure that Python is installed, the PATH configuration is correct, and the script has no syntax errors; confirm the interpreter path and version through which/where and --version; shebang only takes effect on Linux/macOS and requires chmod x; when reporting module errors, you need to check the working directory, sys.path, piplist, and running mode.

Related articles