Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pillow Installation

Pillow Installation

Before installing Pillow, please ensure:

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

print

(

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:

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:**

  1. Import the module

  2. from PIL import Image → Import Pillow's Image module

  3. -

    from PIL import Image → Import Pillow's Image module

    1. Open image

    2. img = Image.open("example.jpg") → Load the image file at the specified path

    3. Supported formats: JPG/PNG/GIF/BMP, etc.

    4. -

      img = Image.open("example.jpg") → Load the image file at the specified path

      -

      Supported formats: JPG/PNG/GIF/BMP, etc.

      1. Displays an image

      2. img.show() → Open with the system's default image viewer

      3. For debugging only, not recommended in production

      4. -

        img.show() → Open with the system's default image viewer

        -

        For debugging only, not recommended in production

        1. Save the image

        2. img.save("example.png") → Save as the specified format

        3. The format is determined by the file extension (e.g., .jpg/.png)

        4. -

          img.save("example.png") → Save as the specified format

          -

          The format is determined by the file extension (e.g., .jpg/.png)

          1. Resize

          2. small_img = img.resize((100,100)) → Force resize to 100x100 pixels

          3. Note: This changes the original image's aspect ratio

          4. -

            small_img = img.resize((100,100)) → Force resize to 100x100 pixels

            -

            Note: This changes the original image's aspect ratio

            1. Save the new image

            2. small_img.save("small_example.jpg") → Save the resized image

            3. You can add the quality parameter to control JPEG quality

            4. -

              small_img.save("small_example.jpg") → Save the resized image

              -

              You can add the quality parameter to control JPEG quality