【问题标题】:Accessing unsupported APIs with wand-py?使用 wand-py 访问不受支持的 API?
【发布时间】:2014-02-03 07:46:57
【问题描述】:

如何通过 Python Wand 接口访问不受支持的 Wand API?例如,我希望调用 Wand API MagickAddNoiseImage,但它在 Python 接口中不可用。

【问题讨论】:

    标签: magickwand wand


    【解决方案1】:

    使用wand.api 访问不受支持的 API 非常简单,但您需要打开 ImageMagick 的 docs/header 文件以供参考。

    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(在您创建可靠的单元测试之后。)

    【讨论】:

      猜你喜欢
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多