【问题标题】:How to create PNG image with color_type=3 and bit_depth=1如何使用 color_type=3 和 bit_depth=1 创建 PNG 图像
【发布时间】:2015-03-08 21:12:03
【问题描述】:

来源是 RGBA PNG 图像(color_type=6 和 bit_depth=8)。

我需要一个带有索引颜色和 2 个调色板项目(color_type=3,bit_depth=1)的图像。

我尝试使用 ImageMagick,但只能获得 1 位灰度图像(color_type=0,bit_depth=1)或仅使用 2 种颜色的 2 位索引彩色图像(color_type=3,bit_depth=2)。

根据PNG规范,这样的图像是可能的,但是如何创建呢?

这是我要转换的图像:

"convert input.png -type palette -depth 1 output.png"的结果:

"convert input.png -type palette -depth 1 -colors 2 output.png"的结果

两个结果都有 bit_depth=2,但第二个结果只使用了 4 种可能的 2 种颜色。

【问题讨论】:

    标签: imagemagick png imagemagick-convert


    【解决方案1】:

    使用以下 3 种之一,按输出文件中从小到大的辅助标签排序:

    convert -colors 2 -define png:include-chunk=none -verbose input.png output.png
    

     

    convert -colors 2 -define png:exclude-chunk=bkgd -verbose input.png output.png
    

     

    convert -colors 2 -background #000000 -verbose input.png output.png
    

    这个问题的根源是 ImageMagick 默认使用白色background color (bKGD) PNG chunk 标记图像(我会说这是不必要的),然后如果它不存在,则将此颜色添加到调色板中,即使图像没有该颜色的像素。您的特定图像在转换为 2 色后没有白色,因此不需要的背景颜色标记变为第 3 色,并且不能再保存为 1 位索引彩色图像。另见this from the author of IM

    其他人未能重现该问题的原因可能是他们测试了两种颜色中的一种恰好是白色的图像。

    -define png:include-chunk=none 的第一个选项通过根本不输出任何辅助 PNG 块(例如 bKGD、gAMA、cHRM、tEXt、tIME)来避免该问题。喜欢pngcrush -rem alla。对于一个不那么杂乱的文件,我更喜欢这个(没有它,即使它们不在输入文件中,IM 也会添加其中的一些)。 注意: 还有一个简单的-strip 选项,should avoid most of these,但从 v6.9.3 开始,由于错误,它不会削减 bKGD。

    带有-define png:exclude-chunk=bkgd 的第二个仅删除有问题的背景块。

    -background #000000 的第三个选项保留所有辅助 PNG 块,包括 bKGD,但将其设置为黑色,这是图像中存在的两种颜色之一,因此它仍然可以是 1 位。调整既不是白色也不是黑色的图像的颜色。

    请注意,对于所有这些,我都包括-verbose 开关。不太冗长;只是从零到两行状态输出,如果图像保持 2 色,您会注意到“2c”,否则会注意到“3c”。它还会告诉您调色板减少是否有损。它还为大多数图像输出“8 位 sRGB”,即使是每像素少于 8 位的调色板;这不是您对另一个答案@johnfound 的评论中的错误,这不是指每像素位数,而是指每颜色位数。对于(稀有)深色图像,它可以超过 8 位。

    【讨论】:

      【解决方案2】:

      转换 input.png -background white -t​​ype palette -depth 1 -colors 2 output.png

      为我工作。

      (为什么-depth=1不够用?不知道。)

      顺便说一句,tweakpng 工具对于检查这类事情很有用。

      更新:如果图像具有透明度,您可以通过显式删除它来更安全:

      转换 input.png -background white -flatten -type palette -depth 1 -colors 2 output.png

      (您可以根据自己的喜好替换white

      【讨论】:

      • 在我的测试中,使用的颜色减少到 2,但位深度也保持为 2。只是没有使用所有可用的颜色。
      • 尝试tweakpng 我发现调色板中的第三种颜色(索引2)用于bKGD 块。也许我们必须以某种方式阻止这个块生成?
      • 我无法重现你的结果,我总是得到 1 位深度;你能发布你的原始图片,或者告诉我们它的类型(和块?)
      • 当然。该问题已被编辑。我添加了原始图像和建议命令的结果(也来自其他答案)。
      • 奇怪,我的图像深度为 1 位(此处使用 ImageMagick 6.9.0-3 Q16 x64)。我建议首先删除透明度,例如将 -flatten 作为第一个选项。
      【解决方案3】:

      我认为你需要的命令是这样的:

      convert input.png -type palette -depth 1 output.png
      

      如果没有,请发布您的输入图片,并说明您如何/在哪里找到您引用的 color_typebit_depth 字段。

      Exiftool 告诉我:

      ExifTool Version Number         : 9.76
      File Name                       : output.png
      Directory                       : .
      File Size                       : 2.3 kB
      File Modification Date/Time     : 2015:01:10 19:20:33+00:00
      File Access Date/Time           : 2015:01:10 19:21:46+00:00
      File Inode Change Date/Time     : 2015:01:10 19:20:33+00:00
      File Permissions                : rw-r--r--
      File Type                       : PNG
      MIME Type                       : image/png
      Image Width                     : 640
      Image Height                    : 427
      Bit Depth                       : 1
      Color Type                      : Grayscale
      Compression                     : Deflate/Inflate
      Filter                          : Adaptive
      Interlace                       : Noninterlaced
      Gamma                           : 2.2
      Background Color                : 1
      Datecreate                      : 2015-01-10T10:21:30+00:00
      Datemodify                      : 2015-01-10T10:21:30+00:00
      Image Size                      : 640x427
      

      ImageMagick identify 告诉我这个:

      Image: output.png
        Format: PNG (Portable Network Graphics)
        Mime type: image/png
        Class: PseudoClass
        Geometry: 640x427+0+0
        Units: Undefined
        Type: Bilevel
        Base type: Bilevel
        Endianess: Undefined
        Colorspace: Gray
        Depth: 8/1-bit
        Channel depth:
          gray: 1-bit
        Channel statistics:
          Pixels: 273280
          Gray:
            min: 0 (0)
            max: 255 (1)
            mean: 1.90168 (0.00745755)
            standard deviation: 21.9388 (0.0860345)
            kurtosis: 129.1
            skewness: 11.4499
        Colors: 2
        Histogram:
          271242: (  0,  0,  0) #000000 gray(0)
            2038: (255,255,255) #FFFFFF gray(255)
        Colormap entries: 2
        Colormap:
               0: (  0,  0,  0) #000000 gray(0)
               1: (255,255,255) #FFFFFF gray(255)
        Rendering intent: Undefined
        Gamma: 0.45455
        Background color: gray(255)
        Border color: gray(223)
        Matte color: gray(189)
        Transparent color: gray(0)
        Interlace: None
        Intensity: Undefined
        Compose: Over
        Page geometry: 640x427+0+0
        Dispose: Undefined
        Iterations: 0
        Compression: Zip
        Orientation: Undefined
        Properties:
          date:create: 2015-01-10T19:20:33+00:00
          date:modify: 2015-01-10T19:20:33+00:00
          png:bKGD: chunk was found (see Background color, above)
          png:gAMA: gamma=0.45455 (See Gamma, above)
          png:IHDR.bit-depth-orig: 1
          png:IHDR.bit_depth: 1
          png:IHDR.color-type-orig: 0
          png:IHDR.color_type: 0 (Grayscale)
          png:IHDR.interlace_method: 0 (Not interlaced)
          png:IHDR.width,height: 640, 427
          png:text: 2 tEXt/zTXt/iTXt chunks were found
          signature: 3e08d7fea7bc7aeb0659ac2e2696084083d35ce30b0e3075dc561ad94259eaec
        Artifacts:
          filename: output.png
          verbose: true
        Tainted: True
        Filesize: 2.33KB
        Number pixels: 273K
        Pixels per second: 27.33MB
        User time: 0.000u
        Elapsed time: 0:01.009
        Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-11-10 http://www.imagemagick.org
      

      【讨论】:

      • 不幸的是,上述命令生成的图像颜色类型为 3,但位深为 2;我检查直接解析图像的字段的值——它们是我的 PNG 解码库实现的测试图像。这些值是正确的,因为程序能够解码和显示它们。顺便说一句,在相同的图像上,“identify -verbose”为位深度提供了错误的值 8 - 可能是错误。
      猜你喜欢
      • 1970-01-01
      • 2011-06-10
      • 2014-12-25
      • 2014-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 1970-01-01
      相关资源
      最近更新 更多