Last modified: Apr 12, 2025 By Alexander Williams

Python Open Digital Images Guide

Python makes it easy to work with digital images. You can open, edit, and save images using various libraries. This guide covers the basics.

Why Use Python for Images?

Python is popular for image processing. It has simple syntax and powerful libraries. You can automate tasks or analyze images.

Common uses include photo editing, computer vision, and machine learning. Python handles these tasks efficiently.

Installing Required Libraries

First, install the necessary libraries. The most common ones are Pillow and OpenCV. Use pip for installation.


# Install Pillow
pip install pillow

# Install OpenCV
pip install opencv-python

These libraries provide tools for image manipulation. Pillow is simpler. OpenCV offers advanced features.

Opening Images with Pillow

Pillow is a fork of PIL (Python Imaging Library). It's easy to use for basic tasks. Here's how to open an image.


from PIL import Image

# Open an image file
img = Image.open('example.jpg')

# Show the image
img.show()

The Image.open() method reads the image file. img.show() displays it using the default viewer.

For more on Pillow, see our Python Image Libraries Guide.

Opening Images with OpenCV

OpenCV is better for complex tasks. It's widely used in computer vision. Here's how to open an image with OpenCV.


import cv2

# Read an image
img = cv2.imread('example.jpg')

# Display the image
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

The cv2.imread() function loads the image. cv2.imshow() displays it in a window.

OpenCV uses BGR color format by default. You may need to convert it to RGB for some applications.

Checking Image Properties

You can check image properties like size and format. This helps in processing.


from PIL import Image

img = Image.open('example.jpg')

# Get image size
print(img.size)

# Get image format
print(img.format)


(800, 600)
JPEG

The size property returns width and height. format shows the image type.

Saving Images in Different Formats

You can save images in various formats. Python makes conversion easy.


from PIL import Image

img = Image.open('example.jpg')

# Save as PNG
img.save('example.png')

The save() method writes the image to a file. It automatically converts based on the extension.

For more on saving images, see our Python Pygame Image Save Guide.

Basic Image Manipulation

Python allows basic edits like resizing and rotating. These are common tasks.


from PIL import Image

img = Image.open('example.jpg')

# Resize image
resized_img = img.resize((400, 300))

# Rotate image
rotated_img = img.rotate(90)

# Save edited images
resized_img.save('resized.jpg')
rotated_img.save('rotated.jpg')

The resize() method changes dimensions. rotate() turns the image by degrees.

Accessing Pixel Data

You can access and modify pixel values. This is useful for custom processing.


from PIL import Image

img = Image.open('example.jpg')

# Get pixel at (x, y)
pixel = img.getpixel((100, 50))

print(pixel)


(255, 0, 0)

The getpixel() method returns RGB values. For more details, see our Python Get Image Pixels Guide.

Conclusion

Python offers powerful tools for working with digital images. Libraries like Pillow and OpenCV simplify the process.

You can open, edit, and save images with just a few lines of code. This makes Python ideal for image processing tasks.

Start experimenting with these examples. Explore more advanced features as you progress.