Skip to content

Instantly share code, notes, and snippets.

@estevaofon
Created July 8, 2024 17:50
Show Gist options
  • Save estevaofon/83db805a47bbf890c710cf44a740c011 to your computer and use it in GitHub Desktop.
Save estevaofon/83db805a47bbf890c710cf44a740c011 to your computer and use it in GitHub Desktop.
import os
import shutil
from PIL import Image
def is_image(file_path):
try:
with Image.open(file_path) as img:
return True
except (IOError, FileNotFoundError):
return False
def gather_photos(source_folder, destination_folder='photos_gathered'):
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
for root, _, files in os.walk(source_folder):
for file in files:
file_path = os.path.join(root, file)
if is_image(file_path):
shutil.copy(file_path, destination_folder)
print(f"Copied: {file_path}")
if __name__ == "__main__":
source_folder = r"C:\Users\estev\Pictures" # You can change this to any directory you want to scan
gather_photos(source_folder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment