如何将 Imagemagick 转换为 Wand?

how convert Imagemagick to Wand?

我正在做一个电子墨水项目,屏幕分辨率为1404x1872,灰度为16色。我正在尝试将命令下方的 Imagemagick 转换为 Wand:

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

我有点得到了最终结果,但我不确定最终图像是否与命令生成的图像相同,而且我不得不计算 canvas 的中心而不是使用重心属性,我尝试了一切并接受了这个解决方案。

有人可以帮助解决这两个问题吗?

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

尝试以下...

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

备注:

  • -white-threshold 3000% 已被省略 - 因为任何高于 100% 的值都会执行空操作,并且不会改变任何像素。
  • 方法 library.MagickSetDepth 直接从 CDLL 库中使用,因为 img.depth 在图像上设置了 属性 -- 它忽略了小于 8 的值。
  • 编码器协议 BMP3: 用于在保存期间强制写入编码器。
  • treedepth=4大概可以省略。
  • 如果我没记错的话,Image.extent() 方法有一个 gravity= kwargs,但它最近才在 Wand 的 0.6.8(未)发布中引入。