Of course. This is a very common question, especially for those working with older systems or legacy projects.
Here’s a complete guide to using Pillow (the modern replacement for PIL) with Python 3.4.
The Short Answer
The original PIL (Python Imaging Library) is not compatible with Python 3. It was last updated in 2009 and only supported Python 2.
The direct, drop-in replacement for PIL is Pillow. Pillow is a "friendly fork" of PIL that is actively maintained and supports modern Python versions, including Python 3.4.
The Detailed Guide: Installing and Using Pillow on Python 3.4
Step 1: Install Pillow
You will use pip, Python's package installer, to add Pillow to your Python 3.4 environment.
-
Open your Command Prompt or Terminal.
-
Run the installation command:
pip install Pillow
Important Note: If you have both Python 2 and Python 3 installed, you might need to use
pip3to ensure the package is installed for the correct Python version.pip3 install Pillow
After a few moments, the installation will complete. You will now have the PIL module available to import in your Python scripts, even though you installed Pillow.
Step 2: Verify the Installation
You can quickly check if Pillow was installed correctly by running a simple Python command.
- Open your command prompt.
- Start the Python 3.4 interpreter:
python
- In the Python interpreter, try to import the
PILmodule and print its version:>>> from PIL import Image >>> print(Image.__version__)
You should see a version number printed, something like
8.1or higher. If you see no errors and a version number is printed, your installation was successful!
Step 3: Basic Usage Example
Here is a simple script that demonstrates opening an image, resizing it, and saving it as a new file. Save this code as process_image.py.
# process_image.py
from PIL import Image
try:
# 1. Open an image file
# Make sure you have an image named 'input.jpg' in the same directory
img = Image.open('input.jpg')
# Print some basic information about the image
print(f"Image format: {img.format}")
print(f"Image size: {img.size}")
print(f"Image mode: {img.mode}")
# 2. Resize the image
# The 'thumbnail' method modifies the image in place to a size that fits within the given box.
# It preserves the aspect ratio.
size = (128, 128)
img.thumbnail(size)
# 3. Save the resized image
# You can save it in a different format, e.g., as a PNG
img.save('output.png')
print("\nImage successfully processed and saved as 'output.png'")
except FileNotFoundError:
print("Error: The file 'input.jpg' was not found. Please make sure it's in the same directory as the script.")
except Exception as e:
print(f"An error occurred: {e}")
To run this script:
- Place an image file (e.g.,
my_photo.jpg) in the same folder as yourprocess_image.pyfile. - Rename
my_photo.jpgtoinput.jpgor change the filename in the script. - Run from your terminal:
python process_image.py
- You will see a new file named
output.pngin the folder, which is a resized version of your original image.
Key Differences and Considerations for Python 3.4
While Pillow is a direct replacement for PIL, there are a few things to keep in mind, especially with an older version like Python 3.4.
-
Version of Pillow Matters: The version of Pillow you install will be compatible with Python 3.4, but it will be an older version of Pillow itself. Newer versions of Pillow may drop support for Python 3.4. The
pip installcommand will automatically find the latest compatible version for you. -
ImageTkfor GUIs: If you are using a GUI library like Tkinter to display images, you will need to use theImageTksubmodule.from PIL import Image, ImageTk import tkinter as tk root = tk.Tk() # ... your tkinter setup code ... # Load the image pil_image = Image.open("image.png") # Convert it to a Tkinter-compatible PhotoImage object tk_image = ImageTk.PhotoImage(pil_image) # Use it in a Label widget label = tk.Label(root, image=tk_image) label.pack() root.mainloop() -
ImageFont: For drawing text on images, Pillow uses its own font format. You may need to convert or locate.ttf(TrueType) or.otf(OpenType) font files for your system.from PIL import Image, ImageDraw, ImageFont img = Image.new('RGB', (200, 50), color = 'white') d = ImageDraw.Draw(img) # You might need to provide the full path to a .ttf file try: # This is a common default font path on Windows, but may not exist font = ImageFont.truetype("arial.ttf", 20) except IOError: # Fallback to a default basic font if the specific one isn't found font = ImageFont.load_default() d.text((10,10), "Hello PIL!", fill=(0,0,0), font=font) img.save('text_image.png')
Summary
| Task | Original PIL (Python 2) | Pillow (Python 3) |
|---|---|---|
| Installation | easy_install PIL |
pip install Pillow |
| Import Statement | from PIL import Image |
from PIL import Image |
| Status | Abandoned | Actively Maintained |
| Compatibility | Python 2 only | Python 3.4+ |
Conclusion: For Python 3.4, Pillow is the only correct and viable choice. It provides all the functionality of the original PIL with full support for modern Python features. Just run pip install Pillow and you're ready to go.
