如何在Python中找到索引颜色模式PNG
我如何获得使用Python中包含许多其他文件的给定文件夹中存在的所有索引颜色模式PNG的列表?
以前尝试过的代码:
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))
使用此代码,我得到了一个图像列表,其中一些是索引颜色模式,其余为RGB颜色模式(通过Photoshop检查它们) https:///www.dropbox.com/s/www.dropbox.com/s/s/s/s/s/vlvywqhcfrkkk.8kq/3978。 png?dl = 0 这是指向图像通过脚本显示为P的图像的链接,但它是Photoshop中的RGB图像。 https://www.dropbox.com/s/x3qiuuhs3gv9bp9/6507. png?dl = 0 这是一个真正的索引颜色图像,就像我需要找到的颜色图像一样。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用以下内容:
有关调色板/索引图像的更多讨论在这里。
请注意,上面的代码还将找到
palettealpha
图像,即具有mode ='pa'
的图像,因此,将中的中的更改为=====
如果您不想要palettealpha
图像。或者,您可以更简单地在终端中使用 ImageMagick 进行操作:
请注意,您可以从 wand 是
ctypes
绑定到 imagemagick 。或者,您可以使用
pngcheck
:您可以使用python
subprocess.run()
调用此。或者,您可以使用
Exiftool
:您可以将Python绑定到
Exiftool
获得相同的结果。示例在这里。或者,您可以搜索PLTE,即PALETTE PNG块:
或者您可以用Python sl缩image并在泥浆中搜索字符串以获得相同的结果:
You can use this:
More discussion on palette/indexed images here.
Note that the above code will also find
PaletteAlpha
images, i.e. those withmode = 'PA'
, so changein
to==
if you don't wantPaletteAlpha
images.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
ctypes
binding 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: