Pillow Installation
Before installing Pillow, please ensure:
- Python 3.7 or higher (latest version recommended)
- pip package manager (usually installed with Python)
- Depending on the platform, some additional system libraries and development tools may be needed
Python version
Pillow requires Python 3.7 or higher.
You can check the Python version with the following command:
python --version
# 或
python3 --version
the pip tool
pip is Python's package management tool and is usually installed with Python.
Check whether pip is available:
pip --version
# 或
pip3 --version
If pip is not installed, you can refer to the official pip installation guide to install it.
Install Pillow
Basic installation method
The simplest method is to install with pip:
pip install pillow
Or specify explicitly for Python 3:
pip3 install pillow
If you use the Anaconda Python distribution:
conda install -c anaconda pillow
Install a specific version
If you need to install a specific version of Pillow:
pip install pillow==9.0.0
Install in a virtual environment
It is recommended to install Pillow in a virtual environment to avoid dependency conflicts:
# 创建虚拟环境
python -m venv pillow_env
# Activate virtual environment
# Windows:
pillow_env\Scripts\activate
# Install Pillow
pip install Pillow
Install from Source
For advanced users, you can install from source:
Download the source code:
git clone https://github.com/python-pillow/Pillow.git
cd Pillow
Install dependencies:
pip install -r requirements.txt
Compile and install:
python setup.py install
Verify the installation
After installation, you can verify that Pillow was installed successfully in the following ways:
Method 1: Python interactive environment
Example
import
PIL
(
PIL.version
)
Method 2: Command-line check
python3 -c "import PIL; print(PIL.__version__)"
If the installation is successful, the Pillow version number will be displayed.
Common Problem Solutions
1. Installation failure
Possible reasons:
- Missing dependency libraries (such as zlib, libjpeg, etc.)
- Permission issues
Solutions:
Linux/macOS users may need to install system dependencies first:
Example
Ubuntu/Debian
sudo
apt-get install
python3-dev python3-setuptools
Fedora
sudo
dnf
install
python3-devel
macOS (using Homebrew)
brew
install
zlib jpeg
Use the --user parameter to avoid permission issues:
pip install --user pillow
2. Import error
If you encounter errors such as ImportError: cannot import name '_imaging' from 'PIL' try:
pip uninstall pillow
pip install --no-cache-dir pillow
3. Performance issues
For large-scale image processing, consider installing an optimized version:
pip install pillow-simd
First Pillow Program
After successfully installing Pillow, you can begin to explore its powerful image processing features:
Example
from
PIL
import
Image
Open image
img
=
Image.
open
(
"example.jpg"
)
Displays an image
img.
show
(
)
Convert image format
img.
save
(
"example.png"
)
resize
small_img
=
img.
resize
(
(
100
,
100
)
)
small_img.
save
(
"small_example.jpg"
)
** Code Analysis:**
-
Import the module
from PIL import Image → Import Pillow's Image module
from PIL import Image → Import Pillow's Image module
-
Open image
img = Image.open("example.jpg") → Load the image file at the specified path
Supported formats: JPG/PNG/GIF/BMP, etc.
img = Image.open("example.jpg") → Load the image file at the specified path
Supported formats: JPG/PNG/GIF/BMP, etc.
-
Displays an image
img.show() → Open with the system's default image viewer
For debugging only, not recommended in production
img.show() → Open with the system's default image viewer
For debugging only, not recommended in production
-
Save the image
img.save("example.png") → Save as the specified format
The format is determined by the file extension (e.g., .jpg/.png)
img.save("example.png") → Save as the specified format
The format is determined by the file extension (e.g., .jpg/.png)
-
Resize
small_img = img.resize((100,100)) → Force resize to 100x100 pixels
Note: This changes the original image's aspect ratio
small_img = img.resize((100,100)) → Force resize to 100x100 pixels
Note: This changes the original image's aspect ratio
-
Save the new image
small_img.save("small_example.jpg") → Save the resized image
You can add the quality parameter to control JPEG quality
small_img.save("small_example.jpg") → Save the resized image
You can add the quality parameter to control JPEG quality