I'm attempting to recreate the following command using wand-py and imagemagick:
convert -density 500 hello_world.pdf -quality 100 -monochrome -enhance – morphology close diamond hello_world.jpg
You'll need to bind methods from MagickCore, and MagickWand libraries. Keep in mind that Kernel performance can differ widely between systems, so don't be surprised if you hit a `Operation canceled' message when working with large density images.
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')