From 75a42ec54dbf721caa659ddf02c1f46fc2cb4bef Mon Sep 17 00:00:00 2001 From: mlot Date: Fri, 6 Jun 2025 13:40:57 -0400 Subject: initial commit for archiving --- Chap17PracPhotoFolders.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Chap17PracPhotoFolders.py (limited to 'Chap17PracPhotoFolders.py') diff --git a/Chap17PracPhotoFolders.py b/Chap17PracPhotoFolders.py new file mode 100644 index 0000000..d20a56c --- /dev/null +++ b/Chap17PracPhotoFolders.py @@ -0,0 +1,37 @@ +#! python3 + +# Chapter 17 Practice Indetifying Photo Folders on the Hard Drive + +# Searches your hard drive for any (sub)folders that contain image files +# that are .png or .jpg that are at least 500 pixels in size or larger +# and prints their absolute path to the screen. + +import os +from PIL import Image + +for foldername, subfolders, filenames in os.walk('c:\\'): + numPhotoFiles = 0 + numNonPhotoFiles = 0 + for filename in filenames: + # Check if extension is not .png or .jpg + if not filename.endswith('.png') or filename.endswith('.jpg'): + numNonPhotoFiles += 1 + continue + + # Open image file using pillow. Skip file if error occurs. + try: im = Image.open(os.path.join(foldername, filename)) + except: continue + width, height = im.size + + # Check if width and height are larger than 500 + if width >= 500 and height >= 500: + # Image is large enough to be considered a photo. + numPhotoFiles += 1 + else: + # Image is too small to be a photo. + numNonPhotoFiles += 1 + + # If more than half of files were photos, + # print the absolute path of the folder. + if numPhotoFiles > numNonPhotoFiles: + print(foldername) -- cgit