gpt4 book ai didi

python - 如何使用 wand-py 和 imagemagick 运行此命令

转载 作者:行者123 更新时间:2023-12-01 03:12:14 26 4
gpt4 key购买 nike

我正在尝试使用 wand-py 和 imagemagick 重新创建以下命令:

convert -density 500 hello_world.pdf -quality 100 -monochrome -enhance – morphology close diamond hello_world.jpg

最佳答案

您需要绑定(bind) MagickCore 和 MagickWand 库中的方法。请记住,系统之间的内核性能可能存在很大差异,因此,如果您在处理大密度图像时遇到“操作已取消”消息,请不要感到惊讶。

import ctypes
from wand.api import libmagick, library
from wand.image import Image

"""
Kernel info methods on MagickCore library.
"""
libmagick.AcquireKernelInfo.argtypes = (ctypes.c_char_p,)
libmagick.AcquireKernelInfo.restype = ctypes.c_void_p
libmagick.DestroyKernelInfo.argtypes = (ctypes.c_void_p,)
libmagick.DestroyKernelInfo.restype = ctypes.c_void_p

"""
Morphology method on MagickWand library.
"""
library.MagickMorphologyImage.argtypes = (ctypes.c_void_p, # wand
ctypes.c_int, # method
ctypes.c_long, # iterations
ctypes.c_void_p) # kernel
"""
Enhance method on MagickWand library.
"""
library.MagickEnhanceImage.argtypes = (ctypes.c_void_p,) # wand

# convert -density 500 hello_world.pdf
with Image(filename='pdf-sample.pdf', resolution=500) as img:
# -quality 100
img.compression_quality = 100
# -monochrome
img.quantize(2, # Target colors
'gray', # Colorspace
1, # Treedepth
False, # No Dither
False) # Quantization error
# -enhance
library.MagickEnhanceImage(img.wand)
# -morphology close diamond
p = ctypes.create_string_buffer(b'Diamond')
kernel = libmagick.AcquireKernelInfo(p)
CloseMorphology = 9 # See `morphology.h'
library.MagickMorphologyImage(img.wand, CloseMorphology, 1, kernel)
kernel = libmagick.DestroyKernelInfo(kernel) # Free memory
# hello_world.jpg
img.save(filename='hello_world.jpg')

关于python - 如何使用 wand-py 和 imagemagick 运行此命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42772714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com