【问题标题】:Imagemagick wand use of depth is not working like command lineImagemagick 魔杖使用深度不像命令行那样工作
【发布时间】:2018-09-09 22:30:44
【问题描述】:

所以我曾经像这样在 bash 脚本中直接运行 imagemagick:

/usr/local/bin/convert image.jpg -resize 1000x1000\! -depth 2 result.jpg

所以我决定使用 wand 将我的脚本转换为 python!

from wand.image import Image
...
with Image(file=f) as img:
    img.transform(resize='1000x1000!') 
    img.depth = 2
    img.save(filename='result_py.jpg') 
f.close()
...

我注意到如果我从 bash 脚本中删除“-depth 2”,结果图像将与 python 的结果完全相同,那么我在 python 程序中缺少什么?为什么python中的depth选项不起作用?

【问题讨论】:

  • 我尝试了使用您的第一张图像作为输入的代码,它使用convertwand.image 生成完全相同的图像 - 但是,如果您运行identify -verbose result.jpg,它会显示Depth: 8-bit。 ..
  • 发布您的原始图片。你的 ImageMagick 是什么版本的?什么版本的魔杖?如果不是最新版本,请尝试升级。
  • @MoRe,您可以使用第二张图片作为原始图片进行测试。原始 (image.jpg) 是第二个的 25x25 尺寸
  • @fmw42 origina(image.jpg), upl.co/uploads/circuloas181536536659.jpg

标签: python linux imagemagick wand color-depth


【解决方案1】:

使用 Python 的 wand 库,您想使用 wand.image.Image.quantize 方法,并将颜色减少到 4 种(黑/白 + 2 种颜色)。

from wand.image import Image
...
with Image(file=f) as img:
    img.transform(resize='1000x1000!') 
    img.quantize(4,      # Number of colors
                 'gray', # Colorspace
                 0,      # Tree depth
                 False,  # Dither
                 False)  # Measure Error
    img.save(filename='result_py.jpg') 
f.close()
...

我相信quantize 方法是在 0.4.2 版本中添加的。另请注意 wand 目前支持 ImageMagick-6,因此您的系统可能同时安装了 6 和 7。

【讨论】:

    【解决方案2】:

    JPG 不支持深度 2。它总是输出到深度 8,并且量化会添加更多颜色。尝试使用带有 -depth 2 的 PNG 或 GIF 或 TIFF 的命令。这行得通吗?

    convert -size 256x256 gradient: -depth 2 grad_d2.jpg
    
      Depth: 8-bit
      Colors: 10
      Histogram:
         10752: (  0,  0,  0) #000000 gray(0)
           256: (  1,  1,  1) #010101 gray(1)
           512: ( 84, 84, 84) #545454 gray(84)
         20992: ( 85, 85, 85) #555555 gray(85)
           256: ( 86, 86, 86) #565656 gray(86)
           256: (169,169,169) #A9A9A9 gray(169)
         21248: (170,170,170) #AAAAAA gray(170)
           256: (171,171,171) #ABABAB gray(171)
           256: (254,254,254) #FEFEFE gray(254)
         10752: (255,255,255) #FFFFFF gray(255)
    
    
    convert -size 256x256 gradient: -depth 2 grad_d2.png
    
      Depth: 8/2-bit
      Colors: 4
      Histogram:
         11008: (  0,  0,  0) #000000 gray(0)
         21760: ( 85, 85, 85) #555555 gray(85)
         21760: (170,170,170) #AAAAAA gray(170)
         11008: (255,255,255) #FFFFFF gray(255)
    

    也许 Wand 有错误,或者您使用的版本太旧?生成的图像与 JPG 或 PNG 看起来应该相似,但 JPG 将具有更相似的颜色。

    【讨论】:

    • python -m wand.version 0.3.8 版本:ImageMagick 7.0.7-22 Q16 x86_64 2018-02-08
    • 魔杖在 0.4.4。你可以试试升级。 ImageMagick 在 7.0.8.11 Q16,但您的版本似乎足够新并且可以正常工作。当我将您的图像处理为 jpg 和 png 时,它们看起来很相似。但是 JPG 有 103 种颜色和 256 种颜色的颜色图和深度 8。PNG 有 2 种颜色和 4 种颜色的颜色图。
    • 好的,我更新到 0.4.4 并添加了 img.format = 'png' 现在它的工作就像一个魅力
    • 它现在也适用于 jpg 吗?它应该看起来相似,但只是从 jpg 压缩中获得了更接近的颜色。
    • 这对我来说似乎是 Wand 中的一个错误。但我不是魔杖专家。我建议您将此问题发布给 Wand 开发人员。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    相关资源
    最近更新 更多