【问题标题】:How can I run this command using wand-py and imagemagick如何使用 wand-py 和 imagemagick 运行此命令
【发布时间】:2017-03-13 20:20:30
【问题描述】:

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

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

【问题讨论】:

    标签: python imagemagick-convert wand


    【解决方案1】:

    您需要绑定来自 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')
    

    【讨论】:

    • 感谢代码!我在这一行收到ArgumentError: argument 1: <class 'TypeError'>: wrong type 错误:kernel = libmagick.AcquireKernelInfo('Diamond'),如果有帮助,我正在使用版本 ImageMagick-6.9.8-Q8 和 Wand 0.4.4。
    • 嗯。尝试使用create_string_buffer。我将更新示例
    • 类似错误:TypeError: Diamond,来自此模块C:\Users\xxxxx\AppData\Local\Continuum\Anaconda3\lib\ctypes\__init__.py in create_string_buffer(init, size) 在这一行p = ctypes.create_string_buffer('Diamond')。再次感谢您的帮助!
    • 啊。可能与 Python 版本有关。尝试为 python 字符串添加一个字节的前缀。 b'Diamond'。使用您的 python here 版本仔细检查文档(最后一个示例)。祝你好运!
    • 是的,我使用的是 Python 3,应该把它放在第一条消息中。 b'Diamond 完美运行。您的原始代码建议也有效。非常感谢!
    猜你喜欢
    • 2015-02-18
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多