anon
anon

Reputation:

Convert image magick command to wand-py code

I'm trying to achieve following imagemagick command in pyhon using wand-py

my original convert command is

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

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

Any help appreciated on converting above command to wand-py python command.

Thanks.

Upvotes: 2

Views: 417

Answers (1)

emcconville
emcconville

Reputation: 24419

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

Real close. Can probably reduce some repetitions code. (... and I'm taking liberty with the image formats to reduce complexity ...)

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

However, to match the CLI given, I believe the second clone is just attempting to create a composite mask of 50%. Might be able to further simplify it by colorize to a temporary image, then blend it back on the source.

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

Just a suggestion.

Upvotes: 3

Related Questions