Advertisement
zelenooki87

Sort images by Orientation - python script

Aug 20th, 2024 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | Science | 0 0
  1. import os
  2. from os import path
  3. from PIL import Image
  4. from enum import Enum
  5. import shutil
  6. import time
  7.  
  8. # Disable decompression bomb check
  9. Image.MAX_IMAGE_PIXELS = None
  10.  
  11. class Orientation(Enum):
  12.     UNDEFINED = 0
  13.     HORIZONTAL = 1
  14.     VERTICAL = 2
  15.     SQUARE = 3
  16.  
  17.  
  18. class ImageAndOrientation(object):
  19.     file = object
  20.     orientation = Orientation.UNDEFINED
  21.  
  22.  
  23. # Get Directory
  24. dir = r"G:\SKEN"
  25. while not path.exists(dir) and not os.path.isdir(dir):
  26.     dir = input("Invalid Path, try again: ")
  27.  
  28. # Scan directory for files and images
  29. print()
  30. print('Scanning the directory for files...')
  31.  
  32. images = []
  33. for file in os.scandir(dir):
  34.     try:
  35.         # Try to open the file as an image
  36.         with Image.open(file.path) as img:
  37.             # If successfully opened as an image, add to the list
  38.             image = ImageAndOrientation()
  39.             image.file = file
  40.             images.append(image)
  41.     except IOError:
  42.         # If not an image, skip the file
  43.         pass
  44.  
  45. # Processing and sorting images
  46. print()
  47. print("{} images found".format(len(images)))
  48. start_time = time.time()
  49. processed_count = 0
  50. for i in range(len(images)):
  51.     try:
  52.         with Image.open(images[i].file.path) as img:
  53.             width, height = img.size
  54.             if width > height:  
  55.                 images[i].orientation = Orientation.HORIZONTAL
  56.             elif width < height:  
  57.                 images[i].orientation = Orientation.VERTICAL
  58.             elif width == height:
  59.                 images[i].orientation = Orientation.SQUARE
  60.     except Exception as e:
  61.         print(f"Error processing {images[i].file.path}: {e}")
  62.         images[i].orientation = Orientation.UNDEFINED
  63.  
  64.     processed_count += 1
  65.     elapsed_time = time.time() - start_time
  66.     print(f"Processed: {processed_count} images ({elapsed_time:.2f}s)", end="\r")
  67.  
  68. # Create directories for each orientation if they don't exist
  69. if not os.path.exists(os.path.join(dir, Orientation.HORIZONTAL.name)):
  70.     os.makedirs(os.path.join(dir, Orientation.HORIZONTAL.name))
  71. if not os.path.exists(os.path.join(dir, Orientation.VERTICAL.name)):
  72.     os.makedirs(os.path.join(dir, Orientation.VERTICAL.name))
  73. if not os.path.exists(os.path.join(dir, Orientation.SQUARE.name)):
  74.     os.makedirs(os.path.join(dir, Orientation.SQUARE.name))
  75.  
  76. # Move images to their respective orientation folders
  77. for image in images:
  78.     if image.orientation != Orientation.UNDEFINED:
  79.         shutil.move(image.file.path, os.path.join(
  80.             dir, image.orientation.name, image.file.name))
  81.  
  82. print()
  83. print("Finished! The images are moved to separate folders based on their orientation.")
  84. print("These folders can be found in: \"{}\"".format(dir))
  85. input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement