如何在Python中找到索引颜色模式PNG

发布于 2025-02-12 05:40:47 字数 839 浏览 0 评论 0原文

我如何获得使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你鎻心 2025-02-19 05:40:47

您可以使用以下内容:

#!/usr/bin/env python3

from PIL import Image
from glob import glob

# Start with empty list
paletteImages = []

# Iterate over all PNGs
for f in glob("*.png"):
  # Open with PIL
  im = Image.open(f)
  # Append to list if palette image
  if 'P' in im.mode: paletteImages.append(f)

print(paletteImages)

有关调色板/索引图像的更多讨论在这里

请注意,上面的代码还将找到palettealpha图像,即具有mode ='pa'的图像,因此,将中的中的更改为=====如果您不想要palettealpha图像。


或者,您可以更简单地在终端中使用 ImageMagick 进行操作:

identify -verbose  *png | egrep "Filename:|Type|png.IHDR.color_type"

Filename: 3978.png
Type: PaletteAlpha
png:IHDR.color_type: 3 (Indexed)
Filename: 6507.png
Type: Palette
png:IHDR.color_type: 3 (Indexed)

请注意,您可以从 wand ctypes绑定到 imagemagick


或者,您可以使用pngcheck

pngcheck *png   
                           
OK: 3978.png (64x64, 8-bit palette+trns, non-interlaced, 33.1%).
OK: 6507.png (64x64, 8-bit palette, non-interlaced, 56.3%).

您可以使用python subprocess.run()调用此。


或者,您可以使用Exiftool

exiftool *png | egrep "Color Type|File Name"   
File Name                       : 3978.png
Color Type                      : Palette
File Name                       : 6507.png
Color Type                      : Palette

您可以将Python绑定到Exiftool获得相同的结果。示例在这里


或者,您可以搜索PLTE,即PALETTE PNG块:

grep PLTE *png     

Binary file 3978.png matches
Binary file 6507.png matches

或者您可以用Python sl缩image并在泥浆中搜索字符串以获得相同的结果:

with open('3978.png', 'rb') as fd:
   data = fd.read()
   if b'PLTE' in data:
      print('PLTE was found')

You can use this:

#!/usr/bin/env python3

from PIL import Image
from glob import glob

# Start with empty list
paletteImages = []

# Iterate over all PNGs
for f in glob("*.png"):
  # Open with PIL
  im = Image.open(f)
  # Append to list if palette image
  if 'P' in im.mode: paletteImages.append(f)

print(paletteImages)

More discussion on palette/indexed images here.

Note that the above code will also find PaletteAlpha images, i.e. those with mode = 'PA', so change in to == if you don't want PaletteAlpha images.


Or you can do it with ImageMagick in the Terminal more simply:

identify -verbose  *png | egrep "Filename:|Type|png.IHDR.color_type"

Filename: 3978.png
Type: PaletteAlpha
png:IHDR.color_type: 3 (Indexed)
Filename: 6507.png
Type: Palette
png:IHDR.color_type: 3 (Indexed)

Note that you can get exactly the same results from wand which is a ctypes binding to ImageMagick.


Or you can use pngcheck:

pngcheck *png   
                           
OK: 3978.png (64x64, 8-bit palette+trns, non-interlaced, 33.1%).
OK: 6507.png (64x64, 8-bit palette, non-interlaced, 56.3%).

You could call this with a Python subprocess.run().


Or you can use exiftool:

exiftool *png | egrep "Color Type|File Name"   
File Name                       : 3978.png
Color Type                      : Palette
File Name                       : 6507.png
Color Type                      : Palette

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:

grep PLTE *png     

Binary file 3978.png matches
Binary file 6507.png matches

Or you can slurp the image with Python and search for string in the slurped file to get the same result:

with open('3978.png', 'rb') as fd:
   data = fd.read()
   if b'PLTE' in data:
      print('PLTE was found')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文