首页 > 解决方案 > 将 ImageMagick 命令转换为 Wand Python

问题描述

我想知道如何使用 Wand 库将这个工作命令行序列从 ImageMagick 转换为 Python 脚本:

/usr/local/bin/convert pic.png -alpha off +dither -colors 2 -colorspace gray -normalize -statistic median 1x200 -negate -format "%[fx:mean*w*h]" info:

我当前的 Python 代码是:

with Image(filename= 'pic.png') as img:
    with img.clone() as img_copy1:
        img_copy1.alpha_channel = False
        img_copy1.quantize(number_colors= 2, colorspace_type='gray', dither= True)
        img_copy1.normalize()
        img_copy1.negate(grayscale= True)

但我仍然不知道如何计算作为垂直特征一部分的像素数......

更新:

感谢@fmw42,我修改了我的代码

with wand.image.Image(filename='pic.png') as img:
    with img.clone() as img_copy1:
        img_copy1.alpha_channel = False
        img_copy1.quantize(number_colors= 2, colorspace_type='gray', dither= True)
        img_copy1.normalize()
        img_copy1.statistic("median", width = 1, height = 200)
        img_copy1.negate()
        img.options['format'] = '%[fx:mean*w*h]'
        print(img.make_blob('INFO'))

标签: pythonimagemagickimagemagick-convertwand

解决方案


这是我从 Eric McConville(Wand 开发人员)那里得到的一个例子,用于进行 fx: 计算。这应该给你一个线索如何做你的。

from wand.image import Image
with Image(filename='zelda.png') as img:
  img.options['format'] = '%wx%h'
  print(img.make_blob('INFO'))

如果现在有更好的方法,Eric 可以进一步评论。


推荐阅读