简体   繁体   中英

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

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:

  • The -white-threshold 3000% has been omitted - as any value above 100% would perform a no-op, and not alter any pixels.
  • Th method library.MagickSetDepth is used directly from the CDLL library, as img.depth sets a property on the image -- which ignores values < 8.
  • The coder protocol BMP3: is used to enforce the write encoder during save.
  • treedepth=4 can probably be omitted.
  • If I remember correctly, the Image.extent() method has a gravity= kwargs, but that was only recently introduced in Wand's 0.6.8 (un)release.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM