Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

python - Delete Rasters that contain pixel values of Zero

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...