How do I get a list of all the Indexed Color mode PNGs that are present in a given folder that contains a lot of other files (which are all images, but they all have different Color Modes) using Python?
Previously tried code:
from PIL import Image
import os
path = 'logos/'
for x in os.listdir (path):
if x.endswith(".png"):
img = Image.open(path + x)
cmode = str(img)
P = 'mode=P' in cmode
if P == True:
print (x + " " + str(img))
Using this code, I got a list of images, some of which are Indexed Color mode, and the rest are RGB color mode (checking them through Photoshop) https://www.dropbox.com/s/vlvywqhcfrkk8kq/3978.png?dl=0 This is a link to an image that shows up as P through the script, but it is an RGB image in Photoshop. https://www.dropbox.com/s/x3qiuuhs3gv9bp9/6507.png?dl=0 This is a truly Indexed Color image, like the ones that I need to find.
You can use this:
More discussion on palette/indexed images here.
Note that the above code will also find
PaletteAlphaimages, i.e. those withmode = 'PA', so changeinto==if you don't wantPaletteAlphaimages.Or you can do it with ImageMagick in the Terminal more simply:
Note that you can get exactly the same results from wand which is a
ctypesbinding to ImageMagick.Or you can use
pngcheck:You could call this with a Python
subprocess.run().Or you can use
exiftool:You can get the same results with the Python binding to
exiftool. Example here.Or you can search for the PLTE, i.e. palette PNG chunk:
Or you can slurp the image with Python and search for string in the slurped file to get the same result: