Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading