Last modified: Apr 21, 2025 By Alexander Williams
Python Simple Image Slideshow Guide
Creating an image slideshow in Python is easy. You can use libraries like PIL or OpenCV. This guide shows you how step by step.
Table Of Contents
Why Create a Slideshow in Python?
Python makes it simple to automate image tasks. A slideshow can display images in order. It's useful for presentations or galleries.
You might also want to check our Python Image Collages Guide for related techniques.
Prerequisites
Before starting, install these libraries:
pip install pillow opencv-python
The Pillow library handles image processing. OpenCV helps with display and timing.
Method 1: Using PIL and time
This simple method shows images one after another. It uses Python's built-in time module for delays.
from PIL import Image
import time
# List of image paths
images = ["image1.jpg", "image2.jpg", "image3.jpg"]
for image in images:
img = Image.open(image)
img.show()
time.sleep(2) # Show each image for 2 seconds
The Image.open()
method loads each image. img.show()
displays it in the default viewer.
Method 2: Using OpenCV
OpenCV provides more control. You can create a proper slideshow window.
import cv2
import time
images = ["image1.jpg", "image2.jpg", "image3.jpg"]
for image in images:
img = cv2.imread(image)
cv2.imshow('Slideshow', img)
cv2.waitKey(2000) # 2000ms = 2 seconds
cv2.destroyAllWindows()
cv2.imread()
loads the image. cv2.imshow()
displays it. The window closes after 2 seconds.
Adding Transitions
For better slideshows, add transitions between images. Here's a simple fade effect:
import cv2
import numpy as np
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
for alpha in np.linspace(0, 1, 30):
blended = cv2.addWeighted(img1, 1-alpha, img2, alpha, 0)
cv2.imshow('Slideshow', blended)
cv2.waitKey(50)
This code creates a smooth fade between two images. The addWeighted()
function blends them.
Saving Your Slideshow
You can save the slideshow as a video file. OpenCV makes this easy.
import cv2
images = ["image1.jpg", "image2.jpg", "image3.jpg"]
video = cv2.VideoWriter('slideshow.avi', cv2.VideoWriter_fourcc(*'DIVX'), 1, (800,600))
for image in images:
img = cv2.imread(image)
img = cv2.resize(img, (800,600))
video.write(img)
video.release()
The VideoWriter
creates the output file. Each image is resized and added to the video.
For more image manipulation techniques, see our Python Image Rescaling Guide.
Advanced Features
You can enhance your slideshow with these features:
- Add text captions to images
- Include background music
- Create custom transition effects
- Add image filters and adjustments
For working with image content, our Python Image Analysis Guide may help.
Common Issues
Some problems you might encounter:
- Images not found (check file paths)
- Different image sizes (resize them first)
- Memory issues with many large images
- Display window not closing properly
Conclusion
Creating image slideshows in Python is straightforward. The PIL and OpenCV libraries provide all needed tools.
Start with simple displays, then add features. Soon you'll create professional slideshows for any purpose.
Remember to handle errors and test your code. Happy slideshow making!