如何使用Python Wand修复奇怪的IHDR属性?

如何使用Python Wand修复奇怪的IHDR属性?,python,imagemagick,png,wand,Python,Imagemagick,Png,Wand,我正在尝试生成一个RGBA PNG文件,该文件的格式与我正在使用的RIP软件兼容。问题是它不喜欢Wand生成的透明度数据。我有一个RIP软件喜欢的示例文件。我使用Imagemagick“identifite-verbose”命令检查已知良好文件的属性。我发现,即使许多其他属性与我的文件匹配,IHDR属性也非常不同。在正确加载的文件中,我看到: png:IHDR.bit-depth-orig: 8 png:IHDR.bit_depth: 8 png:IHDR.color-type-orig: 6

我正在尝试生成一个RGBA PNG文件,该文件的格式与我正在使用的RIP软件兼容。问题是它不喜欢Wand生成的透明度数据。我有一个RIP软件喜欢的示例文件。我使用Imagemagick“identifite-verbose”命令检查已知良好文件的属性。我发现,即使许多其他属性与我的文件匹配,IHDR属性也非常不同。在正确加载的文件中,我看到:

png:IHDR.bit-depth-orig: 8
png:IHDR.bit_depth: 8
png:IHDR.color-type-orig: 6
png:IHDR.color_type: 6 (RGBA)
(在Chrome中,透明背景看起来是黑色的)

当我检查Wand生成的文件的属性时,我看到:

png:IHDR.bit-depth-orig: 4
png:IHDR.bit_depth: 4
png:IHDR.color-type-orig: 3
png:IHDR.color_type: 3 (Indexed)
png:PLTE.number_colors: 8
(在Chrome中,透明背景看起来是黑色的)

我正在使用:

ImageMagick 7.0.8-12 Q16 x86_64 2018-10-23
Wand==0.4.4
绘制此文件的代码在透明背景上为打印机测试图案创建三个彩色框。这是:

from wand.image import Image
from wand.color import Color
from wand.drawing import Drawing

depth   = 32
max_dpi = 300
width   = 600
height  = 600

big_square_color    = Color('blue') #Color('transparent')
med_square_color    = Color('red')
small_square_color  = Color('green')
background_color    = Color('transparent')

# with Image(width=width, height=height, resolution=max_dpi, depth=depth, background=background_color) as source_img:
with Image(width=width, height=height, resolution=max_dpi, background=background_color) as source_img:
    source_img.depth  = 8
    with Drawing() as draw:

        # Draw a big box in the middle
        draw.stroke_color = draw.fill_color = big_square_color
        draw.rectangle(left=(width/16), top=(height/16), right=width-(width/16), bottom=height-(height/16))

        # Draw a smaller box in the middle
        draw.stroke_color = draw.fill_color = med_square_color
        draw.rectangle(left=(width/8), top=(height/8), right=width-(width/8), bottom=height-(height/8))

        # Draw the smallest box in the middle
        draw.stroke_color = draw.fill_color = small_square_color
        draw.rectangle(left=(width/4), top=(height/4), right=width-(width/4), bottom=height-(height/4))

        draw(source_img)

    source_img.format   = 'png'
    source_img.units    = 'pixelspercentimeter'
    source_img.colorspace  = 'srgb'
    source_img.type     = 'truecolor'
    source_img.alpha_channel = True

    source_img.save(filename='output.png')

查看Imagemagick和Wand的文档,我看不到强制使用IHDR值的方法。有人知道如何更改Wand.Image对象的属性以获得IHDR.color\u type:6和png:IHDR.bit\u depth:8吗?

马克的评论是正确的。PNG编码器认为没有足够的图像数据来证明RBGA颜色类型,因此它优化了带有索引调色板的图像。将
PNG32:
协议作为出站文件名的前缀,或设置
source\u img.format='PNG32'
应该可以工作

查看Imagemagick和Wand的文档,我看不到强制使用IHDR值的方法


Anthony的文章涵盖了这一点,但在“顺便说一句”的评论中读到了更多。Wand最近开始暗示代理协议,但我同意,这也可以在这个主题上使用更好的细节/覆盖。这两个文档都没有引起对
IHDR
(图像标题)的太多关注,因此这可能不是搜索的最佳关键字。

请尝试最后一行作为
source\u image.save(filename='PNG32:output.png')
,这很有效!回顾一下文档,我在0.4.5文档中看到了关于这一点的注释。自从我使用0.4.4并升级到0.4.5作为发布前的最后一次测试以来,我一直在查看0.4.4文档。但愿我能把你的评论作为答案!不客气,很高兴成功了!不管怎么说,给Eric@emcconville打分没问题——他是一个很棒的家伙,可以为他所有的努力再加分,再次感谢。这确实奏效了。我试着设置format属性,但只是在文件名中添加了“PNG32:”标记。