Showing Random Picture from a Folder in Python
Using the Python Pillow library, we can show the picture randomly from any folder. Here is the step-by-step procedure for doing this.
Step 1: Install the Required Libary
pip install pillow
Step 2: Getting the image path randomly from the folder
#import necessary package
import os
import random
from PIL import Image
def get_random_image_path(folder_path):
try:
files = os.listdir(folder_path)
# Filter the list to get only image files
images = [file for file in files if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif'))]
if not images:
print("No images found in the specified folder.")
return None
# Choose a random image file
random_image = random.choice(images)
random_image_path = os.path.join(folder_path, random_image)
print(f"Here the random image path from the folder:")
return random_image_path
except Exception as e:
print(f"An error occurred while selecting image randomly: {e}")
return None
Example Output:
Suppose, the images are in this folder, "/home/somraj/All Data/somraj image folder/"
Here the random image path from the folder: /home/somraj/All Data/somraj image folder/image1.png
Step 3: Displaying the random Image using PIL
#imporing all necessary packages
import os
from PIL import Image
def display_image(image_path):
try:
if image_path and os.path.isfile(image_path):
with Image.open(image_path) as img: #opening the random image
img.show()
print(f"Displayed image: {image_path}")
else:
print(f"Invalid image path: {image_path}")
except Exception as e:
print(f"An error occurred while displaying the image: {e}")
Example Output:
Display image: /home/somraj/All Data/somraj image folder/image1.png
Step 4: Integrating both function to showing the random Image
def show_random_image_from_folder(folder_path):
random_image_path = get_random_image_path(folder_path)
display_image(random_image_path)
# usage:
if __name__ == "__main__":
folder_path = '/path/to/your/image_folder'
if os.path.isdir(folder_path):
show_random_image_from_folder(folder_path)
else:
print(f" The specified folder does not exist: {folder_path}")
Full code
import os
import random
from PIL import Image
def get_random_image_path(folder_path):
try:
files = os.listdir(folder_path)
# Filter the list to get only image files
images = [file for file in files if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif'))]
if not images:
print("No images found in the specified folder.")
return None
# Choose a random image file
random_image = random.choice(images)
random_image_path = os.path.join(folder_path, random_image)
return random_image_path
except Exception as e:
print(f"An error occurred while selecting image randomly: {e}")
return None
def display_image(image_path):
try:
if image_path and os.path.isfile(image_path):
with Image.open(image_path) as img:
img.show()
print(f"Displayed image: {image_path}")
else:
print(f"Invalid image path: {image_path}")
except Exception as e:
print(f"An error occurred while displaying the image: {e}")
def show_random_image_from_folder(folder_path):
random_image_path = get_random_image_path(folder_path)
display_image(random_image_path)
# usage:
if __name__ == "__main__":
folder_path = '/path/to/your/image_folder'
if os.path.isdir(folder_path):
show_random_image_from_folder(folder_path)
else:
print(f" The specified folder does not exist: {folder_path}")
Example Output:
Here the random image path from the folder: /home/somraj/All Data/somraj image folder/image1.png
Display image: /home/somraj/All Data/somraj image folder/image1.png
NOTE: The folder structure of showing a Random Image from a Folder will be like this,
Image Folder: home/somraj/All Data/somraj image folder/
├── image1.png
├── image2.jpg
├── document.pdf
└── image3.gif
Here is the video of real output of the code
Show Random Picture from a Folder in Python
In this article, we are going to share with you the steps to create a simple script that selects and displays a random image from a specified folder using Python. By using the combination of os, random, and PIL libraries, you can easily do this, So, let’s get started.