Pillow Installation
Before installing Pillow, please make sure:
- 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 required.
Python Version
Pillow requires Python 3.7 or higher.
You can check your Python version with the following command:
python --version # 或 python3 --version
pip Tool
pip is Python's package management tool, usually installed alongside Python.
Check if pip is available:
pip --version # 或 pip3 --version
If pip is not installed, you can refer to theofficial pip installation guideto install it.
Install Pillow
Basic Installation Method
The simplest way is to install using pip:
pip install pillow
Or specify Python 3 explicitly:
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 # 激活虚拟环境 # Windows: pillow_env\Scripts\activate # 安装 Pillow pip install Pillow
Install from Source Code
For advanced users, you can install from source code:
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 Installation
After installation, you can verify whether Pillow was installed successfully using the following methods:
Method 1: Python Interactive Environment
Example
print(PIL.__version__)
Method 2: Command Line Check
python3 -c "import PIL; print(PIL.__version__)"
If installation succeeds, the version number of Pillow will be displayed.
Troubleshooting Common Issues
1. Installation Failure
Possible causes:
- Missing dependency libraries (such as zlib, libjpeg, etc.)
- Permission issues
Solutions:
Linux/macOS users may need to install system dependencies first:
Example
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 encounterImportError: cannot import name '_imaging' from 'PIL'and other errors, 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
Your First Pillow Program
After successfully installing Pillow, you can start exploring its powerful image processing features:
Example
# Open image
img = Image.open("example.jpg")
# Display image
img.show()
# Convert image format
img.save("example.png")
# Resize
small_img = img.resize((100, 100))
small_img.save("small_example.jpg")
Code Explanation:
Import module
from PIL import Image→ Import Pillow's Image module
Open image
img = Image.open("example.jpg")→ Load the image file at the specified pathSupported formats: JPG/PNG/GIF/BMP, etc.
Display image
img.show()→ Open with the system's default image viewerFor debugging only, not recommended for production environments
Save image
img.save("example.png")→ Save as the specified formatThe format is determined by the file extension (e.g., .jpg/.png)
Resize
small_img = img.resize((100,100))→ Force resize to 100×100 pixelsNote: This will change the original image's aspect ratio.
Save new image
small_img.save("small_example.jpg")→ Save the resized imageYou can add the quality parameter to control JPEG quality.