Jabb
Jabb

Reputation: 3502

How to convert this ImageMagick command for white background removal to Python Wand module?

I am referring to @fmw42 and his wonderful ImageMagic command to turn a white background to a transparent background. I modified his original command to work with the latest version of ImageMagick.

magick test_imagemagick.jpg -fuzz 25% -fill none -draw "alpha 0,0 floodfill" -channel alpha -blur 0x1 -level 50x100% +channel  result.png

This is great on the command line, but I am struggling with understanding how to implement the same in Python Wand.

This is what I have so far which is not much because I have no idea how to map the info from both documentation.s

with Image(filename= 'test_imagemagick.jpg') as img:
   img.fuzz = 0.25 * QUANTUM_RANGE  # 25%
   img.fill_color = 'transparent'

Upvotes: 0

Views: 190

Answers (1)

fmw42
fmw42

Reputation: 53089

Try this command in Python/Wand:

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

with Image(filename='logo:') as img:
    with img.clone() as copied:
        copied.fuzz = 0.25 * img.quantum_range
        with Drawing() as draw:
            draw.fill_color = Color('transparent')
            draw.matte(x=0.0, y=0.0, paint_method='floodfill')
            draw(copied)
        copied.alpha_channel = 'extract'
        copied.blur(radius=0.0, sigma=2)
        copied.level(black=0.5, white=1, gamma=1.0)
        img.composite(copied, left=0, top=0, operator='copy_opacity')
        img.format='png'
        display(img)
        img.save(filename='logo_transparent_antialiased.png')

enter image description here

Upvotes: 1

Related Questions