将image magick命令转换为魔杖式代码

时间:2019-07-23 10:15:26

标签: python imagemagick wand

我正在尝试使用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命令的帮助。

谢谢。

1 个答案:

答案 0 :(得分:2)

  

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

真正的关闭。大概可以减少一些重复代码。 (...而且我正以图像格式为重,以减少复杂性...)

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')

只是一个建议。