【问题标题】:Writing command line argument for Wand为 Wand 编写命令行参数
【发布时间】:2016-05-12 16:41:21
【问题描述】:

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

convert me.jpg -fill none -fuzz 1% -draw 'matte 0,0 floodfill' -flop  -draw 'matte 0,0 floodfill' -flop me.png

它基本上允许我从我的前景图像中减去一个已知的背景图像,并具有一定的容差(即模糊)。我想用纯色填充生成的区域。

我想在内存中使用我的 Python 脚本来完成所有这些操作,以避免将我解码的 RAW 图像数据作为 jpeg 重新压缩两次。通过命令行执行此操作会强制执行额外的压缩步骤,在脚本中执行此操作只需保存一个 jpeg。

我们将不胜感激。

编辑:更正,与我尝试过的其他东西混淆了。上述命令不会从已知图像中减去。这实际上取了左上角的像素,并从那里用 1% 的模糊填充填充。很明显,当您可以看到只有一个输入图像时。仍然知道如何将上述内容转换为 Python 仍然很有帮助。

【问题讨论】:

  • 创建一个命令字符串,然后使用import osos.system(command_string)。例如,command_string = "convert me.jpg -fill none -fuzz 1% -draw 'matte 0,0 floodfill' -flop -draw 'matte 0,0 floodfill' -flop me.png"。可能有更好的方法,但我发现自己经常这样做
  • 感谢@dermen,但我已经在使用它,这意味着保存文件两次。我需要在内存中执行所有操作以避免重新压缩,尤其是因为我可能需要做的不仅仅是上面的命令。

标签: python imagemagick wand


【解决方案1】:

你可以试试这样的。

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

with Image(filename='me.jpg') as img:
    with Drawing() as ctx:
        # -fill none
        ctx.fill_color = Color('transparent')
        #  -draw 'matte 0,0 floodfill'
        ctx.matte(x=0, y=0, paint_method='floodfill')
        ctx(img)
        # -flop
        img.flop()
        ctx(img)
        img.flop()
    img.save(filename='me.png')

对于-fuzz 1%,您需要做一些额外的工作来计算量子值。

from wand.color import Color
from wand.drawing import Drawing
from wand.image import Image
import ctypes
library.MagickSetImageFuzz.argtypes = (ctypes.c_void_p,
                                       ctypes.c_double)
with Image(filename='me.jpg') as img:
    # -fuzz 1%
    library.MagickSetImageFuzz(img.wand, img.quantum_range * 0.1)
    with Drawing() as ctx:
        # -fill none
        ctx.fill_color = Color('transparent')
        #  -draw 'matte 0,0 floodfill'
        ctx.matte(x=0, y=0, paint_method='floodfill')
        # -flop -draw 'matte 0,0 floodfill' -flop
        ctx.matte(x=img.width - 1, y=0, paint_method='floodfill')
        ctx(img)
    img.save(filename='me.png')

【讨论】:

  • 非常感谢您尝试这个。第一个示例运行但似乎没有添加 alpha。第二个例子在第五行出错了。
  • 你运行的是什么版本的 IM?
猜你喜欢
  • 2023-03-25
  • 2012-07-09
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 2015-12-08
  • 2022-01-10
  • 1970-01-01
  • 2018-10-14
相关资源
最近更新 更多