Last modified: Apr 12, 2025 By Alexander Williams
Python Display Images Guide
Displaying images in Python is easy with the right libraries. This guide covers three popular methods.
Using PIL to Display Images
The Python Imaging Library (PIL) is great for basic image operations. First, install it with pip install pillow
.
from PIL import Image
# Open an image file
img = Image.open('example.jpg')
# Display the image
img.show()
This opens the image in your default viewer. For more PIL features, see our Python PIL Image Handling Guide.
Displaying Images with Matplotlib
Matplotlib is perfect when working with data visualization. Install it with pip install matplotlib
.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# Read the image
img = mpimg.imread('example.jpg')
# Display the image
plt.imshow(img)
plt.axis('off') # Hide axes
plt.show()
This method gives you more control over the display. You can add titles or adjust the plot size.
Using OpenCV for Image Display
OpenCV is powerful for computer vision tasks. Install it with pip install opencv-python
.
import cv2
# Read the image
img = cv2.imread('example.jpg')
# Display the image
cv2.imshow('Image Window', img)
# Wait for key press to close
cv2.waitKey(0)
cv2.destroyAllWindows()
OpenCV uses BGR color format by default. You might need to convert it to RGB for proper display.
Comparing the Methods
PIL is simplest for basic display. Matplotlib integrates well with plots. OpenCV offers advanced computer vision features.
For more on processing, see our Python Image Processing Guide.
Handling Common Issues
If images don't display, check:
1. File path is correct
2. Image format is supported
3. Library is properly installed
For metadata issues, our Python Save Images with Metadata Guide can help.
Conclusion
Python offers multiple ways to display images. Choose PIL for simplicity, Matplotlib for integration, or OpenCV for advanced features.
Start with the method that fits your needs and explore others as you progress.