杰瑞科技汇

Python安装PIL库总失败?

Of course! Here’s a comprehensive guide on how to install PIL and its modern, actively maintained replacement, Pillow.

The Short Answer (TL;DR)

The original PIL (Python Imaging Library) is no longer maintained. You should always install its fork, Pillow, instead.

To install Pillow, run this command in your terminal or command prompt:

pip install Pillow

Detailed Explanation: PIL vs. Pillow

This is the most important part to understand.

What is PIL (Python Imaging Library)?

  • PIL was the original, go-to library for image processing in Python.
  • It was developed in the 1990s but has been unmaintained since 2009.
  • It does not support modern Python versions (like Python 3) or image formats (like WebP).

What is Pillow?

  • Pillow is a friendly fork of PIL. It was created to keep the library alive and add modern features.
  • It is the de facto standard for image manipulation in Python today.
  • It is actively maintained, supports Python 2.7, 3.x, and many modern image formats.
  • When you import PIL in your code, you are actually using the Pillow library. It maintains backward compatibility with the original PIL's API.

Conclusion: You should always use Pillow. It's the modern, supported, and correct choice.


How to Install Pillow

Step 1: Open Your Terminal

  • Windows: Open Command Prompt or PowerShell.
  • macOS/Linux: Open Terminal.

Step 2: Run the Installation Command

Use pip (Python's package installer) to install Pillow.

pip install Pillow

Note: If you have both Python 2 and Python 3 installed, you might need to use pip3 to ensure it's installed for Python 3:

pip3 install Pillow

Step 3: Verify the Installation

You can verify that Pillow was installed correctly in two ways:

Method 1: Check the Version Run this command to see the installed version of Pillow:

python -c "import PIL; print(PIL.__version__)"

or for Python 3:

python3 -c "import PIL; print(PIL.__version__)"

You should see a version number printed (e.g., 0.1).

Method 2: Run a Quick Test Create a simple Python script (e.g., test_pillow.py) with the following code:

from PIL import Image
try:
    # Try to open an image (you can use any image file)
    # For this example, we'll create a new one.
    img = Image.new('RGB', (100, 100), color = 'red')
    # Save the image
    img.save('test_image.png')
    print("Pillow is working correctly!")
    print("A file named 'test_image.png' has been created.")
except ImportError:
    print("Pillow is not installed. Please run 'pip install Pillow'.")

Run the script from your terminal:

python test_pillow.py

If it works, you'll see the success message, and a red test_image.png file will be created in the same directory.


Common Issues and Solutions

Issue 1: pip is not recognized

This usually means Python's Scripts directory is not in your system's PATH environment variable.

  • Solution: Re-run the Python installer and make sure to check the box that says "Add Python to PATH". After that, close and reopen your terminal, and try the pip install command again.

Issue 2: Permission Denied on macOS or Linux

You might get an error like Permission denied: '/usr/local/lib/python3.x/site-packages/PIL'.

  • Solution: Use pip with the --user flag to install the package only for your user account, which doesn't require administrator privileges.
    pip install --user Pillow

Issue 3: Building from Source Fails

On some systems (especially Linux), Pillow might require certain system-level libraries to be installed before it can be built (e.g., libjpeg, libpng, libtiff, libwebp, zlib, libopenjp2).

  • Solution: Install the required development packages using your system's package manager.
    • On Debian/Ubuntu:
      sudo apt-get update
      sudo apt-get install python3-dev libjpeg-dev zlib1g-dev libpng-dev
    • On Fedora/CentOS/RHEL:
      sudo dnf install python3-devel libjpeg-turbo-devel zlib-devel libpng-devel
    • On macOS (using Homebrew):
      brew install libjpeg libpng libtiff libwebp

      After installing these, try running pip install Pillow again.

分享:
扫描分享到社交APP
上一篇
下一篇