如何通过Python Wand接口访问不受支持的Wand API?例如,我希望调用Wand API MagickAddNoiseImage,但它在Python接口中不可用。
发布于 2015-01-17 21:42:18
使用wand.api访问不受支持的API非常容易,但您需要打开ImageMagick的文档/头文件以供参考。
from wand.api import library
import ctypes
# Re-create NoiseType enum
NOISE_TYPES = ('undefined', 'uniform', 'gaussian', 'multiplicative_gaussian',
'impulse', 'laplacian', 'poisson', 'random')
# Map API i/o
library.MagickAddNoiseImage.argtypes = [ctypes.c_void_p,
ctypes.c_uint]
library.MagickAddNoiseImage.restype = ctypes.c_int
# Extend wand's Image class with your new API
from wand.image import Image
class MySupportedImage(Image):
def add_noise(self, noise_type):
"""My MagickAddNoiseImage"""
if noise_type not in NOISE_TYPES:
self.raise_exception()
return library.MagickAddNoiseImage.argtypes(self.resource,
NOISE_TYPES.index(noise_type))如果你的解决方案成功了,考虑提交你的解决方案back to the community (在你创建了一个可靠的单元测试之后)。
https://stackoverflow.com/questions/21522094
复制相似问题