Info
Currently the program assumes the image files are all valid and as of now if it gets a invalid image file it doesn't fail smoothly.
ToDo: Figure out how to handle this issue.
Brainstorming Possible Solution(s)
- Verify all the images before starting the slide show (using something like PIL.Image.verify)
- Handle when you get to the invalid image and either discard the image and continue
- Show "Error can't open {image name here}" text on screen when it gets to that image
Solution (1)
something like this could work
from PIL import Image
import os
def is_valid_image(path):
try:
with Image.open(path) as img:
img.verify() # Verify integrity without fully decoding
return True
except Exception:
return False
def get_valid_images_in_folder(folder_path):
valid_images = []
for filename in os.listdir(folder_path):
path = os.path.join(folder_path, filename)
if os.path.isfile(path) and is_valid_image(path):
valid_images.append(path)
return valid_images
OR to take it one step further to make it more efficient(especially if there are LOTS of images to verify). We can use multi threading like:
from PIL import Image
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
def is_valid_image(path):
try:
with Image.open(path) as img:
img.verify()
return path # Return path if valid
except Exception:
return None # Return None if invalid
def get_valid_images_in_folder(folder_path, max_workers=8):
image_paths = [
os.path.join(folder_path, f)
for f in os.listdir(folder_path)
if os.path.isfile(os.path.join(folder_path, f))
]
valid_images = []
with ThreadPoolExecutor(max_workers=max_workers) as executor:
future_to_path = {executor.submit(is_valid_image, path): path for path in image_paths}
for future in as_completed(future_to_path):
result = future.result()
if result:
valid_images.append(result)
return valid_images
Info
Currently the program assumes the image files are all valid and as of now if it gets a invalid image file it doesn't fail smoothly.
ToDo: Figure out how to handle this issue.
Brainstorming Possible Solution(s)
Solution (1)
something like this could work
OR to take it one step further to make it more efficient(especially if there are LOTS of images to verify). We can use multi threading like: