Search code examples
pythonpython-imaging-library

Efficient way to delete image of the same size


I have many images in a folder and I am want to delete images of the same size. My code below, using PIL, works but I want to know if there is a more efficient way to achieve this.

import os
from PIL import Image

def checkImages(dirs):
  image_list = os.listdir(dirs)
  for i in range(len(image_list)):
      for j in range(i + 1, len(image_list)):
        im_a = Image.open(dirs + '/' + image_list[i])
        im_b = Image.open(dirs + '/' + image_list[j])
        if im_a.size == im_b.size:
          os.remove(dirs + '/' + image_list[j])
          del image_list[j]

checkImages('/content/gdrive/MyDrive/testdata')

Solution

  • You can keep a dictionary of sizes and delete any images that have a size that have already been seen. That way you don't need a nested loop, and don't have to create Image objects for the same file multiple times.

    def checkImages(dirs):
      sizes = {}
      for file in os.listdir(dirs):
          size = Image.open(dirs + '/' + file).size
          if size in sizes:
             os.remove(dirs + '/' + file)
          else:
             sizes[size] = file