将图像 magick 命令转换为 wand-py 代码

Convert image magick command to wand-py code

我正在尝试使用 wand-py

在 pyhon 中实现以下 imagemagick 命令

我原来的转换命令是

convert ./img_1.png   ( -clone 0 -colorspace SRGB -resize 1x1! -resize 569x380\! -modulate 100,100,0 ) ( -clone 0 -fill gray(50%) -colorize 100 ) -compose colorize -composite -colorspace sRGB -auto-level media/color-cast-1-out-1.jpeg

我正在尝试使用 wand-py 创建两个克隆,如下所示,是正确的还是我应该只做一个克隆?

with Image(filename='media/img1.jpeg') as original:
    size = original.size
    with original.convert('png') as converted:
        # creating temp miff file

        # 1st clone
        with converted.clone() as firstClone:
            firstClone.resize(1, 1)
            firstClone.transform_colorspace('srgb')
            firstClone.modulate(100, 100, 0)
            firstClone.resize(size[0], size[1])
            firstClone.format = 'jpeg'
            firstClone.save(filename='media/img-1-clone-1.jpeg')

        # 2nd clone
        with converted.clone() as secondClone:
            with Drawing() as draw:
                draw.fill_color = 'gray'
                draw.fill_opacity = 0.5
                draw.draw(secondClone)
            secondClone.format = 'jpeg'
            secondClone.save(filename='media/img-1-clone-2.jpeg')

感谢任何将上述命令转换为 wand-py python 命令的帮助。

谢谢。

i'm trying to create two clones using wand-py like below, is it right or should I only do only one clone?

真正接近。大概可以减少一些重复代码。 (...我随意使用图像格式以降低复杂性...)

with Image(filename='input.png') as img:
    with img.clone() as clone1:
        clone1.transform_colorspace('srgb')
        clone1.resize(1, 1)
        clone1.resize(*img.size)
        clone1.modulate(100, 100, 0)
        clone1.save(filename='clone1.png')
    with img.clone() as clone2:
        clone2.colorize(color='gray50', alpha='#FFFFFF')
        clone2.save(filename='clone2.png')

但是,为了匹配给定的 CLI,我相信第二个克隆只是试图创建一个 50% 的复合掩码。或许可以通过 colorize 将其进一步简化为临时图像,然后 blend 将其放回到源文件中。

with Image(filename='input.png') as img:
    with img.clone() as clone1:
        clone1.transform_colorspace('srgb')
        clone1.resize(1, 1)
        clone1.resize(*img.size)
        clone1.modulate(100, 100, 0)
        with img.clone() as temp:
            temp.composite(clone1, operator='colorize')
            img.composite(temp, operator='blend', arguments='50,50')
    img.auto_level()
    img.save(filename='output.png')

只是一个建议。