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
question from:
https://stackoverflow.com/questions/66055818/delete-rasters-that-contain-pixel-values-of-zero