The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a photo sorter by resolution

Sorting photos by resolution is a useful tool for organizing large image collections. Below is a comprehensive guide to building a photo sorter that reads images from a folder, sorts them by their resolution (width × height), and outputs the sorted list or moves/copies the files accordingly.


Photo Sorter by Resolution (Python)

python
import os from PIL import Image def get_image_resolution(image_path): """Return (width, height) of the image.""" with Image.open(image_path) as img: return img.size # returns (width, height) def sort_photos_by_resolution(folder_path): """Sort images in a folder by their resolution.""" supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff') images_info = [] for filename in os.listdir(folder_path): if filename.lower().endswith(supported_formats): full_path = os.path.join(folder_path, filename) try: width, height = get_image_resolution(full_path) resolution = width * height images_info.append((filename, width, height, resolution)) except Exception as e: print(f"Error reading {filename}: {e}") # Sort images by resolution (descending) images_info.sort(key=lambda x: x[3], reverse=True) return images_info def print_sorted_images(images_info): """Print sorted image details.""" for filename, width, height, resolution in images_info: print(f"{filename}: {width}x{height} ({resolution} pixels)") if __name__ == "__main__": folder = input("Enter the folder path containing images: ").strip() sorted_images = sort_photos_by_resolution(folder) print_sorted_images(sorted_images)

Explanation:

  • PIL (Pillow): Used for opening images and getting their dimensions.

  • Supported formats: JPG, PNG, BMP, GIF, TIFF.

  • Sorting logic: Images are sorted by total pixel count (width × height), descending order.

  • Output: Prints file name and resolution details.


Optional Enhancements:

  • Move files into subfolders by resolution category, e.g., High, Medium, Low.

  • Copy files instead of just listing.

  • GUI interface with Tkinter for user-friendliness.

  • Export results to CSV or JSON.

If you want, I can help build any of these extensions!

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About