I have a working code that will iterate through a folder, identify and delete if a .tif
only contains pixel values of all zero, hence a black image. The problem is that I have 12,000 images in the folder and it take quite a long time for the process to finish. I am wondering if there is a faster way I could do this?
from PIL import Image
import os
directory = 'D:/images/'
for image in os.listdir(directory):
indiv = Image.open(directory + image)
pixel_values = list(indiv.getdata())
y = len(pixel_values)
list_yes = []
for RGBA in pixel_values:
if RGBA == (0, 0, 0, 0):
Black_image = 'yes'
list_yes.append(Black_image)
x = len(list_yes)
if x == y:
os.remove(directory + image)
Output of black .tif:
(0, 0, 0, 0)
(0, 0, 0, 0)
(0, 0, 0, 0)
(0, 0, 0, 0)
(0, 0, 0, 0)
(0, 0, 0, 0)
(0, 0, 0, 0)
(0, 0, 0, 0)
(0, 0, 0, 0)
(0, 0, 0, 0)
(0, 0, 0, 0)
(0, 0, 0, 0)
(0, 0, 0, 0)
....
Like 400,000 more rows of this
This should be substantially faster
directory = 'D:/images/'
for image in os.listdir(directory):
indiv = Image.open(directory + image)
if all(pixels == (0, 0, 0, 0) for pixels in list(indiv.getdata())):
os.remove(directory + image)
I'm not sure the list(...)
is needed either, I'm not too familiar with PIL. If it works without, removing it should cause another speedup.