如何确定 PNG 是 PNG-8 还是 PNG-24?

Exi*_*xil 15 command-line png image-processing

我有一个 PNG 文件,但我不知道它是 PNG-8 还是 PNG-24?

Rin*_*ind 31

32 位 PNG(每通道 8 位 - 带 alpha,也称为“带有 8 位 alpha 的 24 位 PNG”或“带有 alpha 的 PNG24”):

$ file zyne_zyne.png: 
PNG image data, 64 x 64, 8-bit/color RGBA, non-interlaced
Run Code Online (Sandbox Code Playgroud)

24 位 PNG(每通道 8 位 - 无 alpha 通道)

$ file 24.png
PNG image data, 1060 x 821, 8-bit/color RGB, non-interlaced
Run Code Online (Sandbox Code Playgroud)

8 位 PNG(256 色):

$ file 8bit.png
PNG image data, 1060 x 821, 8-bit colormap, non-interlaced
Run Code Online (Sandbox Code Playgroud)


Win*_*nix 26

file其他答案中使用的命令有时不一致。例如:

???????????????????????????????????????????????????????????????????????????????
rick@dell:~/Pictures$ file "256 colors.jpg"
256 colors.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 96x96, segment length 16, baseline, precision 8, 800x800, frames 3
???????????????????????????????????????????????????????????????????????????????
rick@dell:~/Pictures$ file "vnStat.png"
vnStat.png: PNG image data, 410 x 121, 8-bit/color RGB, non-interlaced
Run Code Online (Sandbox Code Playgroud)

第一个实例可能是相机图片,第二个实例是我使用屏幕截图拍摄的。

identify我的答案所基于的命令似乎在所有情况下都能正常工作。例如在上面的第一个实例上进行测试:

rick@dell:~/Pictures$ identify "256 colors.jpg"
256 colors.jpg JPEG 800x800 800x800+0+0 8-bit sRGB 45KB 0.000u 0:00.000
Run Code Online (Sandbox Code Playgroud)

我安装了ImageMagick,它允许我使用:

$ identify -verbose vnStat.png
Image: vnStat.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 410x121+0+0
  Units: Undefined
  Type: TrueColor
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 49610
    Red:
      min: 0 (0)
      max: 255 (1)
      mean: 12.0814 (0.0473779)
      standard deviation: 46.7032 (0.18315)
      kurtosis: 18.8998
      skewness: 4.49862
    Green:
      min: 0 (0)
      max: 255 (1)
      mean: 21.1804 (0.0830606)
      standard deviation: 57.5336 (0.225622)
      kurtosis: 6.68226
      skewness: 2.82613
    Blue:
      min: 0 (0)
      max: 255 (1)
      mean: 14.2606 (0.0559237)
      standard deviation: 50.2969 (0.197243)
      kurtosis: 13.5573
      skewness: 3.85914
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 15.8408 (0.0621207)
      standard deviation: 51.7078 (0.202776)
      kurtosis: 11.8528
      skewness: 3.62638
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 410x121+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2017-01-24T20:04:50-07:00
    date:modify: 2017-01-24T20:04:50-07:00
    png:IHDR.bit-depth-orig: 8
    png:IHDR.bit_depth: 8
    png:IHDR.color-type-orig: 2
    png:IHDR.color_type: 2 (Truecolor)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 410, 121
    png:sRGB: intent=0 (Perceptual Intent)
    png:text: 1 tEXt/zTXt/iTXt chunks were found
    signature: e9f31b79da6ce46cdea5da21ae8648496faa181b0621098aa2dbbdff4a9a4502
    Software: gnome-screenshot
  Artifacts:
    filename: vnStat.png
    verbose: true
  Tainted: False
  Filesize: 14.7KB
  Number pixels: 49.6K
  Pixels per second: 0B
  User time: 0.000u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 http://www.imagemagick.org
Run Code Online (Sandbox Code Playgroud)

对于摘要报告,省略所有参数:

$ identify vnStat.png
vnStat.png PNG 410x121 410x121+0+0 8-bit sRGB 14.7KB 0.000u 0:00.000
Run Code Online (Sandbox Code Playgroud)

替换vnStat.png为您自己的.png文件名,并在必要时以路径开头。


要了解 PNG-24 与 PNG-32,SuperUser 的这个答案总结了它:

如果您的图像是 24 位,您将看到:

Channel depth:
  red: 8-bit
  green: 8-bit
  blue: 8-bit
Run Code Online (Sandbox Code Playgroud)

如果您的 PNG 图像是 32 位,您将看到:

Channel depth:
  red: 8-bit
  green: 8-bit
  blue: 8-bit
  alpha: 8-bit
Run Code Online (Sandbox Code Playgroud)

然而,这个答案并不完整,因为 8 位颜色还显示了 8 位的红色、绿色和蓝色,仅使用调色板来限制选择的数量(就像在旧的 Windows 2.0 中一样)。


检查维基百科深度文章中的这三张图片:

8_bit.png

文档名称: 8_bit.png

使用:

identify -verbose  8_bit.png | grep colors
    png:PLTE.number_colors: 249
Run Code Online (Sandbox Code Playgroud)

我们看到使用了 249 种颜色。8 位的最大可能是 256 色。


4 位

文档名称: 4_bit.png

使用:

identify -verbose  4_bit.png | grep colors
    png:PLTE.number_colors: 16
Run Code Online (Sandbox Code Playgroud)

我们看到使用了 16 种颜色,这是 4 位可能的最大颜色组合。


2_bit.png

文档名称: 2_bit.png

使用:

$ identify -verbose  2_bit.png | grep colors
    png:PLTE.number_colors: 4
Run Code Online (Sandbox Code Playgroud)

我们看到使用了 4 种颜色,这是 2 位可能的最大颜色组合。

  • 要获得更适合脚本的格式,您还可以执行 `identify -format %z` - 有关更多信息,请参阅 [ImageMagick 的 -format 选项](https://www.imagemagick.org/script/escape.php)。 (2认同)

小智 8

如果您安装了exiftool,您可以执行以下操作:

bash-4.3# exiftool 大洲_blank_l.png 
ExifTool 版本号:10.20
文件名:大洲_blank_l.png
目录 : 。
文件大小:170 kB
文件修改日期/时间:2016:07:05 07:59:28+05:30
文件存取日期/时间:2017:08:07 00:35:15+05:30
文件 inode 更改日期/时间:2016:07:05 07:59:28+05:30
文件权限:rw-r--r--
文件类型:PNG
文件类型扩展名:.png
MIME 类型:图像/png
图像宽度:1100
图像高度:850
位深:8
颜色类型 : RGB 带 Alpha
压缩:放气/充气
过滤器:自适应
隔行:非隔行
背景颜色:255 255 255
每单位像素 X : 3937
每单位 Y 的像素数:3937
像素单位:米
修改日期:2016:07:05 02:29:28
图像尺寸:1100x850
百万像素:0.935


leo*_*loy 5

黑客的方法:窥视IHDR PNG 块中的真实字节。

 $ od -j 24 -N 2 -t u1 -An drivemount-applet_open.png
   8   2
Run Code Online (Sandbox Code Playgroud)

第一个数字告诉您位深度。第二,颜色类型(2为RGB图像,即具有三个通道的图像,或每像素三个样本值)。

因此,在这种情况下,我们有一个 RGB8 PNG(每像素 24 位)。