pythonimagemagickimagemagick-convertwand

how convert Imagemagick to Wand?


I am doing a e-ink project, the screen has 1404x1872 resolution and 16 colors grayscale. I am trying to convert Imagemagick below command to Wand:

convert image.jpg -gravity center -background black -extent 1404x1872 
-type Grayscale -white-threshold 3000% +dither -colors 16 -depth 4 BMP3:test.bmp

I kinda got the final result, but I am unsure if the final image will be the same as generated by the command, also I had to calculate the center of the canvas instead of use gravity center attribute, I tried everything and settle for this solution.

Can someone help on those two issues?

from wand.image import Image

canvas_width = 1404
canvas_height = 1872

with Image(filename= 'image.jpg') as img:
    top = int((canvas_width / 2 - img.width / 2) * -1)
    left = int((canvas_height / 2 - img.height / 2) * -1)
    img.type = 'grayscale'
    img.quantize(number_colors= 16, dither= True)
    img.depth = 4
    img.extent(canvas_width,canvas_height, top, left)
    #img.gravity = 'center'
    img.white_threshold = 3000

    img.save(filename='test.bmp');


Solution

  • Try the following...

    from wand.image import Image
    from wand.api import library
    
    canvas_width = 1404
    canvas_height = 1872
    
    with Image(filename='image.jpg') as img:
        # -background black
        img.background_color = 'black'
        # -gravity center -extent 1404x1872 
        img.extent(width=canvas_width,
                   height=canvas_height,
                   left=img.width // 2 - canvas_width // 2,
                   top=img.height // 2 - canvas_height // 2)
        # -type Grayscale
        img.type = 'grayscale'
        # +dither -colors 16
        img.quantize(number_colors=16, dither=False, treedepth=4)
        # -depth 4 
        library.MagickSetDepth(img.wand, 4)
        img.save(filename='BMP3:test.bmp')
    

    notes: