Commit 8973e81a authored by Cervinko Cera's avatar Cervinko Cera

added Wand 4.2 to lib

parent 8be85c8e
""":mod:`wand` --- Simple `MagickWand API`_ binding for Python
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. _MagickWand API: http://www.imagemagick.org/script/magick-wand.php
"""
""":mod:`wand.api` --- Low-level interfaces
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. versionchanged:: 0.1.10
Changed to throw :exc:`~exceptions.ImportError` instead of
:exc:`~exceptions.AttributeError` when the shared library fails to load.
"""
import ctypes
import ctypes.util
import itertools
import os
import os.path
import platform
import sys
import traceback
if platform.system() == "Windows":
try:
import winreg
except ImportError:
import _winreg as winreg
__all__ = ('MagickPixelPacket', 'PointInfo', 'AffineMatrix', 'c_magick_char_p',
'library', 'libc', 'libmagick', 'load_library')
class c_magick_char_p(ctypes.c_char_p):
"""This subclass prevents the automatic conversion behavior of
:class:`ctypes.c_char_p`, allowing memory to be properly freed in the
destructor. It must only be used for non-const character pointers
returned by ImageMagick functions.
"""
def __del__(self):
"""Relinquishes memory allocated by ImageMagick.
We don't need to worry about checking for ``NULL`` because
:c:func:`MagickRelinquishMemory` does that for us.
Note alslo that :class:`ctypes.c_char_p` has no
:meth:`~object.__del__` method, so we don't need to
(and indeed can't) call the superclass destructor.
"""
library.MagickRelinquishMemory(self)
def library_paths():
"""Iterates for library paths to try loading. The result paths are not
guaranteed that they exist.
:returns: a pair of libwand and libmagick paths. they can be the same.
path can be ``None`` as well
:rtype: :class:`tuple`
"""
libwand = None
libmagick = None
versions = '', '-6', '-Q16', '-Q8', '-6.Q16'
options = '', 'HDRI', 'HDRI-2'
system = platform.system()
magick_home = os.environ.get('MAGICK_HOME')
if system == 'Windows':
# ImageMagick installers normally install coder and filter DLLs in
# subfolders, we need to add those folders to PATH, otherwise loading
# the DLL later will fail.
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
r"SOFTWARE\ImageMagick\Current") as reg_key:
libPath = winreg.QueryValueEx(reg_key, "LibPath")
coderPath = winreg.QueryValueEx(reg_key, "CoderModulesPath")
filterPath = winreg.QueryValueEx(reg_key, "FilterModulesPath")
magick_home = libPath[0]
os.environ['PATH'] += (';' + libPath[0] + ";" +
coderPath[0] + ";" + filterPath[0])
except OSError:
# otherwise use MAGICK_HOME, and we assume the coder and
# filter DLLs are in the same directory
pass
magick_path = lambda dir: os.path.join(magick_home, *dir)
combinations = itertools.product(versions, options)
for suffix in (version + option for version, option in combinations):
# On Windows, the API is split between two libs. On other platforms,
# it's all contained in one.
if magick_home:
if system == 'Windows':
libwand = 'CORE_RL_wand_{0}.dll'.format(suffix),
libmagick = 'CORE_RL_magick_{0}.dll'.format(suffix),
yield magick_path(libwand), magick_path(libmagick)
libwand = 'libMagickWand{0}.dll'.format(suffix),
libmagick = 'libMagickCore{0}.dll'.format(suffix),
yield magick_path(libwand), magick_path(libmagick)
elif system == 'Darwin':
libwand = 'lib', 'libMagickWand{0}.dylib'.format(suffix),
yield magick_path(libwand), magick_path(libwand)
else:
libwand = 'lib', 'libMagickWand{0}.so'.format(suffix),
yield magick_path(libwand), magick_path(libwand)
if system == 'Windows':
libwand = ctypes.util.find_library('CORE_RL_wand_' + suffix)
libmagick = ctypes.util.find_library('CORE_RL_magick_' + suffix)
yield libwand, libmagick
libwand = ctypes.util.find_library('libMagickWand' + suffix)
libmagick = ctypes.util.find_library('libMagickCore' + suffix)
yield libwand, libmagick
else:
libwand = ctypes.util.find_library('MagickWand' + suffix)
yield libwand, libwand
def load_library():
"""Loads the MagickWand library.
:returns: the MagickWand library and the ImageMagick library
:rtype: :class:`ctypes.CDLL`
"""
tried_paths = []
for libwand_path, libmagick_path in library_paths():
if libwand_path is None or libmagick_path is None:
continue
try:
tried_paths.append(libwand_path)
libwand = ctypes.CDLL(libwand_path)
if libwand_path == libmagick_path:
libmagick = libwand
else:
tried_paths.append(libmagick_path)
libmagick = ctypes.CDLL(libmagick_path)
except (IOError, OSError):
continue
return libwand, libmagick
raise IOError('cannot find library; tried paths: ' + repr(tried_paths))
if not hasattr(ctypes, 'c_ssize_t'):
if ctypes.sizeof(ctypes.c_uint) == ctypes.sizeof(ctypes.c_void_p):
ctypes.c_ssize_t = ctypes.c_int
elif ctypes.sizeof(ctypes.c_ulong) == ctypes.sizeof(ctypes.c_void_p):
ctypes.c_ssize_t = ctypes.c_long
elif ctypes.sizeof(ctypes.c_ulonglong) == ctypes.sizeof(ctypes.c_void_p):
ctypes.c_ssize_t = ctypes.c_longlong
class MagickPixelPacket(ctypes.Structure):
_fields_ = [('storage_class', ctypes.c_int),
('colorspace', ctypes.c_int),
('matte', ctypes.c_int),
('fuzz', ctypes.c_double),
('depth', ctypes.c_size_t),
('red', ctypes.c_double),
('green', ctypes.c_double),
('blue', ctypes.c_double),
('opacity', ctypes.c_double),
('index', ctypes.c_double)]
class PointInfo(ctypes.Structure):
_fields_ = [('x', ctypes.c_double),
('y', ctypes.c_double)]
class AffineMatrix(ctypes.Structure):
_fields_ = [('sx', ctypes.c_double),
('rx', ctypes.c_double),
('ry', ctypes.c_double),
('sy', ctypes.c_double),
('tx', ctypes.c_double),
('ty', ctypes.c_double)]
# Preserve the module itself even if it fails to import
sys.modules['wand._api'] = sys.modules['wand.api']
try:
libraries = load_library()
except (OSError, IOError):
msg = 'http://docs.wand-py.org/en/latest/guide/install.html'
if sys.platform.startswith('freebsd'):
msg = 'pkg_add -r'
elif sys.platform == 'win32':
msg += '#install-imagemagick-on-windows'
elif sys.platform == 'darwin':
mac_pkgmgrs = {'brew': 'brew install freetype imagemagick',
'port': 'port install imagemagick'}
for pkgmgr in mac_pkgmgrs:
with os.popen('which ' + pkgmgr) as f:
if f.read().strip():
msg = mac_pkgmgrs[pkgmgr]
break
else:
msg += '#install-imagemagick-on-mac'
else:
distname, _, __ = platform.linux_distribution()
distname = (distname or '').lower()
if distname in ('debian', 'ubuntu'):
msg = 'apt-get install libmagickwand-dev'
elif distname in ('fedora', 'centos', 'redhat'):
msg = 'yum install ImageMagick-devel'
raise ImportError('MagickWand shared library not found.\n'
'You probably had not installed ImageMagick library.\n'
'Try to install:\n ' + msg)
#: (:class:`ctypes.CDLL`) The MagickWand library.
library = libraries[0]
#: (:class:`ctypes.CDLL`) The ImageMagick library. It is the same with
#: :data:`library` on platforms other than Windows.
#:
#: .. versionadded:: 0.1.10
libmagick = libraries[1]
try:
library.MagickWandGenesis.argtypes = []
library.MagickWandTerminus.argtypes = []
library.NewMagickWand.argtypes = []
library.NewMagickWand.restype = ctypes.c_void_p
library.MagickNewImage.argtypes = [ctypes.c_void_p, ctypes.c_int,
ctypes.c_int, ctypes.c_void_p]
library.ClearMagickWand.argtypes = [ctypes.c_void_p]
library.DestroyMagickWand.argtypes = [ctypes.c_void_p]
library.DestroyMagickWand.restype = ctypes.c_void_p
library.CloneMagickWand.argtypes = [ctypes.c_void_p]
library.CloneMagickWand.restype = ctypes.c_void_p
library.IsMagickWand.argtypes = [ctypes.c_void_p]
library.MagickGetException.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_int)]
library.MagickGetException.restype = c_magick_char_p
library.MagickClearException.argtypes = [ctypes.c_void_p]
library.MagickSetFilename.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
library.MagickReadImageBlob.argtypes = [ctypes.c_void_p, ctypes.c_void_p,
ctypes.c_size_t]
library.MagickReadImage.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
library.MagickReadImageFile.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
library.MagickGetImageFormat.argtypes = [ctypes.c_void_p]
library.MagickGetImageFormat.restype = c_magick_char_p
library.MagickSetImageFormat.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
libmagick.MagickToMime.argtypes = [ctypes.c_char_p]
libmagick.MagickToMime.restype = c_magick_char_p
library.MagickGetImageSignature.argtypes = [ctypes.c_void_p]
library.MagickGetImageSignature.restype = c_magick_char_p
library.MagickGetImageProperty.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
library.MagickGetImageProperty.restype = c_magick_char_p
library.MagickGetImageProperties.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.POINTER(ctypes.c_size_t)
]
library.MagickGetImageProperties.restype = ctypes.POINTER(ctypes.c_char_p)
library.MagickSetImageProperty.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p]
library.MagickDeleteImageProperty.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
library.MagickGetImageBackgroundColor.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
library.MagickSetImageBackgroundColor.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
library.MagickSetImageMatte.argtypes = [ctypes.c_void_p, ctypes.c_int]
library.MagickGetImageMatteColor.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
library.MagickSetImageMatteColor.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
library.MagickGetImageAlphaChannel.argtypes = [ctypes.c_void_p]
library.MagickGetImageAlphaChannel.restype = ctypes.c_size_t
library.MagickSetImageAlphaChannel.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.MagickGetImageBlob.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_size_t)]
library.MagickGetImageBlob.restype = ctypes.POINTER(ctypes.c_ubyte)
library.MagickGetImagesBlob.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_size_t)]
library.MagickGetImagesBlob.restype = ctypes.POINTER(ctypes.c_ubyte)
library.MagickWriteImage.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
library.MagickWriteImageFile.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
library.MagickWriteImages.argtypes = [ctypes.c_void_p, ctypes.c_char_p,
ctypes.c_int]
library.MagickWriteImagesFile.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
library.MagickGetImageResolution.argtypes = [
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double)
]
library.MagickSetImageResolution.argtypes = [ctypes.c_void_p,
ctypes.c_double,
ctypes.c_double]
library.MagickSetResolution.argtypes = [ctypes.c_void_p, ctypes.c_double,
ctypes.c_double]
library.MagickGetImageWidth.argtypes = [ctypes.c_void_p]
library.MagickGetImageWidth.restype = ctypes.c_size_t
library.MagickGetImageHeight.argtypes = [ctypes.c_void_p]
library.MagickGetImageHeight.restype = ctypes.c_size_t
library.MagickGetImageOrientation.argtypes = [ctypes.c_void_p]
library.MagickGetImageOrientation.restype = ctypes.c_int
library.MagickSetImageOrientation.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.MagickGetImageUnits.argtypes = [ctypes.c_void_p]
library.MagickSetImageUnits.argtypes = [ctypes.c_void_p, ctypes.c_int]
library.MagickGetImageVirtualPixelMethod.argtypes = [ctypes.c_void_p]
library.MagickSetImageVirtualPixelMethod.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.MagickGetImageColorspace.argtypes = [ctypes.c_void_p]
library.MagickGetImageColorspace.restype = ctypes.c_int
library.MagickSetImageColorspace.argtypes = [ctypes.c_void_p, ctypes.c_int]
library.MagickTransformImageColorspace.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.MagickGetImageCompression.argtypes = [ctypes.c_void_p]
library.MagickGetImageCompression.restype = ctypes.c_int
library.MagickSetImageCompression.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.MagickGetImageDepth.argtypes = [ctypes.c_void_p]
library.MagickGetImageDepth.restype = ctypes.c_size_t
library.MagickSetImageDepth.argtypes = [ctypes.c_void_p]
library.MagickGetImageChannelDepth.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.MagickGetImageChannelDepth.restype = ctypes.c_size_t
library.MagickSeparateImageChannel.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.MagickCropImage.argtypes = [ctypes.c_void_p, ctypes.c_size_t,
ctypes.c_size_t, ctypes.c_ssize_t,
ctypes.c_ssize_t]
library.MagickFlipImage.argtypes = [ctypes.c_void_p]
library.MagickFlopImage.argtypes = [ctypes.c_void_p]
library.MagickFrameImage.argtypes = [ctypes.c_void_p, # wand
ctypes.c_void_p, # matte_color
ctypes.c_size_t, # width
ctypes.c_size_t, # height
ctypes.c_ssize_t, # inner_bevel
ctypes.c_ssize_t] # outer_bevel
library.MagickFunctionImage.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_int, # MagickFunction
ctypes.c_size_t, # number_arguments
ctypes.POINTER(ctypes.c_double), # arguments
]
library.MagickFunctionImageChannel.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_int, # channel
ctypes.c_int, # MagickFunction
ctypes.c_size_t, # number_arguments
ctypes.POINTER(ctypes.c_double), # arguments
]
library.MagickFxImage.argtypes = [ctypes.c_void_p, # wand
ctypes.c_char_p] # expression
library.MagickFxImage.restype = ctypes.c_void_p
library.MagickFxImageChannel.argtypes = [ctypes.c_void_p, # wand
ctypes.c_int, # channel
ctypes.c_char_p] # expression
library.MagickFxImageChannel.restype = ctypes.c_void_p
library.MagickResetImagePage.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
library.MagickSampleImage.argtypes = [ctypes.c_void_p, ctypes.c_size_t,
ctypes.c_size_t]
library.MagickResizeImage.argtypes = [ctypes.c_void_p, ctypes.c_size_t,
ctypes.c_size_t, ctypes.c_int,
ctypes.c_double]
library.MagickTransformImage.argtypes = [ctypes.c_void_p, ctypes.c_char_p,
ctypes.c_char_p]
library.MagickTransformImage.restype = ctypes.c_void_p
library.MagickTransparentPaintImage.argtypes = [
ctypes.c_void_p, ctypes.c_void_p, ctypes.c_double, ctypes.c_double,
ctypes.c_int
]
library.MagickLiquidRescaleImage.argtypes = [
ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t,
ctypes.c_double, ctypes.c_double
]
library.MagickRotateImage.argtypes = [ctypes.c_void_p, ctypes.c_void_p,
ctypes.c_double]
library.MagickBorderImage.argtypes = [ctypes.c_void_p, ctypes.c_void_p,
ctypes.c_size_t, ctypes.c_size_t]
library.MagickResetIterator.argtypes = [ctypes.c_void_p]
library.MagickSetLastIterator.argtypes = [ctypes.c_void_p]
library.MagickGetIteratorIndex.argtypes = [ctypes.c_void_p]
library.MagickGetIteratorIndex.restype = ctypes.c_ssize_t
library.MagickCoalesceImages.argtypes = [ctypes.c_void_p]
library.MagickCoalesceImages.restype = ctypes.c_void_p
library.MagickIdentifyImage.argtypes = [ctypes.c_void_p]
library.MagickIdentifyImage.restype = ctypes.c_char_p
library.MagickRelinquishMemory.argtypes = [ctypes.c_void_p]
library.MagickRelinquishMemory.restype = ctypes.c_void_p
library.NewPixelIterator.argtypes = [ctypes.c_void_p]
library.NewPixelIterator.restype = ctypes.c_void_p
library.DestroyPixelIterator.argtypes = [ctypes.c_void_p]
library.DestroyPixelIterator.restype = ctypes.c_void_p
library.ClonePixelIterator.argtypes = [ctypes.c_void_p]
library.ClonePixelIterator.restype = ctypes.c_void_p
library.IsPixelIterator.argtypes = [ctypes.c_void_p]
library.PixelGetIteratorException.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_int)]
library.PixelGetIteratorException.restype = c_magick_char_p
library.PixelClearIteratorException.argtypes = [ctypes.c_void_p]
library.PixelSetFirstIteratorRow.argtypes = [ctypes.c_void_p]
library.PixelSetIteratorRow.argtypes = [ctypes.c_void_p, ctypes.c_ssize_t]
library.PixelGetNextIteratorRow.argtypes = [
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_size_t)
]
library.PixelGetNextIteratorRow.restype = ctypes.POINTER(ctypes.c_void_p)
library.NewPixelWand.argtypes = []
library.NewPixelWand.restype = ctypes.c_void_p
library.DestroyPixelWand.argtypes = [ctypes.c_void_p]
library.DestroyPixelWand.restype = ctypes.c_void_p
library.IsPixelWand.argtypes = [ctypes.c_void_p]
library.PixelGetException.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_int)]
library.PixelGetException.restype = c_magick_char_p
library.PixelClearException.argtypes = [ctypes.c_void_p]
library.IsPixelWandSimilar.argtypes = [ctypes.c_void_p, ctypes.c_void_p,
ctypes.c_double]
library.PixelGetMagickColor.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
library.PixelSetMagickColor.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
library.PixelSetColor.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
library.PixelGetColorAsString.argtypes = [ctypes.c_void_p]
library.PixelGetColorAsString.restype = c_magick_char_p
library.PixelGetColorAsNormalizedString.argtypes = [ctypes.c_void_p]
library.PixelGetColorAsNormalizedString.restype = c_magick_char_p
library.PixelGetRed.argtypes = [ctypes.c_void_p]
library.PixelGetRed.restype = ctypes.c_double
library.PixelGetGreen.argtypes = [ctypes.c_void_p]
library.PixelGetGreen.restype = ctypes.c_double
library.PixelGetBlue.argtypes = [ctypes.c_void_p]
library.PixelGetBlue.restype = ctypes.c_double
library.PixelGetAlpha.argtypes = [ctypes.c_void_p]
library.PixelGetAlpha.restype = ctypes.c_double
library.PixelGetRedQuantum.argtypes = [ctypes.c_void_p]
library.PixelGetRedQuantum.restype = ctypes.c_size_t
library.PixelGetGreenQuantum.argtypes = [ctypes.c_void_p]
library.PixelGetGreenQuantum.restype = ctypes.c_size_t
library.PixelGetBlueQuantum.argtypes = [ctypes.c_void_p]
library.PixelGetBlueQuantum.restype = ctypes.c_size_t
library.PixelGetAlphaQuantum.argtypes = [ctypes.c_void_p]
library.PixelGetAlphaQuantum.restype = ctypes.c_size_t
library.PixelGetColorCount.argtypes = [ctypes.c_void_p]
library.PixelGetColorCount.restype = ctypes.c_size_t
library.MagickGetQuantumRange.argtypes = [ctypes.POINTER(ctypes.c_size_t)]
library.MagickSetIteratorIndex.argtypes = [ctypes.c_void_p,
ctypes.c_ssize_t]
library.MagickGetImageType.argtypes = [ctypes.c_void_p]
library.MagickSetImageType.argtypes = [ctypes.c_void_p, ctypes.c_int]
library.MagickEvaluateImage.argtypes = [ctypes.c_void_p,
ctypes.c_int,
ctypes.c_double]
library.MagickLevelImage.argtypes = [ctypes.c_void_p,
ctypes.c_double,
ctypes.c_double,
ctypes.c_double]
library.MagickLevelImageChannel.argtypes = [ctypes.c_void_p,
ctypes.c_int,
ctypes.c_double,
ctypes.c_double,
ctypes.c_double]
library.MagickEvaluateImageChannel.argtypes = [ctypes.c_void_p,
ctypes.c_int,
ctypes.c_int,
ctypes.c_double]
library.MagickContrastStretchImage.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # black
ctypes.c_double] # white
library.MagickContrastStretchImageChannel.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_int, # channel
ctypes.c_double, # black
ctypes.c_double, # white
]
library.MagickGammaImage.argtypes = [ctypes.c_void_p,
ctypes.c_double]
library.MagickGammaImageChannel.argtypes = [ctypes.c_void_p,
ctypes.c_int,
ctypes.c_double]
library.MagickLinearStretchImage.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # black
ctypes.c_double] # white
library.MagickCompositeImage.argtypes = [ctypes.c_void_p, ctypes.c_void_p,
ctypes.c_int, ctypes.c_ssize_t,
ctypes.c_ssize_t]
library.MagickCompositeImageChannel.argtypes = [
ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p,
ctypes.c_int, ctypes.c_ssize_t, ctypes.c_ssize_t
]
library.MagickGetImageCompressionQuality.argtypes = [ctypes.c_void_p]
library.MagickGetImageCompressionQuality.restype = ctypes.c_ssize_t
library.MagickSetImageCompressionQuality.argtypes = [ctypes.c_void_p,
ctypes.c_ssize_t]
library.MagickStripImage.argtypes = [ctypes.c_void_p]
library.MagickTrimImage.argtypes = [ctypes.c_void_p,
ctypes.c_double]
library.MagickGaussianBlurImage.argtypes = [ctypes.c_void_p,
ctypes.c_double,
ctypes.c_double]
library.MagickUnsharpMaskImage.argtypes = [ctypes.c_void_p,
ctypes.c_double,
ctypes.c_double,
ctypes.c_double,
ctypes.c_double]
library.MagickGetNumberImages.argtypes = [ctypes.c_void_p]
library.MagickGetNumberImages.restype = ctypes.c_size_t
library.MagickGetIteratorIndex.argtypes = [ctypes.c_void_p]
library.MagickGetIteratorIndex.restype = ctypes.c_size_t
library.MagickSetIteratorIndex.argtypes = [ctypes.c_void_p,
ctypes.c_ssize_t]
library.MagickSetFirstIterator.argtypes = [ctypes.c_void_p]
library.MagickSetLastIterator.argtypes = [ctypes.c_void_p]
library.MagickAddImage.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
library.MagickRemoveImage.argtypes = [ctypes.c_void_p]
libmagick.GetNextImageInList.argtypes = [ctypes.c_void_p]
libmagick.GetNextImageInList.restype = ctypes.c_void_p
library.MagickGetImageDelay.argtypes = [ctypes.c_void_p]
library.MagickGetImageDelay.restype = ctypes.c_ssize_t
library.MagickSetImageDelay.argtypes = [ctypes.c_void_p, ctypes.c_ssize_t]
library.NewMagickWandFromImage.argtypes = [ctypes.c_void_p]
library.NewMagickWandFromImage.restype = ctypes.c_void_p
library.GetImageFromMagickWand.argtypes = [ctypes.c_void_p]
library.GetImageFromMagickWand.restype = ctypes.c_void_p
libmagick.CloneImages.argtypes = [ctypes.c_void_p, ctypes.c_char_p,
ctypes.c_void_p]
libmagick.CloneImages.restype = ctypes.c_void_p
libmagick.AcquireExceptionInfo.argtypes = []
libmagick.AcquireExceptionInfo.restype = ctypes.c_void_p
libmagick.DestroyExceptionInfo.argtypes = [ctypes.c_void_p]
libmagick.DestroyExceptionInfo.restype = ctypes.c_void_p
libmagick.DestroyImage.argtypes = [ctypes.c_void_p]
libmagick.DestroyImage.restype = ctypes.c_void_p
library.MagickGetSize.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_uint),
ctypes.POINTER(ctypes.c_uint)]
library.MagickGetSize.restype = ctypes.c_int
library.MagickSetSize.argtypes = [ctypes.c_void_p,
ctypes.c_uint,
ctypes.c_uint]
library.MagickSetSize.restype = ctypes.c_int
library.MagickSetDepth.argtypes = [ctypes.c_void_p,
ctypes.c_uint]
library.MagickSetDepth.restype = ctypes.c_int
library.MagickSetFormat.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
library.MagickSetFormat.restype = ctypes.c_int
library.MagickGetFont.argtypes = [ctypes.c_void_p]
library.MagickGetFont.restype = ctypes.c_char_p
library.MagickSetFont.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
library.MagickSetFont.restype = ctypes.c_int
library.MagickGetPointsize.argtypes = [ctypes.c_void_p]
library.MagickGetPointsize.restype = ctypes.c_double
library.MagickSetPointsize.argtypes = [ctypes.c_void_p,
ctypes.c_double]
library.MagickSetPointsize.restype = ctypes.c_int
library.MagickGetGravity.argtypes = [ctypes.c_void_p]
library.MagickGetGravity.restype = ctypes.c_int
library.MagickSetGravity.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.MagickSetGravity.restype = ctypes.c_int
library.MagickSetLastIterator.argtypes = [ctypes.c_void_p]
library.MagickGetBackgroundColor.argtypes = [ctypes.c_void_p]
library.MagickGetBackgroundColor.restype = ctypes.c_void_p
library.MagickSetBackgroundColor.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
library.MagickSetBackgroundColor.restype = ctypes.c_int
library.MagickGetOption.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
library.MagickGetOption.restype = ctypes.c_char_p
library.MagickSetOption.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p]
library.MagickSetOption.restype = ctypes.c_int
library.MagickDeleteOption.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
library.MagickDeleteOption.restype = ctypes.c_int
library.MagickGetAntialias.argtypes = [ctypes.c_void_p]
library.MagickGetAntialias.restype = ctypes.c_int
library.MagickSetAntialias.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.MagickSetAntialias.restype = ctypes.c_int
library.MagickGetImageHistogram.argtypes = [
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_size_t)
]
library.MagickGetImageHistogram.restype = ctypes.POINTER(ctypes.c_void_p)
# These functions are const so it's okay for them to be c_char_p
libmagick.GetMagickVersion.argtypes = [ctypes.POINTER(ctypes.c_size_t)]
libmagick.GetMagickVersion.restype = ctypes.c_char_p
libmagick.GetMagickReleaseDate.argtypes = []
libmagick.GetMagickReleaseDate.restype = ctypes.c_char_p
libmagick.GetMagickQuantumDepth.argtypes = [
ctypes.POINTER(ctypes.c_size_t)
]
libmagick.GetMagickQuantumDepth.restype = ctypes.c_char_p
library.NewDrawingWand.restype = ctypes.c_void_p
library.CloneDrawingWand.argtypes = [ctypes.c_void_p]
library.CloneDrawingWand.restype = ctypes.c_void_p
library.DestroyDrawingWand.argtypes = [ctypes.c_void_p]
library.DestroyDrawingWand.restype = ctypes.c_void_p
library.IsDrawingWand.argtypes = [ctypes.c_void_p]
library.IsDrawingWand.restype = ctypes.c_int
library.DrawGetException.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_int)]
library.DrawGetException.restype = ctypes.c_char_p
library.DrawClearException.argtypes = [ctypes.c_void_p]
library.DrawClearException.restype = ctypes.c_int
library.DrawAffine.argtypes = [
ctypes.c_void_p, # Drawing wand
ctypes.POINTER(AffineMatrix), # AffineMatrix
]
library.DrawComment.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_char_p, # comment
]
library.DrawComposite.argtypes = [
ctypes.c_void_p, # DrawingWand wand
ctypes.c_int, # CompositeOperator
ctypes.c_double, # x
ctypes.c_double, # y
ctypes.c_double, # width
ctypes.c_double, # height
ctypes.c_void_p, # MagickWand wand
]
library.DrawComposite.restype = ctypes.c_uint
library.DrawSetBorderColor.argtypes = [ctypes.c_void_p, # wand
ctypes.c_void_p] # PixelWand color
library.DrawSetClipPath.argtypes = [ctypes.c_void_p, # wand
ctypes.c_char_p] # clip_mask
library.DrawSetClipPath.restype = ctypes.c_int
library.DrawSetClipRule.argtypes = [ctypes.c_void_p, # wand
ctypes.c_uint] # FillRule
library.DrawSetClipUnits.argtypes = [ctypes.c_void_p, # wand
ctypes.c_uint] # ClipPathUnits
library.DrawSetFont.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
library.DrawSetFontFamily.argtypes = [ctypes.c_void_p, # wand
ctypes.c_char_p] # font_family
library.DrawSetFontFamily.restype = ctypes.c_uint
library.DrawSetFontResolution.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x
ctypes.c_double] # y
library.DrawSetFontResolution.restype = ctypes.c_uint
library.DrawSetFontSize.argtypes = [ctypes.c_void_p,
ctypes.c_double]
library.DrawSetFontStretch.argtypes = [ctypes.c_void_p, # wand
ctypes.c_int] # font_stretch
library.DrawSetFontStyle.argtypes = [ctypes.c_void_p, # wand
ctypes.c_int] # style
library.DrawSetFontWeight.argtypes = [ctypes.c_void_p, # wand
ctypes.c_size_t] # font_weight
library.DrawSetFillColor.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
library.DrawSetFillOpacity.argtypes = [ctypes.c_void_p,
ctypes.c_double]
library.DrawSetFillPatternURL.argtypes = [ctypes.c_void_p, # wand
ctypes.c_char_p] # fill_url
library.DrawSetFillPatternURL.restype = ctypes.c_uint
library.DrawSetFillRule.argtypes = [ctypes.c_void_p,
ctypes.c_uint]
library.DrawSetOpacity.argtypes = [ctypes.c_void_p, ctypes.c_double]
library.DrawSetStrokeAntialias.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_int, # stroke_antialias
]
library.DrawSetStrokeColor.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
library.DrawSetStrokeDashArray.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_size_t, # number_elements
ctypes.POINTER(ctypes.c_double),
]
library.DrawSetStrokeDashOffset.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_double, # dash_offset
]
library.DrawSetStrokeLineCap.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_int, # linecap
]
library.DrawSetStrokeLineJoin.argtypes = [ctypes.c_void_p, # wand
ctypes.c_int] # linejoin
library.DrawSetStrokeMiterLimit.argtypes = [ctypes.c_void_p, # wand
ctypes.c_size_t] # miterlimit
library.DrawSetStrokeOpacity.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double] # stroke_opacity
library.DrawSetStrokePatternURL.argtypes = [ctypes.c_void_p, # wand
ctypes.c_char_p] # fill_url
library.DrawSetStrokePatternURL.restype = ctypes.c_uint
library.DrawSetStrokeWidth.argtypes = [ctypes.c_void_p,
ctypes.c_double]
library.DrawSetTextAlignment.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.DrawSetTextAntialias.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.DrawSetTextDecoration.argtypes = [ctypes.c_void_p,
ctypes.c_int]
try:
library.DrawSetTextDirection.argtypes = [ctypes.c_void_p,
ctypes.c_int]
except AttributeError:
library.DrawSetTextDirection = None
library.DrawSetTextEncoding.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
try:
library.DrawSetTextInterlineSpacing.argtypes = [ctypes.c_void_p,
ctypes.c_double]
except AttributeError:
library.DrawSetTextInterlineSpacing = None
library.DrawSetTextInterwordSpacing.argtypes = [ctypes.c_void_p,
ctypes.c_double]
library.DrawSetTextKerning.argtypes = [ctypes.c_void_p,
ctypes.c_double]
library.DrawSetTextUnderColor.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
library.DrawSetVectorGraphics.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
library.DrawSetVectorGraphics.restype = ctypes.c_int
library.DrawResetVectorGraphics.argtypes = [ctypes.c_void_p]
library.DrawSetViewbox.argtypes = [ctypes.c_void_p, # wand
ctypes.c_ssize_t, # x1
ctypes.c_ssize_t, # y1
ctypes.c_ssize_t, # x2
ctypes.c_ssize_t] # y2
library.DrawGetBorderColor.argtypes = [ctypes.c_void_p, # wand
ctypes.c_void_p] # PixelWand color
library.DrawGetClipPath.argtypes = [ctypes.c_void_p]
library.DrawGetClipPath.restype = c_magick_char_p
library.DrawGetClipRule.argtypes = [ctypes.c_void_p]
library.DrawGetClipRule.restype = ctypes.c_uint
library.DrawGetClipUnits.argtypes = [ctypes.c_void_p]
library.DrawGetClipUnits.restype = ctypes.c_uint
library.DrawGetFillColor.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
library.DrawGetFillOpacity.argtypes = [ctypes.c_void_p]
library.DrawGetFillOpacity.restype = ctypes.c_double
library.DrawGetFillRule.argtypes = [ctypes.c_void_p]
library.DrawGetFillRule.restype = ctypes.c_uint
library.DrawGetOpacity.argtypes = [ctypes.c_void_p]
library.DrawGetOpacity.restype = ctypes.c_double
library.DrawGetStrokeAntialias.argtypes = [ctypes.c_void_p]
library.DrawGetStrokeAntialias.restype = ctypes.c_int
library.DrawGetStrokeColor.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
library.DrawGetStrokeDashArray.argtypes = [
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_size_t),
]
library.DrawGetStrokeDashArray.restype = ctypes.POINTER(ctypes.c_double)
library.DrawGetStrokeDashOffset.argtypes = [ctypes.c_void_p]
library.DrawGetStrokeDashOffset.restype = ctypes.c_double
library.DrawGetStrokeLineCap.argtypes = [ctypes.c_void_p]
library.DrawGetStrokeLineCap.restype = ctypes.c_int
library.DrawGetStrokeLineJoin.argtypes = [ctypes.c_void_p]
library.DrawGetStrokeLineJoin.restype = ctypes.c_int
library.DrawGetStrokeMiterLimit.argtypes = [ctypes.c_void_p]
library.DrawGetStrokeMiterLimit.restype = ctypes.c_size_t
library.DrawGetStrokeOpacity.argtypes = [ctypes.c_void_p]
library.DrawGetStrokeOpacity.restype = ctypes.c_double
library.DrawGetStrokeWidth.argtypes = [ctypes.c_void_p]
library.DrawGetStrokeWidth.restype = ctypes.c_double
library.DrawGetFont.argtypes = [ctypes.c_void_p]
library.DrawGetFont.restype = c_magick_char_p
library.DrawGetFontFamily.argtypes = [ctypes.c_void_p]
library.DrawGetFontFamily.restype = c_magick_char_p
library.DrawGetFontResolution.argtypes = [
ctypes.c_void_p, # wand
ctypes.POINTER(ctypes.c_double), # x
ctypes.POINTER(ctypes.c_double), # y
]
library.DrawGetFontResolution.restype = ctypes.c_uint
library.DrawGetFontSize.argtypes = [ctypes.c_void_p]
library.DrawGetFontSize.restype = ctypes.c_double
library.DrawGetFontStyle.argtypes = [ctypes.c_void_p]
library.DrawGetFontStyle.restype = ctypes.c_int
library.DrawGetFontWeight.argtypes = [ctypes.c_void_p]
library.DrawGetFontWeight.restype = ctypes.c_size_t
library.DrawGetFontStretch.argtypes = [ctypes.c_void_p]
library.DrawGetFontStretch.restype = ctypes.c_int
library.DrawGetTextAlignment.argtypes = [ctypes.c_void_p]
library.DrawGetTextAlignment.restype = ctypes.c_int
library.DrawGetTextAntialias.argtypes = [ctypes.c_void_p]
library.DrawGetTextAntialias.restype = ctypes.c_int
library.DrawGetTextDecoration.argtypes = [ctypes.c_void_p]
library.DrawGetTextDecoration.restype = ctypes.c_int
try:
library.DrawGetTextDirection.argtypes = [ctypes.c_void_p]
library.DrawGetTextDirection.restype = ctypes.c_int
except AttributeError:
library.DrawGetTextDirection = None
library.DrawGetTextEncoding.argtypes = [ctypes.c_void_p]
library.DrawGetTextEncoding.restype = c_magick_char_p
try:
library.DrawGetTextInterlineSpacing.argtypes = [ctypes.c_void_p]
library.DrawGetTextInterlineSpacing.restype = ctypes.c_double
except AttributeError:
library.DrawGetTextInterlineSpacing = None
library.DrawGetTextInterwordSpacing.argtypes = [ctypes.c_void_p]
library.DrawGetTextInterwordSpacing.restype = ctypes.c_double
library.DrawGetTextKerning.argtypes = [ctypes.c_void_p]
library.DrawGetTextKerning.restype = ctypes.c_double
library.DrawGetTextUnderColor.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
library.DrawGetVectorGraphics.argtypes = [ctypes.c_void_p]
library.DrawGetVectorGraphics.restype = c_magick_char_p
library.DrawSetGravity.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.DrawGetGravity.argtypes = [ctypes.c_void_p]
library.DrawGetGravity.restype = ctypes.c_int
library.MagickAnnotateImage.argtypes = [ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_double,
ctypes.c_double,
ctypes.c_double,
ctypes.c_char_p]
library.MagickAnnotateImage.restype = ctypes.c_int
library.MagickDistortImage.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_int, # method
ctypes.c_size_t, # number_arguments
ctypes.POINTER(ctypes.c_double), # arguments
ctypes.c_int, # bestfit
]
library.MagickDistortImage.restype = ctypes.c_int
library.ClearDrawingWand.argtypes = [ctypes.c_void_p]
library.MagickDrawImage.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
library.MagickDrawImage.restype = ctypes.c_int
library.DrawAnnotation.argtypes = [ctypes.c_void_p,
ctypes.c_double,
ctypes.c_double,
ctypes.POINTER(ctypes.c_ubyte)]
library.DrawArc.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # sx
ctypes.c_double, # sy
ctypes.c_double, # ex
ctypes.c_double, # ey
ctypes.c_double, # sd
ctypes.c_double] # ed
library.DrawBezier.argtypes = [ctypes.c_void_p,
ctypes.c_ulong,
ctypes.POINTER(PointInfo)]
library.DrawCircle.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # ox
ctypes.c_double, # oy
ctypes.c_double, # px
ctypes.c_double] # py
library.DrawColor.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x
ctypes.c_double, # y
ctypes.c_uint] # PaintMethod
library.DrawEllipse.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # ox
ctypes.c_double, # oy
ctypes.c_double, # rx
ctypes.c_double, # ry
ctypes.c_double, # start
ctypes.c_double] # end
library.DrawLine.argtypes = [ctypes.c_void_p,
ctypes.c_double,
ctypes.c_double,
ctypes.c_double,
ctypes.c_double]
library.DrawMatte.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x
ctypes.c_double, # y
ctypes.c_uint] # PaintMethod
library.DrawPathClose.argtypes = [ctypes.c_void_p] # wand
library.DrawPathCurveToAbsolute.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x1
ctypes.c_double, # y1
ctypes.c_double, # x2
ctypes.c_double, # y2
ctypes.c_double, # x
ctypes.c_double] # y
library.DrawPathCurveToRelative.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x1
ctypes.c_double, # y1
ctypes.c_double, # x2
ctypes.c_double, # y2
ctypes.c_double, # x
ctypes.c_double] # y
library.DrawPathCurveToQuadraticBezierAbsolute.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_double, # x1
ctypes.c_double, # y1
ctypes.c_double, # x
ctypes.c_double, # y
]
library.DrawPathCurveToQuadraticBezierRelative.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_double, # x1
ctypes.c_double, # y1
ctypes.c_double, # x
ctypes.c_double, # y
]
library.DrawPathCurveToQuadraticBezierSmoothAbsolute.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_double, # x
ctypes.c_double, # y
]
library.DrawPathCurveToQuadraticBezierSmoothRelative.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_double, # x
ctypes.c_double, # y
]
library.DrawPathCurveToSmoothAbsolute.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x2
ctypes.c_double, # y2
ctypes.c_double, # x
ctypes.c_double] # y
library.DrawPathCurveToSmoothRelative.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x2
ctypes.c_double, # y2
ctypes.c_double, # x
ctypes.c_double] # y
library.DrawPathEllipticArcAbsolute.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_double, # rx
ctypes.c_double, # ry
ctypes.c_double, # rotation
ctypes.c_uint, # arc_flag
ctypes.c_uint, # sweep_flag
ctypes.c_double, # x
ctypes.c_double, # y
]
library.DrawPathEllipticArcRelative.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_double, # rx
ctypes.c_double, # ry
ctypes.c_double, # rotation
ctypes.c_uint, # arc_flag
ctypes.c_uint, # sweep_flag
ctypes.c_double, # x
ctypes.c_double, # y
]
library.DrawPathFinish.argtypes = [ctypes.c_void_p] # wand
library.DrawPathLineToAbsolute.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x
ctypes.c_double] # y
library.DrawPathLineToRelative.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x
ctypes.c_double] # y
library.DrawPathLineToHorizontalAbsolute.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_double, # x
]
library.DrawPathLineToHorizontalRelative.argtypes = [
ctypes.c_void_p, # wand
ctypes.c_double, # x
]
library.DrawPathLineToVerticalAbsolute.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double] # y
library.DrawPathLineToVerticalRelative.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double] # y
library.DrawPathMoveToAbsolute.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x
ctypes.c_double] # y
library.DrawPathMoveToRelative.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x
ctypes.c_double] # y
library.DrawPathStart.argtypes = [ctypes.c_void_p] # wand
library.DrawPoint.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x
ctypes.c_double] # y
library.DrawPolygon.argtypes = [ctypes.c_void_p,
ctypes.c_ulong,
ctypes.POINTER(PointInfo)]
library.DrawPolyline.argtypes = [ctypes.c_void_p,
ctypes.c_ulong,
ctypes.POINTER(PointInfo)]
library.DrawRotate.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double] # degree
library.DrawRectangle.argtypes = [ctypes.c_void_p,
ctypes.c_double,
ctypes.c_double,
ctypes.c_double,
ctypes.c_double]
library.DrawRoundRectangle.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x1
ctypes.c_double, # y1
ctypes.c_double, # x2
ctypes.c_double, # y2
ctypes.c_double, # rx
ctypes.c_double] # ry
library.DrawScale.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x
ctypes.c_double] # y
library.DrawSkewX.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double] # degree
library.DrawSkewY.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double] # degree
library.DrawTranslate.argtypes = [ctypes.c_void_p, # wand
ctypes.c_double, # x
ctypes.c_double] # y
# -- Drawing stack management --
library.PushDrawingWand.argtypes = [ctypes.c_void_p]
library.PushDrawingWand.restype = ctypes.c_uint
library.DrawPushClipPath.argtypes = [ctypes.c_void_p, # wand
ctypes.c_char_p] # clip_mask_id
library.DrawPushDefs.argtypes = [ctypes.c_void_p]
library.DrawPushPattern.argtypes = [ctypes.c_void_p, # wand
ctypes.c_char_p, # clip_mask_id
ctypes.c_double, # x
ctypes.c_double, # y
ctypes.c_double, # width
ctypes.c_double] # height
library.DrawPushClipPath.restype = ctypes.c_uint
library.PopDrawingWand.argtypes = [ctypes.c_void_p]
library.PopDrawingWand.restype = ctypes.c_uint
library.DrawPopClipPath.argtypes = [ctypes.c_void_p]
library.DrawPopDefs.argtypes = [ctypes.c_void_p]
library.DrawPopPattern.argtypes = [ctypes.c_void_p]
library.MagickNegateImage.argtypes = [ctypes.c_void_p, ctypes.c_int]
library.MagickNegateImageChannel.argtypes = [ctypes.c_void_p,
ctypes.c_int,
ctypes.c_int]
library.MagickNormalizeImage.argtypes = [ctypes.c_void_p]
library.MagickNormalizeImageChannel.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.MagickEqualizeImage.argtypes = [ctypes.c_void_p]
library.MagickQueryConfigureOption.argtypes = [ctypes.c_char_p]
library.MagickQueryConfigureOption.restype = c_magick_char_p
library.MagickQueryConfigureOptions.argtypes = [
ctypes.c_char_p,
ctypes.POINTER(ctypes.c_size_t),
]
library.MagickQueryConfigureOptions.restype = \
ctypes.POINTER(c_magick_char_p)
library.MagickQueryFontMetrics.argtypes = [ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_char_p]
library.MagickQueryFontMetrics.restype = ctypes.POINTER(ctypes.c_double)
library.MagickQueryFonts.argtypes = [ctypes.c_char_p,
ctypes.POINTER(ctypes.c_size_t)]
library.MagickQueryFonts.restype = ctypes.POINTER(c_magick_char_p)
library.MagickQueryFormats.argtypes = [ctypes.c_char_p,
ctypes.POINTER(ctypes.c_size_t)]
library.MagickQueryFormats.restype = ctypes.POINTER(c_magick_char_p)
library.MagickQueryMultilineFontMetrics.argtypes = [ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_char_p]
library.MagickQueryMultilineFontMetrics.restype = ctypes.POINTER(
ctypes.c_double
)
library.MagickThresholdImage.argtypes = [ctypes.c_void_p, ctypes.c_double]
library.MagickThresholdImageChannel.argtypes = [ctypes.c_void_p,
ctypes.c_int,
ctypes.c_double]
library.MagickModulateImage.argtypes = [ctypes.c_void_p,
ctypes.c_double,
ctypes.c_double,
ctypes.c_double]
library.MagickAppendImages.argtypes = [ctypes.c_void_p,
ctypes.c_int]
library.MagickAppendImages.restype = ctypes.c_void_p
library.MagickTransposeImage.argtypes = [ctypes.c_void_p]
library.MagickTransverseImage.argtypes = [ctypes.c_void_p]
library.MagickQuantizeImage.argtypes = [ctypes.c_void_p,
ctypes.c_int,
ctypes.c_int,
ctypes.c_int,
ctypes.c_bool,
ctypes.c_bool]
except AttributeError:
raise ImportError('MagickWand shared library not found or incompatible\n'
'Original exception was raised in:\n' +
traceback.format_exc())
try:
library.MagickAutoOrientImage.argtypes = [ctypes.c_void_p]
except AttributeError:
# MagickAutoOrientImage was added in 6.8.9+, we have a fallback function
# so we pass silently if we cant import it
pass
#: (:class:`ctypes.CDLL`) The C standard library.
libc = None
if platform.system() == 'Windows':
msvcrt = ctypes.util.find_msvcrt()
# workaround -- the newest visual studio DLL is named differently:
if not msvcrt and "1900" in platform.python_compiler():
msvcrt = "vcruntime140.dll"
if msvcrt:
libc = ctypes.CDLL(msvcrt)
else:
if platform.system() == 'Darwin':
libc = ctypes.cdll.LoadLibrary('libc.dylib')
elif platform.system() == 'FreeBSD':
libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c'))
else:
libc = ctypes.cdll.LoadLibrary('libc.so.6')
libc.fdopen.argtypes = [ctypes.c_int, ctypes.c_char_p]
libc.fdopen.restype = ctypes.c_void_p
libc.fflush.argtypes = [ctypes.c_void_p]
""":mod:`wand.color` --- Colors
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. versionadded:: 0.1.2
"""
import ctypes
from .api import MagickPixelPacket, library
from .compat import binary, text
from .resource import Resource
from .version import QUANTUM_DEPTH
__all__ = 'Color', 'scale_quantum_to_int8'
class Color(Resource):
"""Color value.
Unlike any other objects in Wand, its resource management can be
implicit when it used outside of :keyword:`with` block. In these case,
its resource are allocated for every operation which requires a resource
and destroyed immediately. Of course it is inefficient when the
operations are much, so to avoid it, you should use color objects
inside of :keyword:`with` block explicitly e.g.::
red_count = 0
with Color('#f00') as red:
with Image(filename='image.png') as img:
for row in img:
for col in row:
if col == red:
red_count += 1
:param string: a color namel string e.g. ``'rgb(255, 255, 255)'``,
``'#fff'``, ``'white'``. see `ImageMagick Color Names`_
doc also
:type string: :class:`basestring`
.. versionchanged:: 0.3.0
:class:`Color` objects become hashable.
.. seealso::
`ImageMagick Color Names`_
The color can then be given as a color name (there is a limited
but large set of these; see below) or it can be given as a set
of numbers (in decimal or hexadecimal), each corresponding to
a channel in an RGB or RGBA color model. HSL, HSLA, HSB, HSBA,
CMYK, or CMYKA color models may also be specified. These topics
are briefly described in the sections below.
.. _ImageMagick Color Names: http://www.imagemagick.org/script/color.php
.. describe:: == (other)
Equality operator.
:param other: a color another one
:type color: :class:`Color`
:returns: ``True`` only if two images equal.
:rtype: :class:`bool`
"""
c_is_resource = library.IsPixelWand
c_destroy_resource = library.DestroyPixelWand
c_get_exception = library.PixelGetException
c_clear_exception = library.PixelClearException
__slots__ = 'raw', 'c_resource', 'allocated'
def __init__(self, string=None, raw=None):
if (string is None and raw is None or
string is not None and raw is not None):
raise TypeError('expected one argument')
self.allocated = 0
if raw is None:
self.raw = ctypes.create_string_buffer(
ctypes.sizeof(MagickPixelPacket)
)
with self:
library.PixelSetColor(self.resource, binary(string))
library.PixelGetMagickColor(self.resource, self.raw)
else:
self.raw = raw
def __getinitargs__(self):
return self.string, None
def __enter__(self):
if not self.allocated:
with self.allocate():
self.resource = library.NewPixelWand()
library.PixelSetMagickColor(self.resource, self.raw)
self.allocated += 1
return Resource.__enter__(self)
def __exit__(self, type, value, traceback):
self.allocated -= 1
if not self.allocated:
Resource.__exit__(self, type, value, traceback)
@property
def string(self):
"""(:class:`basestring`) The string representation of the color."""
with self:
color_string = library.PixelGetColorAsString(self.resource)
return text(color_string.value)
@property
def normalized_string(self):
"""(:class:`basestring`) The normalized string representation of
the color. The same color is always represented to the same
string.
.. versionadded:: 0.3.0
"""
with self:
string = library.PixelGetColorAsNormalizedString(self.resource)
return text(string.value)
@staticmethod
def c_equals(a, b):
"""Raw level version of equality test function for two pixels.
:param a: a pointer to PixelWand to compare
:type a: :class:`ctypes.c_void_p`
:param b: a pointer to PixelWand to compare
:type b: :class:`ctypes.c_void_p`
:returns: ``True`` only if two pixels equal
:rtype: :class:`bool`
.. note::
It's only for internal use. Don't use it directly.
Use ``==`` operator of :class:`Color` instead.
"""
alpha = library.PixelGetAlpha
return bool(library.IsPixelWandSimilar(a, b, 0) and
alpha(a) == alpha(b))
def __eq__(self, other):
if not isinstance(other, Color):
return False
with self as this:
with other:
return self.c_equals(this.resource, other.resource)
def __ne__(self, other):
return not (self == other)
def __hash__(self):
if self.alpha:
return hash(self.normalized_string)
return hash(None)
@property
def red(self):
"""(:class:`numbers.Real`) Red, from 0.0 to 1.0."""
with self:
return library.PixelGetRed(self.resource)
@property
def green(self):
"""(:class:`numbers.Real`) Green, from 0.0 to 1.0."""
with self:
return library.PixelGetGreen(self.resource)
@property
def blue(self):
"""(:class:`numbers.Real`) Blue, from 0.0 to 1.0."""
with self:
return library.PixelGetBlue(self.resource)
@property
def alpha(self):
"""(:class:`numbers.Real`) Alpha value, from 0.0 to 1.0."""
with self:
return library.PixelGetAlpha(self.resource)
@property
def red_quantum(self):
"""(:class:`numbers.Integral`) Red.
Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.
.. versionadded:: 0.3.0
"""
with self:
return library.PixelGetRedQuantum(self.resource)
@property
def green_quantum(self):
"""(:class:`numbers.Integral`) Green.
Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.
.. versionadded:: 0.3.0
"""
with self:
return library.PixelGetGreenQuantum(self.resource)
@property
def blue_quantum(self):
"""(:class:`numbers.Integral`) Blue.
Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.
.. versionadded:: 0.3.0
"""
with self:
return library.PixelGetBlueQuantum(self.resource)
@property
def alpha_quantum(self):
"""(:class:`numbers.Integral`) Alpha value.
Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.
.. versionadded:: 0.3.0
"""
with self:
return library.PixelGetAlphaQuantum(self.resource)
@property
def red_int8(self):
"""(:class:`numbers.Integral`) Red as 8bit integer which is a common
style. From 0 to 255.
.. versionadded:: 0.3.0
"""
return scale_quantum_to_int8(self.red_quantum)
@property
def green_int8(self):
"""(:class:`numbers.Integral`) Green as 8bit integer which is
a common style. From 0 to 255.
.. versionadded:: 0.3.0
"""
return scale_quantum_to_int8(self.green_quantum)
@property
def blue_int8(self):
"""(:class:`numbers.Integral`) Blue as 8bit integer which is
a common style. From 0 to 255.
.. versionadded:: 0.3.0
"""
return scale_quantum_to_int8(self.blue_quantum)
@property
def alpha_int8(self):
"""(:class:`numbers.Integral`) Alpha value as 8bit integer which is
a common style. From 0 to 255.
.. versionadded:: 0.3.0
"""
return scale_quantum_to_int8(self.alpha_quantum)
def __str__(self):
return self.string
def __repr__(self):
c = type(self)
return '{0}.{1}({2!r})'.format(c.__module__, c.__name__, self.string)
def _repr_html_(self):
html = """
<span style="background-color:#{red:02X}{green:02X}{blue:02X};
display:inline-block;
line-height:1em;
width:1em;">&nbsp;</span>
<strong>#{red:02X}{green:02X}{blue:02X}</strong>
"""
return html.format(red=self.red_int8,
green=self.green_int8,
blue=self.blue_int8)
def scale_quantum_to_int8(quantum):
"""Straightforward port of :c:func:`ScaleQuantumToChar()` inline
function.
:param quantum: quantum value
:type quantum: :class:`numbers.Integral`
:returns: 8bit integer of the given ``quantum`` value
:rtype: :class:`numbers.Integral`
.. versionadded:: 0.3.0
"""
if quantum <= 0:
return 0
table = {8: 1, 16: 257.0, 32: 16843009.0, 64: 72340172838076673.0}
v = quantum / table[QUANTUM_DEPTH]
if v >= 255:
return 255
return int(v + 0.5)
""":mod:`wand.compat` --- Compatibility layer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This module provides several subtle things to support
multiple Python versions (2.6, 2.7, 3.2--3.5) and VM implementations
(CPython, PyPy).
"""
import contextlib
import io
import sys
import types
__all__ = ('PY3', 'binary', 'binary_type', 'encode_filename', 'file_types',
'nested', 'string_type', 'text', 'text_type', 'xrange')
#: (:class:`bool`) Whether it is Python 3.x or not.
PY3 = sys.version_info >= (3,)
#: (:class:`type`) Type for representing binary data. :class:`str` in Python 2
#: and :class:`bytes` in Python 3.
binary_type = bytes if PY3 else str
#: (:class:`type`) Type for text data. :class:`basestring` in Python 2
#: and :class:`str` in Python 3.
string_type = str if PY3 else basestring # noqa
#: (:class:`type`) Type for representing Unicode textual data.
#: :class:`unicode` in Python 2 and :class:`str` in Python 3.
text_type = str if PY3 else unicode # noqa
def binary(string, var=None):
"""Makes ``string`` to :class:`str` in Python 2.
Makes ``string`` to :class:`bytes` in Python 3.
:param string: a string to cast it to :data:`binary_type`
:type string: :class:`bytes`, :class:`str`, :class:`unicode`
:param var: an optional variable name to be used for error message
:type var: :class:`str`
"""
if isinstance(string, text_type):
return string.encode()
elif isinstance(string, binary_type):
return string
if var:
raise TypeError('{0} must be a string, not {1!r}'.format(var, string))
raise TypeError('expected a string, not ' + repr(string))
if PY3:
def text(string):
if isinstance(string, bytes):
return string.decode('utf-8')
return string
else:
def text(string):
"""Makes ``string`` to :class:`str` in Python 3.
Does nothing in Python 2.
:param string: a string to cast it to :data:`text_type`
:type string: :class:`bytes`, :class:`str`, :class:`unicode`
"""
return string
#: The :func:`xrange()` function. Alias for :func:`range()` in Python 3.
xrange = range if PY3 else xrange # noqa
#: (:class:`type`, :class:`tuple`) Types for file objects that have
#: ``fileno()``.
file_types = io.RawIOBase if PY3 else (io.RawIOBase, types.FileType)
def encode_filename(filename):
"""If ``filename`` is a :data:`text_type`, encode it to
:data:`binary_type` according to filesystem's default encoding.
"""
if isinstance(filename, text_type):
return filename.encode(sys.getfilesystemencoding())
return filename
try:
nested = contextlib.nested
except AttributeError:
# http://hg.python.org/cpython/file/v2.7.6/Lib/contextlib.py#l88
@contextlib.contextmanager
def nested(*managers):
exits = []
vars = []
exc = (None, None, None)
try:
for mgr in managers:
exit = mgr.__exit__
enter = mgr.__enter__
vars.append(enter())
exits.append(exit)
yield vars
except:
exc = sys.exc_info()
finally:
while exits:
exit = exits.pop()
try:
if exit(*exc):
exc = (None, None, None)
except:
exc = sys.exc_info()
if exc != (None, None, None):
# PEP 3109
e = exc[0](exc[1])
e.__traceback__ = e[2]
raise e
""":mod:`wand.display` --- Displaying images
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The :func:`display()` functions shows you the image. It is useful for
debugging.
If you are in Mac, the image will be opened by your default image application
(:program:`Preview.app` usually).
If you are in Windows, the image will be opened by :program:`imdisplay.exe`,
or your default image application (:program:`Windows Photo Viewer` usually)
if :program:`imdisplay.exe` is unavailable.
You can use it from CLI also. Execute :mod:`wand.display` module through
:option:`python -m` option:
.. sourcecode:: console
$ python -m wand.display wandtests/assets/mona-lisa.jpg
.. versionadded:: 0.1.9
"""
import ctypes
import os
import platform
import sys
import tempfile
from .image import Image
from .api import library
from .exceptions import BlobError, DelegateError
__all__ = 'display',
def display(image, server_name=':0'):
"""Displays the passed ``image``.
:param image: an image to display
:type image: :class:`~wand.image.Image`
:param server_name: X11 server name to use. it is ignored and not used
for Mac. default is ``':0'``
:type server_name: :class:`str`
"""
if not isinstance(image, Image):
raise TypeError('image must be a wand.image.Image instance, not ' +
repr(image))
system = platform.system()
if system == 'Windows':
try:
image.save(filename='win:.')
except DelegateError:
pass
else:
return
if system in ('Windows', 'Darwin'):
ext = '.' + image.format.lower()
path = tempfile.mktemp(suffix=ext)
image.save(filename=path)
os.system(('start ' if system == 'Windows' else 'open ') + path)
else:
library.MagickDisplayImage.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
library.MagickDisplayImage(image.wand, str(server_name).encode())
if __name__ == '__main__':
if len(sys.argv) < 2:
print>>sys.stderr, 'usage: python -m wand.display FILE'
raise SystemExit
path = sys.argv[1]
try:
with Image(filename=path) as image:
display(image)
except BlobError:
print>>sys.stderr, 'cannot read the file', path
""":mod:`wand.drawing` --- Drawings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The module provides some vector drawing functions.
.. versionadded:: 0.3.0
"""
import collections
import ctypes
import numbers
from .api import library, MagickPixelPacket, PointInfo, AffineMatrix
from .color import Color
from .compat import binary, string_type, text, text_type, xrange
from .image import Image, COMPOSITE_OPERATORS
from .resource import Resource
from .exceptions import WandLibraryVersionError
__all__ = ('CLIP_PATH_UNITS', 'FILL_RULE_TYPES', 'FONT_METRICS_ATTRIBUTES',
'GRAVITY_TYPES', 'LINE_CAP_TYPES', 'LINE_JOIN_TYPES',
'PAINT_METHOD_TYPES', 'STRETCH_TYPES', 'STYLE_TYPES',
'TEXT_ALIGN_TYPES', 'TEXT_DECORATION_TYPES',
'TEXT_DIRECTION_TYPES', 'Drawing', 'FontMetrics')
#: (:class:`collections.Sequence`) The list of clip path units
#:
#: - ``'undefined_path_units'``
#: - ``'user_space'``
#: - ``'user_space_on_use'``
#: - ``'object_bounding_box'``
CLIP_PATH_UNITS = ('undefined_path_units', 'user_space', 'user_space_on_use',
'object_bounding_box')
#: (:class:`collections.Sequence`) The list of text align types.
#:
#: - ``'undefined'``
#: - ``'left'``
#: - ``'center'``
#: - ``'right'``
TEXT_ALIGN_TYPES = 'undefined', 'left', 'center', 'right'
#: (:class:`collections.Sequence`) The list of text decoration types.
#:
#: - ``'undefined'``
#: - ``'no'``
#: - ``'underline'``
#: - ``'overline'``
#: - ``'line_through'``
TEXT_DECORATION_TYPES = ('undefined', 'no', 'underline', 'overline',
'line_through')
#: (:class:`collections.Sequence`) The list of text direction types.
#:
#: - ``'undefined'``
#: - ``'right_to_left'``
#: - ``'left_to_right'``
TEXT_DIRECTION_TYPES = ('undefined', 'right_to_left', 'left_to_right')
#: (:class:`collections.Sequence`) The list of text gravity types.
#:
#: - ``'forget'``
#: - ``'north_west'``
#: - ``'north'``
#: - ``'north_east'``
#: - ``'west'``
#: - ``'center'``
#: - ``'east'``
#: - ``'south_west'``
#: - ``'south'``
#: - ``'south_east'``
#: - ``'static'``
GRAVITY_TYPES = ('forget', 'north_west', 'north', 'north_east', 'west',
'center', 'east', 'south_west', 'south', 'south_east',
'static')
#: (:class:`collections.Sequence`) The list of fill-rule types.
#:
#: - ``'undefined'``
#: - ``'evenodd'``
#: - ``'nonzero'``
FILL_RULE_TYPES = ('undefined', 'evenodd', 'nonzero')
#: (:class:`collections.Sequence`) The attribute names of font metrics.
FONT_METRICS_ATTRIBUTES = ('character_width', 'character_height', 'ascender',
'descender', 'text_width', 'text_height',
'maximum_horizontal_advance', 'x1', 'y1', 'x2',
'y2', 'x', 'y')
#: The tuple subtype which consists of font metrics data.
FontMetrics = collections.namedtuple('FontMetrics', FONT_METRICS_ATTRIBUTES)
#: (:class:`collections.Sequence`) The list of stretch types for fonts
#:
#: - ``'undefined;``
#: - ``'normal'``
#: - ``'ultra_condensed'``
#: - ``'extra_condensed'``
#: - ``'condensed'``
#: - ``'semi_condensed'``
#: - ``'semi_expanded'``
#: - ``'expanded'``
#: - ``'extra_expanded'``
#: - ``'ultra_expanded'``
#: - ``'any'``
STRETCH_TYPES = ('undefined', 'normal', 'ultra_condensed', 'extra_condensed',
'condensed', 'semi_condensed', 'semi_expanded', 'expanded',
'extra_expanded', 'ultra_expanded', 'any')
#: (:class:`collections.Sequence`) The list of style types for fonts
#:
#: - ``'undefined;``
#: - ``'normal'``
#: - ``'italic'``
#: - ``'oblique'``
#: - ``'any'``
STYLE_TYPES = ('undefined', 'normal', 'italic', 'oblique', 'any')
#: (:class:`collections.Sequence`) The list of LineCap types
#:
#: - ``'undefined;``
#: - ``'butt'``
#: - ``'round'``
#: - ``'square'``
LINE_CAP_TYPES = ('undefined', 'butt', 'round', 'square')
#: (:class:`collections.Sequence`) The list of LineJoin types
#:
#: - ``'undefined'``
#: - ``'miter'``
#: - ``'round'``
#: - ``'bevel'``
LINE_JOIN_TYPES = ('undefined', 'miter', 'round', 'bevel')
#: (:class:`collections.Sequence`) The list of paint method types.
#:
#: - ``'undefined'``
#: - ``'point'``
#: - ``'replace'``
#: - ``'floodfill'``
#: - ``'filltoborder'``
#: - ``'reset'``
PAINT_METHOD_TYPES = ('undefined', 'point', 'replace',
'floodfill', 'filltoborder', 'reset')
class Drawing(Resource):
"""Drawing object. It maintains several vector drawing instructions
and can get drawn into zero or more :class:`~wand.image.Image` objects
by calling it.
For example, the following code draws a diagonal line to the ``image``::
with Drawing() as draw:
draw.line((0, 0), image.size)
draw(image)
:param drawing: an optional drawing object to clone.
use :meth:`clone()` method rather than this parameter
:type drawing: :class:`Drawing`
.. versionadded:: 0.3.0
"""
c_is_resource = library.IsDrawingWand
c_destroy_resource = library.DestroyDrawingWand
c_get_exception = library.DrawGetException
c_clear_exception = library.DrawClearException
def __init__(self, drawing=None):
with self.allocate():
if not drawing:
wand = library.NewDrawingWand()
elif not isinstance(drawing, type(self)):
raise TypeError('drawing must be a wand.drawing.Drawing '
'instance, not ' + repr(drawing))
else:
wand = library.CloneDrawingWand(drawing.resource)
self.resource = wand
def clone(self):
"""Copies a drawing object.
:returns: a duplication
:rtype: :class:`Drawing`
"""
return type(self)(drawing=self)
@property
def border_color(self):
"""(:class:`~wand.color.Color`) the current border color. It also can
be set. This attribute controls the behavior of
:meth:`~wand.drawing.Drawing.color()` during ``'filltoborder'``
operation.
.. versionadded:: 0.4.0
"""
pixelwand = library.NewPixelWand()
library.DrawGetBorderColor(self.resource, pixelwand)
size = ctypes.sizeof(MagickPixelPacket)
buffer = ctypes.create_string_buffer(size)
library.PixelGetMagickColor(pixelwand, buffer)
return Color(raw=buffer)
@border_color.setter
def border_color(self, border_color):
if not isinstance(border_color, Color):
raise ValueError('expected wand.color.Color, not ' +
repr(border_color))
with border_color:
library.DrawSetBorderColor(self.resource, border_color.resource)
@property
def clip_path(self):
"""(:class:`basestring`) The current clip path. It also can be set.
.. versionadded:: 0.4.0
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
clip_path_p = library.DrawGetClipPath(self.resource)
return text(clip_path_p.value)
@clip_path.setter
def clip_path(self, path):
if not isinstance(path, string_type):
raise TypeError('expected a string, not ' + repr(path))
okay = library.DrawSetClipPath(self.resource, binary(path))
if okay == 0:
raise ValueError('Clip path not understood')
@property
def clip_rule(self):
"""(:class:`basestring`) The current clip rule. It also can be set.
It's a string value from :const:`FILL_RULE_TYPES` list.
.. versionadded:: 0.4.0
"""
clip_rule = library.DrawGetClipRule(self.resource)
return FILL_RULE_TYPES[clip_rule]
@clip_rule.setter
def clip_rule(self, clip_rule):
if not isinstance(clip_rule, string_type):
raise TypeError('expected a string, not ' + repr(clip_rule))
elif clip_rule not in FILL_RULE_TYPES:
raise ValueError('expected a string from FILE_RULE_TYPES, not' +
repr(clip_rule))
library.DrawSetClipRule(self.resource,
FILL_RULE_TYPES.index(clip_rule))
@property
def clip_units(self):
"""(:class:`basestring`) The current clip units. It also can be set.
It's a string value from :const:`CLIP_PATH_UNITS` list.
.. versionadded:: 0.4.0
"""
clip_unit = library.DrawGetClipUnits(self.resource)
return CLIP_PATH_UNITS[clip_unit]
@clip_units.setter
def clip_units(self, clip_unit):
if not isinstance(clip_unit, string_type):
raise TypeError('expected a string, not ' + repr(clip_unit))
elif clip_unit not in CLIP_PATH_UNITS:
raise ValueError('expected a string from CLIP_PATH_UNITS, not' +
repr(clip_unit))
library.DrawSetClipUnits(self.resource,
CLIP_PATH_UNITS.index(clip_unit))
@property
def font(self):
"""(:class:`basestring`) The current font name. It also can be set.
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
font_p = library.DrawGetFont(self.resource)
return text(font_p.value)
@font.setter
def font(self, font):
if not isinstance(font, string_type):
raise TypeError('expected a string, not ' + repr(font))
library.DrawSetFont(self.resource, binary(font))
@property
def font_family(self):
"""(:class:`basestring`) The current font family. It also can be set.
.. versionadded:: 0.4.0
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
font_family_p = library.DrawGetFontFamily(self.resource)
return text(font_family_p.value)
@font_family.setter
def font_family(self, family):
if not isinstance(family, string_type):
raise TypeError('expected a string, not ' + repr(family))
library.DrawSetFontFamily(self.resource, binary(family))
@property
def font_resolution(self):
"""(:class:`~collections.Sequence`) The current font resolution. It also
can be set.
.. versionadded:: 0.4.0
"""
x, y = ctypes.c_double(0.0), ctypes.c_double(0.0)
library.DrawGetFontResolution(self.resource,
ctypes.byref(x),
ctypes.byref(y))
return x.value, y.value
@font_resolution.setter
def font_resolution(self, resolution):
if not isinstance(resolution, collections.Sequence):
raise TypeError('expected sequence, not ' + repr(resolution))
if len(resolution) != 2:
raise ValueError('expected sequence of 2 floats')
library.DrawSetFontResolution(self.resource, *resolution)
@property
def font_size(self):
"""(:class:`numbers.Real`) The font size. It also can be set."""
return library.DrawGetFontSize(self.resource)
@font_size.setter
def font_size(self, size):
if not isinstance(size, numbers.Real):
raise TypeError('expected a numbers.Real, but got ' + repr(size))
elif size < 0.0:
raise ValueError('cannot be less then 0.0, but got ' + repr(size))
library.DrawSetFontSize(self.resource, size)
@property
def font_stretch(self):
"""(:class:`basestring`) The current font family. It also can be set.
.. versionadded:: 0.4.0
"""
stretch_index = library.DrawGetFontStretch(self.resource)
return text(STRETCH_TYPES[stretch_index])
@font_stretch.setter
def font_stretch(self, stretch):
if not isinstance(stretch, string_type):
raise TypeError('expected a string, not ' + repr(stretch))
elif stretch not in STRETCH_TYPES:
raise ValueError('expected a string from STRETCH_TYPES, not' +
repr(stretch))
library.DrawSetFontStretch(self.resource,
STRETCH_TYPES.index(stretch))
@property
def font_style(self):
"""(:class:`basestring`) The current font style. It also can be set.
.. versionadded:: 0.4.0
"""
style_index = library.DrawGetFontStyle(self.resource)
return text(STYLE_TYPES[style_index])
@font_style.setter
def font_style(self, style):
if not isinstance(style, string_type):
raise TypeError('expected a string, not ' + repr(style))
elif style not in STYLE_TYPES:
raise ValueError('expected a string from STYLE_TYPES, not' +
repr(style))
library.DrawSetFontStyle(self.resource,
STYLE_TYPES.index(style))
@property
def font_weight(self):
"""(:class:`~numbers.Integral`) The current font weight.
It also can be set.
.. versionadded:: 0.4.0
"""
return library.DrawGetFontWeight(self.resource)
@font_weight.setter
def font_weight(self, weight):
if not isinstance(weight, numbers.Integral):
raise TypeError('expected a integral, not ' + repr(weight))
library.DrawSetFontWeight(self.resource, weight)
@property
def fill_color(self):
"""(:class:`~wand.color.Color`) The current color to fill.
It also can be set.
"""
pixel = library.NewPixelWand()
library.DrawGetFillColor(self.resource, pixel)
size = ctypes.sizeof(MagickPixelPacket)
buffer = ctypes.create_string_buffer(size)
library.PixelGetMagickColor(pixel, buffer)
return Color(raw=buffer)
@fill_color.setter
def fill_color(self, color):
if not isinstance(color, Color):
raise TypeError('color must be a wand.color.Color object, not ' +
repr(color))
with color:
library.DrawSetFillColor(self.resource, color.resource)
@property
def fill_opacity(self):
"""(:class:`~numbers.Real`) The current fill opacity.
It also can be set.
.. versionadded:: 0.4.0
"""
return library.DrawGetFillOpacity(self.resource)
@fill_opacity.setter
def fill_opacity(self, opacity):
if not isinstance(opacity, numbers.Real):
raise TypeError('opacity must be a double, not ' +
repr(opacity))
library.DrawSetFillOpacity(self.resource, opacity)
@property
def fill_rule(self):
"""(:class:`basestring`) The current fill rule. It can also be set.
It's a string value from :const:`FILL_RULE_TYPES` list.
.. versionadded:: 0.4.0
"""
fill_rule_index = library.DrawGetFillRule(self.resource)
if fill_rule_index not in FILL_RULE_TYPES:
self.raise_exception()
return text(FILL_RULE_TYPES[fill_rule_index])
@fill_rule.setter
def fill_rule(self, fill_rule):
if not isinstance(fill_rule, string_type):
raise TypeError('expected a string, not ' + repr(fill_rule))
elif fill_rule not in FILL_RULE_TYPES:
raise ValueError('expected a string from FILE_RULE_TYPES, not' +
repr(fill_rule))
library.DrawSetFillRule(self.resource,
FILL_RULE_TYPES.index(fill_rule))
@property
def opacity(self):
"""(:class:`~numbers.Real`) returns the opacity used when drawing with
the fill or stroke color or texture. Fully opaque is 1.0. This method
only affects vector graphics, and is experimental. To set the opacity
of a drawing, use
:attr:`Drawing.fill_opacity` & :attr:`Drawing.stroke_opacity`
.. versionadded:: 0.4.0
"""
return library.DrawGetOpacity(self.resource)
@opacity.setter
def opacity(self, opaque):
library.DrawSetOpacity(self.resource, ctypes.c_double(opaque))
@property
def stroke_antialias(self):
"""(:class:`bool`) Controls whether stroked outlines are antialiased.
Stroked outlines are antialiased by default. When antialiasing is
disabled stroked pixels are thresholded to determine if the stroke
color or underlying canvas color should be used.
It also can be set.
.. versionadded:: 0.4.0
"""
stroke_antialias = library.DrawGetStrokeAntialias(self.resource)
return bool(stroke_antialias)
@stroke_antialias.setter
def stroke_antialias(self, stroke_antialias):
library.DrawSetStrokeAntialias(self.resource, bool(stroke_antialias))
@property
def stroke_color(self):
"""(:class:`~wand.color.Color`) The current color of stroke.
It also can be set.
.. versionadded:: 0.3.3
"""
pixel = library.NewPixelWand()
library.DrawGetStrokeColor(self.resource, pixel)
size = ctypes.sizeof(MagickPixelPacket)
buffer = ctypes.create_string_buffer(size)
library.PixelGetMagickColor(pixel, buffer)
return Color(raw=buffer)
@stroke_color.setter
def stroke_color(self, color):
if not isinstance(color, Color):
raise TypeError('color must be a wand.color.Color object, not ' +
repr(color))
with color:
library.DrawSetStrokeColor(self.resource, color.resource)
@property
def stroke_dash_array(self):
"""(:class:`~collections.Sequence`) - (:class:`numbers.Real`) An array
representing the pattern of dashes & gaps used to stroke paths.
It also can be set.
.. versionadded:: 0.4.0
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
number_elements = ctypes.c_size_t(0)
dash_array_p = library.DrawGetStrokeDashArray(
self.resource, ctypes.byref(number_elements)
)
dash_array = []
if dash_array_p is not None:
dash_array = [float(dash_array_p[i])
for i in xrange(number_elements.value)]
library.MagickRelinquishMemory(dash_array_p)
return dash_array
@stroke_dash_array.setter
def stroke_dash_array(self, dash_array):
dash_array_l = len(dash_array)
dash_array_p = (ctypes.c_double * dash_array_l)(*dash_array)
library.DrawSetStrokeDashArray(self.resource,
dash_array_l,
dash_array_p)
@property
def stroke_dash_offset(self):
"""(:class:`numbers.Real`) The stroke dash offset. It also can be set.
.. versionadded:: 0.4.0
"""
return library.DrawGetStrokeDashOffset(self.resource)
@stroke_dash_offset.setter
def stroke_dash_offset(self, offset):
library.DrawSetStrokeDashOffset(self.resource, float(offset))
@property
def stroke_line_cap(self):
"""(:class:`basestring`) The stroke line cap. It also can be set.
.. versionadded:: 0.4.0
"""
line_cap_index = library.DrawGetStrokeLineCap(self.resource)
if line_cap_index not in LINE_CAP_TYPES:
self.raise_exception()
return text(LINE_CAP_TYPES[line_cap_index])
@stroke_line_cap.setter
def stroke_line_cap(self, line_cap):
if not isinstance(line_cap, string_type):
raise TypeError('expected a string, not ' + repr(line_cap))
elif line_cap not in LINE_CAP_TYPES:
raise ValueError('expected a string from LINE_CAP_TYPES, not' +
repr(line_cap))
library.DrawSetStrokeLineCap(self.resource,
LINE_CAP_TYPES.index(line_cap))
@property
def stroke_line_join(self):
"""(:class:`basestring`) The stroke line join. It also can be set.
.. versionadded:: 0.4.0
"""
line_join_index = library.DrawGetStrokeLineJoin(self.resource)
if line_join_index not in LINE_JOIN_TYPES:
self.raise_exception()
return text(LINE_JOIN_TYPES[line_join_index])
@stroke_line_join.setter
def stroke_line_join(self, line_join):
if not isinstance(line_join, string_type):
raise TypeError('expected a string, not ' + repr(line_join))
elif line_join not in LINE_JOIN_TYPES:
raise ValueError('expected a string from LINE_JOIN_TYPES, not' +
repr(line_join))
library.DrawSetStrokeLineJoin(self.resource,
LINE_JOIN_TYPES.index(line_join))
@property
def stroke_miter_limit(self):
"""(:class:`~numbers.Integral`) The current miter limit.
It also can be set.
.. versionadded:: 0.4.0
"""
return library.DrawGetStrokeMiterLimit(self.resource)
@stroke_miter_limit.setter
def stroke_miter_limit(self, miter_limit):
if not isinstance(miter_limit, numbers.Integral):
raise TypeError('opacity must be a integer, not ' +
repr(miter_limit))
library.DrawSetStrokeMiterLimit(self.resource, miter_limit)
@property
def stroke_opacity(self):
"""(:class:`~numbers.Real`) The current stroke opacity.
It also can be set.
.. versionadded:: 0.4.0
"""
return library.DrawGetStrokeOpacity(self.resource)
@stroke_opacity.setter
def stroke_opacity(self, opacity):
if not isinstance(opacity, numbers.Real):
raise TypeError('opacity must be a double, not ' +
repr(opacity))
library.DrawSetStrokeOpacity(self.resource, opacity)
@property
def stroke_width(self):
"""(:class:`numbers.Real`) The stroke width. It also can be set.
.. versionadded:: 0.3.3
"""
return library.DrawGetStrokeWidth(self.resource)
@stroke_width.setter
def stroke_width(self, width):
if not isinstance(width, numbers.Real):
raise TypeError('expected a numbers.Real, but got ' + repr(width))
elif width < 0.0:
raise ValueError('cannot be less then 0.0, but got ' + repr(width))
library.DrawSetStrokeWidth(self.resource, width)
@property
def text_alignment(self):
"""(:class:`basestring`) The current text alignment setting.
It's a string value from :const:`TEXT_ALIGN_TYPES` list.
It also can be set.
"""
text_alignment_index = library.DrawGetTextAlignment(self.resource)
if not text_alignment_index:
self.raise_exception()
return text(TEXT_ALIGN_TYPES[text_alignment_index])
@text_alignment.setter
def text_alignment(self, align):
if not isinstance(align, string_type):
raise TypeError('expected a string, not ' + repr(align))
elif align not in TEXT_ALIGN_TYPES:
raise ValueError('expected a string from TEXT_ALIGN_TYPES, not ' +
repr(align))
library.DrawSetTextAlignment(self.resource,
TEXT_ALIGN_TYPES.index(align))
@property
def text_antialias(self):
"""(:class:`bool`) The boolean value which represents whether
antialiasing is used for text rendering. It also can be set to
``True`` or ``False`` to switch the setting.
"""
result = library.DrawGetTextAntialias(self.resource)
return bool(result)
@text_antialias.setter
def text_antialias(self, value):
library.DrawSetTextAntialias(self.resource, bool(value))
@property
def text_decoration(self):
"""(:class:`basestring`) The text decoration setting, a string
from :const:`TEXT_DECORATION_TYPES` list. It also can be set.
"""
text_decoration_index = library.DrawGetTextDecoration(self.resource)
if not text_decoration_index:
self.raise_exception()
return text(TEXT_DECORATION_TYPES[text_decoration_index])
@text_decoration.setter
def text_decoration(self, decoration):
if not isinstance(decoration, string_type):
raise TypeError('expected a string, not ' + repr(decoration))
elif decoration not in TEXT_DECORATION_TYPES:
raise ValueError('expected a string from TEXT_DECORATION_TYPES, '
'not ' + repr(decoration))
library.DrawSetTextDecoration(self.resource,
TEXT_DECORATION_TYPES.index(decoration))
@property
def text_direction(self):
"""(:class:`basestring`) The text direction setting. a string
from :const:`TEXT_DIRECTION_TYPES` list. It also can be set."""
if library.DrawGetTextDirection is None:
raise WandLibraryVersionError(
'the installed version of ImageMagick does not support '
'this feature'
)
text_direction_index = library.DrawGetTextDirection(self.resource)
if not text_direction_index:
self.raise_exception()
return text(TEXT_DIRECTION_TYPES[text_direction_index])
@text_direction.setter
def text_direction(self, direction):
if library.DrawGetTextDirection is None:
raise WandLibraryVersionError(
'The installed version of ImageMagick does not support '
'this feature'
)
if not isinstance(direction, string_type):
raise TypeError('expected a string, not ' + repr(direction))
elif direction not in TEXT_DIRECTION_TYPES:
raise ValueError('expected a string from TEXT_DIRECTION_TYPES, '
'not ' + repr(direction))
library.DrawSetTextDirection(self.resource,
TEXT_DIRECTION_TYPES.index(direction))
@property
def text_encoding(self):
"""(:class:`basestring`) The internally used text encoding setting.
Although it also can be set, but it's not encouraged.
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
text_encoding_p = library.DrawGetTextEncoding(self.resource)
return text(text_encoding_p.value)
@text_encoding.setter
def text_encoding(self, encoding):
if encoding is not None and not isinstance(encoding, string_type):
raise TypeError('expected a string, not ' + repr(encoding))
elif encoding is None:
# encoding specify an empty string to set text encoding
# to system's default.
encoding = b''
else:
encoding = binary(encoding)
library.DrawSetTextEncoding(self.resource, encoding)
@property
def text_interline_spacing(self):
"""(:class:`numbers.Real`) The setting of the text line spacing.
It also can be set.
"""
if library.DrawGetTextInterlineSpacing is None:
raise WandLibraryVersionError('The installed version of '
'ImageMagick does not support '
'this feature')
return library.DrawGetTextInterlineSpacing(self.resource)
@text_interline_spacing.setter
def text_interline_spacing(self, spacing):
if library.DrawSetTextInterlineSpacing is None:
raise WandLibraryVersionError('The installed version of '
'ImageMagick does not support '
'this feature')
if not isinstance(spacing, numbers.Real):
raise TypeError('expected a numbers.Real, but got ' +
repr(spacing))
library.DrawSetTextInterlineSpacing(self.resource, spacing)
@property
def text_interword_spacing(self):
"""(:class:`numbers.Real`) The setting of the word spacing.
It also can be set.
"""
return library.DrawGetTextInterwordSpacing(self.resource)
@text_interword_spacing.setter
def text_interword_spacing(self, spacing):
if not isinstance(spacing, numbers.Real):
raise TypeError('expeted a numbers.Real, but got ' + repr(spacing))
library.DrawSetTextInterwordSpacing(self.resource, spacing)
@property
def text_kerning(self):
"""(:class:`numbers.Real`) The setting of the text kerning.
It also can be set.
"""
return library.DrawGetTextKerning(self.resource)
@text_kerning.setter
def text_kerning(self, kerning):
if not isinstance(kerning, numbers.Real):
raise TypeError('expected a numbers.Real, but got ' +
repr(kerning))
library.DrawSetTextKerning(self.resource, kerning)
@property
def text_under_color(self):
"""(:class:`~wand.color.Color`) The color of a background rectangle
to place under text annotations. It also can be set.
"""
pixel = library.NewPixelWand()
library.DrawGetTextUnderColor(self.resource, pixel)
size = ctypes.sizeof(MagickPixelPacket)
buffer = ctypes.create_string_buffer(size)
library.PixelGetMagickColor(pixel, buffer)
return Color(raw=buffer)
@text_under_color.setter
def text_under_color(self, color):
if not isinstance(color, Color):
raise TypeError('expected a wand.color.Color object, not ' +
repr(color))
with color:
library.DrawSetTextUnderColor(self.resource, color.resource)
@property
def vector_graphics(self):
"""(:class:`basestring`) The XML text of the Vector Graphics.
It also can be set. The drawing-wand XML is experimental,
and subject to change.
Setting this property to None will reset all vector graphic properties
to the default state.
.. versionadded:: 0.4.0
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
vector_graphics_p = library.DrawGetVectorGraphics(self.resource)
return '<wand>' + text(vector_graphics_p.value) + '</wand>'
@vector_graphics.setter
def vector_graphics(self, vector_graphics):
if vector_graphics is not None and not isinstance(vector_graphics,
string_type):
raise TypeError('expected a string, not ' + repr(vector_graphics))
elif vector_graphics is None:
# Reset all vector graphic properties on drawing wand.
library.DrawResetVectorGraphics(self.resource)
else:
vector_graphics = binary(vector_graphics)
okay = library.DrawSetVectorGraphics(self.resource,
vector_graphics)
if okay == 0:
raise ValueError("Vector graphic not understood.")
@property
def gravity(self):
"""(:class:`basestring`) The text placement gravity used when
annotating with text. It's a string from :const:`GRAVITY_TYPES`
list. It also can be set.
"""
gravity_index = library.DrawGetGravity(self.resource)
if not gravity_index:
self.raise_exception()
return text(GRAVITY_TYPES[gravity_index])
@gravity.setter
def gravity(self, value):
if not isinstance(value, string_type):
raise TypeError('expected a string, not ' + repr(value))
elif value not in GRAVITY_TYPES:
raise ValueError('expected a string from GRAVITY_TYPES, not '
+ repr(value))
library.DrawSetGravity(self.resource, GRAVITY_TYPES.index(value))
def clear(self):
library.ClearDrawingWand(self.resource)
def draw(self, image):
"""Renders the current drawing into the ``image``. You can simply
call :class:`Drawing` instance rather than calling this method.
That means the following code which calls :class:`Drawing` object
itself::
drawing(image)
is equivalent to the following code which calls :meth:`draw()` method::
drawing.draw(image)
:param image: the image to be drawn
:type image: :class:`~wand.image.Image`
"""
if not isinstance(image, Image):
raise TypeError('image must be a wand.image.Image instance, not '
+ repr(image))
res = library.MagickDrawImage(image.wand, self.resource)
if not res:
self.raise_exception()
def affine(self, matrix):
"""Adjusts the current affine transformation matrix with the specified
affine transformation matrix. Note that the current affine transform is
adjusted rather than replaced.
.. sourcecode:: text
| sx rx 0 |
| x', y', 1 | = | x, y, 1 | * | ry sy 0 |
| tx ty 1 |
:param matrix: a list of :class:`~numbers.Real` to define affine
matrix ``[sx, rx, ry, sy, tx, ty]``
:type matrix: :class:`collections.Sequence`
.. versionadded:: 0.4.0
"""
if not isinstance(matrix, collections.Sequence) or len(matrix) != 6:
raise ValueError('matrix must be a list of size Real numbers')
for idx, val in enumerate(matrix):
if not isinstance(val, numbers.Real):
raise TypeError('expecting numbers.Real in position #' +
repr(idx))
amx = AffineMatrix(sx=matrix[0], rx=matrix[1],
ry=matrix[2], sy=matrix[3],
tx=matrix[4], ty=matrix[5])
library.DrawAffine(self.resource, amx)
def arc(self, start, end, degree):
"""Draws a arc using the current :attr:`stroke_color`,
:attr:`stroke_width`, and :attr:`fill_color`.
:param start: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents starting x and y of the arc
:type start: :class:`~collections.Sequence`
:param end: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents ending x and y of the arc
:type end: :class:`~collections.Sequence`
:param degree: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents starting degree, and ending degree
:type degree: :class:`~collections.Sequence`
.. versionadded:: 0.4.0
"""
start_x, start_y = start
end_x, end_y = end
degree_start, degree_end = degree
library.DrawArc(self.resource,
float(start_x), float(start_y),
float(end_x), float(end_y),
float(degree_start), float(degree_end))
def circle(self, origin, perimeter):
"""Draws a circle from ``origin`` to ``perimeter``
:param origin: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents origin x and y of circle
:type origin: :class:`collections.Sequence`
:param perimeter: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents perimeter x and y of circle
:type perimeter: :class:`collections.Sequence`
.. versionadded:: 0.4.0
"""
origin_x, origin_y = origin
perimeter_x, perimeter_y = perimeter
library.DrawCircle(self.resource,
float(origin_x), float(origin_y), # origin
float(perimeter_x), float(perimeter_y)) # perimeter
def color(self, x=None, y=None, paint_method='undefined'):
"""Draws a color on the image using current fill color, starting
at specified position & method.
Available methods in :class:`wand.drawing.PAINT_METHOD_TYPES`:
- ``'undefined'``
- ``'point'``
- ``'replace'``
- ``'floodfill'``
- ``'filltoborder'``
- ``'reset'``
.. versionadded:: 0.4.0
"""
if x is None or y is None:
raise TypeError('Both x & y coordinates need to be defined')
if not isinstance(paint_method, string_type):
raise TypeError('expected a string, not ' + repr(paint_method))
elif paint_method not in PAINT_METHOD_TYPES:
raise ValueError('expected a string from PAINT_METHOD_TYPES, not '
+ repr(paint_method))
library.DrawColor(self.resource, float(x), float(y),
PAINT_METHOD_TYPES.index(paint_method))
def comment(self, message=None):
"""Adds a comment to the vector stream.
:param message: the comment to set.
:type message: :class:`basestring`
.. versionadded:: 0.4.0
"""
if message is not None and not isinstance(message, string_type):
raise TypeError('expected a string, not ' + repr(message))
elif message is None:
message = b''
else:
message = binary(message)
library.DrawComment(self.resource, message)
def composite(self, operator, left, top, width, height, image):
"""Composites an image onto the current image, using the specified
composition operator, specified position, and at the specified size.
:param operator: the operator that affects how the composite
is applied to the image. available values
can be found in the :const:`COMPOSITE_OPERATORS`
list
:param type: :const:`COMPOSITE_OPERATORS`
:param left: the column offset of the composited drawing source
:type left: :class:`numbers.Real`
:param top: the row offset of the composited drawing source
:type top: :class:`numbers.Real`
:param width: the total columns to include in the composited source
:type width: :class:`numbers.Real`
:param height: the total rows to include in the composited source
:type height: :class:`numbers.Real`
.. versionadded:: 0.4.0
"""
if not isinstance(operator, string_type):
raise TypeError('operator must be a string, not ' +
repr(operator))
elif not isinstance(left, numbers.Real):
raise TypeError('left must be an integer, not ' + repr(left))
elif not isinstance(top, numbers.Real):
raise TypeError('top must be an integer, not ' + repr(left))
elif not isinstance(width, numbers.Real):
raise TypeError('width must be an integer, not ' + repr(left))
elif not isinstance(height, numbers.Real):
raise TypeError('height must be an integer, not ' + repr(left))
try:
op = COMPOSITE_OPERATORS.index(operator)
except IndexError:
raise IndexError(repr(operator) + ' is an invalid composite '
'operator type; see wand.image.COMPOSITE_'
'OPERATORS dictionary')
okay = library.DrawComposite(self.resource, op, left, top, width,
height, image.wand)
if okay == 0:
self.raise_exception()
def ellipse(self, origin, radius, rotation=(0, 360)):
"""Draws a ellipse at ``origin`` with independent x & y ``radius``.
Ellipse can be partial by setting start & end ``rotation``.
:param origin: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents origin x and y of circle
:type origin: :class:`collections.Sequence`
:param radius: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents radius x and radius y of circle
:type radius: :class:`collections.Sequence`
:param rotation: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents start and end of ellipse.
Default (0,360)
:type rotation: :class:`collections.Sequence`
.. versionadded:: 0.4.0
"""
origin_x, origin_y = origin
radius_x, radius_y = radius
rotation_start, rotation_end = rotation
library.DrawEllipse(self.resource,
float(origin_x), float(origin_y), # origin
float(radius_x), float(radius_y), # radius
float(rotation_start), float(rotation_end))
def line(self, start, end):
"""Draws a line ``start`` to ``end``.
:param start: (:class:`~numbers.Integral`, :class:`numbers.Integral`)
pair which represents starting x and y of the line
:type start: :class:`collections.Sequence`
:param end: (:class:`~numbers.Integral`, :class:`numbers.Integral`)
pair which represents ending x and y of the line
:type end: :class:`collections.Sequence`
"""
start_x, start_y = start
end_x, end_y = end
library.DrawLine(self.resource,
int(start_x), int(start_y),
int(end_x), int(end_y))
def matte(self, x=None, y=None, paint_method='undefined'):
"""Paints on the image's opacity channel in order to set effected pixels
to transparent.
To influence the opacity of pixels. The available methods are:
- ``'undefined'``
- ``'point'``
- ``'replace'``
- ``'floodfill'``
- ``'filltoborder'``
- ``'reset'``
.. versionadded:: 0.4.0
"""
if x is None or y is None:
raise TypeError('Both x & y coordinates need to be defined')
if not isinstance(paint_method, string_type):
raise TypeError('expected a string, not ' + repr(paint_method))
elif paint_method not in PAINT_METHOD_TYPES:
raise ValueError('expected a string from PAINT_METHOD_TYPES, not '
+ repr(paint_method))
library.DrawMatte(self.resource, float(x), float(y),
PAINT_METHOD_TYPES.index(paint_method))
def path_close(self):
"""Adds a path element to the current path which closes
the current subpath by drawing a straight line from the current point
to the current subpath's most recent starting point.
.. versionadded:: 0.4.0
"""
library.DrawPathClose(self.resource)
return self
def path_curve(self, to=None, controls=None, smooth=False, relative=False):
"""Draws a cubic Bezier curve from the current point to given ``to``
(x,y) coordinate using ``controls`` points at the beginning and
the end of the curve.
If ``smooth`` is set to True, only one ``controls`` is expected
and the previous control is used, else two pair of coordinates are
expected to define the control points. The ``to`` coordinate then
becomes the new current point.
:param to: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents coordinates to draw to
:type to: :class:`collections.Sequence`
:param controls: (:class:`~numbers.Real`, :class:`numbers.Real`)
coordinate to used to influence curve
:type controls: :class:`collections.Sequence`
:param smooth: :class:`bool` assume last defined control coordinate
:type smooth: :class:`bool`
:param relative: treat given coordinates as relative to current point
:type relative: :class:`bool`
.. versionadded:: 0.4.0
"""
if to is None:
raise TypeError('to is missing')
if controls is None:
raise TypeError('controls is missing')
x, y = to
if smooth:
x2, y2 = controls
else:
(x1, y1), (x2, y2) = controls
if smooth:
if relative:
library.DrawPathCurveToSmoothRelative(self.resource,
x2, y2, x, y)
else:
library.DrawPathCurveToSmoothAbsolute(self.resource,
x2, y2, x, y)
else:
if relative:
library.DrawPathCurveToRelative(self.resource,
x1, y1, x2, y2, x, y)
else:
library.DrawPathCurveToAbsolute(self.resource,
x1, y1, x2, y2, x, y)
return self
def path_curve_to_quadratic_bezier(self, to=None, control=None,
smooth=False, relative=False):
"""Draws a quadratic Bezier curve from the current point to given
``to`` coordinate. The control point is assumed to be the reflection of
the control point on the previous command if ``smooth`` is True, else a
pair of ``control`` coordinates must be given. Each coordinates can be
relative, or absolute, to the current point by setting the ``relative``
flag. The ``to`` coordinate then becomes the new current point, and the
``control`` coordinate will be assumed when called again
when ``smooth`` is set to true.
:param to: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents coordinates to draw to
:type to: :class:`collections.Sequence`
:param control: (:class:`~numbers.Real`, :class:`numbers.Real`)
coordinate to used to influence curve
:type control: :class:`collections.Sequence`
:param smooth: assume last defined control coordinate
:type smooth: :class:`bool`
:param relative: treat given coordinates as relative to current point
:type relative: :class:`bool`
.. versionadded:: 0.4.0
"""
if to is None:
raise TypeError('to is missing')
x, y = to
if smooth:
if relative:
library.DrawPathCurveToQuadraticBezierSmoothRelative(
self.resource, float(x), float(y)
)
else:
library.DrawPathCurveToQuadraticBezierSmoothAbsolute(
self.resource, float(x), float(y)
)
else:
if control is None:
raise TypeError('control is missing')
x1, y1 = control
if relative:
library.DrawPathCurveToQuadraticBezierRelative(self.resource,
float(x1),
float(y1),
float(x),
float(y))
else:
library.DrawPathCurveToQuadraticBezierAbsolute(self.resource,
float(x1),
float(y1),
float(x),
float(y))
return self
def path_elliptic_arc(self, to=None, radius=None, rotation=0.0,
large_arc=False, clockwise=False, relative=False):
"""Draws an elliptical arc from the current point to given ``to``
coordinates. The ``to`` coordinates can be relative, or absolute,
to the current point by setting the ``relative`` flag.
The size and orientation of the ellipse are defined by
two radii (rx, ry) in ``radius`` and an ``rotation`` parameters,
which indicates how the ellipse as a whole is
rotated relative to the current coordinate system. The center of the
ellipse is calculated automagically to satisfy the constraints imposed
by the other parameters. ``large_arc`` and ``clockwise`` contribute to
the automatic calculations and help determine how the arc is drawn.
If ``large_arc`` is True then draw the larger of the available arcs.
If ``clockwise`` is true, then draw the arc matching a clock-wise
rotation.
:param to: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents coordinates to draw to
:type to: :class:`collections.Sequence`
:param radius: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents the radii of the ellipse to draw
:type radius: :class:`collections.Sequence`
:param rotate: degree to rotate ellipse on x-axis
:type rotate: :class:`~numbers.Real`
:param large_arc: draw largest available arc
:type large_arc: :class:`bool`
:param clockwise: draw arc path clockwise from start to target
:type clockwise: :class:`bool`
:param relative: treat given coordinates as relative to current point
:type relative: :class:`bool`
.. versionadded:: 0.4.0
"""
if to is None:
raise TypeError('to is missing')
if radius is None:
raise TypeError('radius is missing')
x, y = to
rx, ry = radius
if relative:
library.DrawPathEllipticArcRelative(self.resource,
float(rx), float(ry),
float(rotation),
bool(large_arc),
bool(clockwise),
float(x), float(y))
else:
library.DrawPathEllipticArcAbsolute(self.resource,
float(rx), float(ry),
float(rotation),
bool(large_arc),
bool(clockwise),
float(x), float(y))
return self
def path_finish(self):
"""Terminates the current path.
.. versionadded:: 0.4.0
"""
library.DrawPathFinish(self.resource)
return self
def path_line(self, to=None, relative=False):
"""Draws a line path from the current point to the given ``to``
coordinate. The ``to`` coordinates can be relative, or absolute, to the
current point by setting the ``relative`` flag. The coordinate then
becomes the new current point.
:param to: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents coordinates to draw to.
:type to: :class:`collections.Sequence`
:param relative: :class:`bool`
treat given coordinates as relative to current point
:type relative: :class:`bool`
.. versionadded:: 0.4.0
"""
if to is None:
raise TypeError('to is missing')
x, y = to
if relative:
library.DrawPathLineToRelative(self.resource, float(x), float(y))
else:
library.DrawPathLineToAbsolute(self.resource, float(x), float(y))
return self
def path_horizontal_line(self, x=None, relative=False):
"""Draws a horizontal line path from the current point to the target
point. Given ``x`` parameter can be relative, or absolute, to the
current point by setting the ``relative`` flag. The target point then
becomes the new current point.
:param x: :class:`~numbers.Real`
x-axis point to draw to.
:type x: :class:`~numbers.Real`
:param relative: :class:`bool`
treat given point as relative to current point
:type relative: :class:`bool`
.. versionadded:: 0.4.0
"""
if x is None:
raise TypeError('x is missing')
if relative:
library.DrawPathLineToHorizontalRelative(self.resource, float(x))
else:
library.DrawPathLineToHorizontalAbsolute(self.resource, float(x))
return self
def path_vertical_line(self, y=None, relative=False):
"""Draws a vertical line path from the current point to the target
point. Given ``y`` parameter can be relative, or absolute, to the
current point by setting the ``relative`` flag. The target point then
becomes the new current point.
:param y: :class:`~numbers.Real`
y-axis point to draw to.
:type y: :class:`~numbers.Real`
:param relative: :class:`bool`
treat given point as relative to current point
:type relative: :class:`bool`
.. versionadded:: 0.4.0
"""
if y is None:
raise TypeError('y is missing')
if relative:
library.DrawPathLineToVerticalRelative(self.resource, float(y))
else:
library.DrawPathLineToVerticalAbsolute(self.resource, float(y))
return self
def path_move(self, to=None, relative=False):
"""Starts a new sub-path at the given coordinates. Given ``to``
parameter can be relative, or absolute, by setting the ``relative``
flag.
:param to: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents coordinates to draw to.
:type to: :class:`collections.Sequence`
:param relative: :class:`bool`
treat given coordinates as relative to current point
:type relative: :class:`bool`
.. versionadded:: 0.4.0
"""
if to is None:
raise TypeError('to is missing')
x, y = to
if relative:
library.DrawPathMoveToRelative(self.resource, float(x), float(y))
else:
library.DrawPathMoveToAbsolute(self.resource, float(x), float(y))
return self
def path_start(self):
"""Declares the start of a path drawing list which is terminated by a
matching :meth:`path_finish()` command. All other `path_*` commands
must be enclosed between a :meth:`path_start()` and a
:meth:`path_finish()` command. This is because path drawing commands
are subordinate commands and they do not function by themselves.
.. versionadded:: 0.4.0
"""
library.DrawPathStart(self.resource)
return self
def point(self, x, y):
"""Draws a point at given ``x`` and ``y``
:param x: :class:`~numbers.Real` x of point
:type x: :class:`~numbers.Real`
:param y: :class:`~numbers.Real` y of point
:type y: :class:`~numbers.Real`
.. versionadded:: 0.4.0
"""
library.DrawPoint(self.resource,
float(x),
float(y))
def pop(self):
"""Pop destroys the current drawing wand and returns to the previously
pushed drawing wand. Multiple drawing wands may exist. It is an error
to attempt to pop more drawing wands than have been pushed, and it is
proper form to pop all drawing wands which have been pushed.
:returns: success of pop operation
:rtype: `bool`
.. versionadded:: 0.4.0
"""
return bool(library.PopDrawingWand(self.resource))
def pop_clip_path(self):
"""Terminates a clip path definition.
.. versionadded:: 0.4.0
"""
library.DrawPopClipPath(self.resource)
def pop_defs(self):
"""Terminates a definition list.
.. versionadded:: 0.4.0
"""
library.DrawPopDefs(self.resource)
def pop_pattern(self):
"""Terminates a pattern definition.
.. versionadded:: 0.4.0
"""
library.DrawPopPattern(self.resource)
def push(self):
"""Push clones the current drawing wand to create a new drawing wand.
The original drawing wand(s) may be returned to by invoking
:class:`Drawing.pop`. The drawing wands are stored on a drawing wand
stack. For every Pop there must have already been an equivalent Push.
:returns: success of push operation
:rtype: `bool`
.. versionadded:: 0.4.0
"""
return bool(library.PushDrawingWand(self.resource))
def push_clip_path(self, clip_mask_id):
"""Starts a clip path definition which is comprised of any number of
drawing commands and terminated by a :class:`Drawing.pop_clip_path`
command.
:param clip_mask_id: string identifier to associate with the clip path.
:type clip_mask_id: :class:`basestring`
.. versionadded:: 0.4.0
"""
library.DrawPushClipPath(self.resource, binary(clip_mask_id))
def push_defs(self):
"""Indicates that commands up to a terminating :class:`Drawing.pop_defs`
command create named elements (e.g. clip-paths, textures, etc.) which
may safely be processed earlier for the sake of efficiency.
.. versionadded:: 0.4.0
"""
library.DrawPushDefs(self.resource)
def push_pattern(self, pattern_id, left, top, width, height):
"""Indicates that subsequent commands up to a
:class:`Drawing.pop_pattern` command comprise the definition of a named
pattern. The pattern space is assigned top left corner coordinates, a
width and height, and becomes its own drawing space. Anything which can
be drawn may be used in a pattern definition.
Named patterns may be used as stroke or brush definitions.
:param pattern_id: a unique identifier for the pattern.
:type pattern_id: :class:`basestring`
:param left: x ordinate of top left corner.
:type left: :class:`numbers.Real`
:param top: y ordinate of top left corner.
:type top: :class:`numbers.Real`
:param width: width of pattern space.
:type width: :class:`numbers.Real`
:param height: height of pattern space.
:type height: :class:`numbers.Real`
:returns: success of push operation
:rtype: `bool`
.. versionadded:: 0.4.0
"""
if not isinstance(pattern_id, string_type):
raise TypeError('pattern_id must be a string, not ' +
repr(pattern_id))
elif not isinstance(left, numbers.Real):
raise TypeError('left must be numbers.Real, not ' + repr(left))
elif not isinstance(top, numbers.Real):
raise TypeError('top must be numbers.Real, not ' + repr(top))
elif not isinstance(width, numbers.Real):
raise TypeError('width must be numbers.Real, not ' + repr(width))
elif not isinstance(height, numbers.Real):
raise TypeError('height must be numbers.Real, not ' + repr(height))
okay = library.DrawPushPattern(self.resource, binary(pattern_id),
left, top,
width, height)
return bool(okay)
def rectangle(self, left=None, top=None, right=None, bottom=None,
width=None, height=None, radius=None, xradius=None,
yradius=None):
"""Draws a rectangle using the current :attr:`stoke_color`,
:attr:`stroke_width`, and :attr:`fill_color`.
.. sourcecode:: text
+--------------------------------------------------+
| ^ ^ |
| | | |
| top | |
| | | |
| v | |
| <-- left --> +-------------------+ bottom |
| | ^ | | |
| | <-- width --|---> | | |
| | height | | |
| | | | | |
| | v | | |
| +-------------------+ v |
| <--------------- right ----------> |
+--------------------------------------------------+
:param left: x-offset of the rectangle to draw
:type left: :class:`numbers.Real`
:param top: y-offset of the rectangle to draw
:type top: :class:`numbers.Real`
:param right: second x-offset of the rectangle to draw.
this parameter and ``width`` parameter are exclusive
each other
:type right: :class:`numbers.Real`
:param bottom: second y-offset of the rectangle to draw.
this parameter and ``height`` parameter are exclusive
each other
:type bottom: :class:`numbers.Real`
:param width: the :attr:`width` of the rectangle to draw.
this parameter and ``right`` parameter are exclusive
each other
:type width: :class:`numbers.Real`
:param height: the :attr:`height` of the rectangle to draw.
this parameter and ``bottom`` parameter are exclusive
each other
:type height: :class:`numbers.Real`
:param radius: the corner rounding. this is a short-cut for setting
both :attr:`xradius`, and :attr:`yradius`
:type radius: :class:`numbers.Real`
:param xradius: the :attr:`xradius` corner in horizontal direction.
:type xradius: :class:`numbers.Real`
:param yradius: the :attr:`yradius` corner in vertical direction.
:type yradius: :class:`numbers.Real`
.. versionadded:: 0.3.6
.. versionchanged:: 0.4.0
Radius keywords added to create rounded rectangle.
"""
if left is None:
raise TypeError('left is missing')
elif top is None:
raise TypeError('top is missing')
elif right is None and width is None:
raise TypeError('right/width is missing')
elif bottom is None and height is None:
raise TypeError('bottom/height is missing')
elif not (right is None or width is None):
raise TypeError('parameters right and width are exclusive each '
'other; use one at a time')
elif not (bottom is None or height is None):
raise TypeError('parameters bottom and height are exclusive each '
'other; use one at a time')
elif not isinstance(left, numbers.Real):
raise TypeError('left must be numbers.Real, not ' + repr(left))
elif not isinstance(top, numbers.Real):
raise TypeError('top must be numbers.Real, not ' + repr(top))
elif not (right is None or isinstance(right, numbers.Real)):
raise TypeError('right must be numbers.Real, not ' + repr(right))
elif not (bottom is None or isinstance(bottom, numbers.Real)):
raise TypeError('bottom must be numbers.Real, not ' + repr(bottom))
elif not (width is None or isinstance(width, numbers.Real)):
raise TypeError('width must be numbers.Real, not ' + repr(width))
elif not (height is None or isinstance(height, numbers.Real)):
raise TypeError('height must be numbers.Real, not ' + repr(height))
if right is None:
if width < 0:
raise ValueError('width must be positive, not ' + repr(width))
right = left + width
elif right < left:
raise ValueError('right must be more than left ({0!r}), '
'not {1!r})'.format(left, right))
if bottom is None:
if height < 0:
raise ValueError('height must be positive, not ' +
repr(height))
bottom = top + height
elif bottom < top:
raise ValueError('bottom must be more than top ({0!r}), '
'not {1!r})'.format(top, bottom))
if radius is not None:
xradius = yradius = radius
if xradius is not None or yradius is not None:
if xradius is None:
xradius = 0.0
if yradius is None:
yradius = 0.0
if not isinstance(xradius, numbers.Real):
raise TypeError('xradius must be numbers.Real, not ' +
repr(xradius))
if not isinstance(yradius, numbers.Real):
raise TypeError('yradius must be numbers.Real, not ' +
repr(xradius))
library.DrawRoundRectangle(self.resource, left, top, right, bottom,
xradius, yradius)
else:
library.DrawRectangle(self.resource, left, top, right, bottom)
self.raise_exception()
def rotate(self, degree):
"""Applies the specified rotation to the current coordinate space.
:param degree: degree to rotate
:type degree: :class:`~numbers.Real`
.. versionadded:: 0.4.0
"""
library.DrawRotate(self.resource, float(degree))
def polygon(self, points=None):
"""Draws a polygon using the current :attr:`stoke_color`,
:attr:`stroke_width`, and :attr:`fill_color`, using the specified
array of coordinates.
Example polygon on ``image`` ::
with Drawing() as draw:
points = [(40,10), (20,50), (90,10), (70,40)]
draw.polygon(points)
draw.draw(image)
:param points: list of x,y tuples
:type points: :class:`list`
.. versionadded:: 0.4.0
"""
(points_l, points_p) = _list_to_point_info(points)
library.DrawPolygon(self.resource, points_l,
ctypes.cast(points_p, ctypes.POINTER(PointInfo)))
def polyline(self, points=None):
"""Draws a polyline using the current :attr:`stoke_color`,
:attr:`stroke_width`, and :attr:`fill_color`, using the specified
array of coordinates.
Identical to :class:`~wand.drawing.Drawing.polygon`, but without closed
stroke line.
:param points: list of x,y tuples
:type points: :class:`list`
.. versionadded:: 0.4.0
"""
(points_l, points_p) = _list_to_point_info(points)
library.DrawPolyline(self.resource, points_l,
ctypes.cast(points_p, ctypes.POINTER(PointInfo)))
def bezier(self, points=None):
"""Draws a bezier curve through a set of points on the image, using
the specified array of coordinates.
At least four points should be given to complete a bezier path.
The first & forth point being the start & end point, and the second
& third point controlling the direction & curve.
Example bezier on ``image`` ::
with Drawing() as draw:
points = [(40,10), # Start point
(20,50), # First control
(90,10), # Second control
(70,40)] # End point
draw.stroke_color = Color('#000')
draw.fill_color = Color('#fff')
draw.bezier(points)
draw.draw(image)
:param points: list of x,y tuples
:type points: :class:`list`
.. versionadded:: 0.4.0
"""
(points_l, points_p) = _list_to_point_info(points)
library.DrawBezier(self.resource, points_l,
ctypes.cast(points_p, ctypes.POINTER(PointInfo)))
def text(self, x, y, body):
"""Writes a text ``body`` into (``x``, ``y``).
:param x: the left offset where to start writing a text
:type x: :class:`numbers.Integral`
:param y: the baseline where to start writing text
:type y: :class:`numbers.Integral`
:param body: the body string to write
:type body: :class:`basestring`
"""
if not isinstance(x, numbers.Integral) or x < 0:
exc = ValueError if x < 0 else TypeError
raise exc('x must be a natural number, not ' + repr(x))
elif not isinstance(y, numbers.Integral) or y < 0:
exc = ValueError if y < 0 else TypeError
raise exc('y must be a natural number, not ' + repr(y))
elif not isinstance(body, string_type):
raise TypeError('body must be a string, not ' + repr(body))
elif not body:
raise ValueError('body string cannot be empty')
if isinstance(body, text_type):
# According to ImageMagick C API docs, we can use only UTF-8
# at this time, so we do hardcoding here.
# http://imagemagick.org/api/drawing-wand.php#DrawSetTextEncoding
if not self.text_encoding:
self.text_encoding = 'UTF-8'
body = body.encode(self.text_encoding)
body_p = ctypes.create_string_buffer(body)
library.DrawAnnotation(
self.resource, x, y,
ctypes.cast(body_p, ctypes.POINTER(ctypes.c_ubyte))
)
def scale(self, x=None, y=None):
"""
Adjusts the scaling factor to apply in the horizontal and vertical
directions to the current coordinate space.
:param x: Horizontal scale factor
:type x: :class:`~numbers.Real`
:param y: Vertical scale factor
:type y: :class:`~numbers.Real`
.. versionadded:: 0.4.0
"""
if not isinstance(x, numbers.Real):
raise TypeError('expecting numbers.Real, not ' + repr(x))
if not isinstance(y, numbers.Real):
raise TypeError('expecting numbers.Real, not ' + repr(y))
library.DrawScale(self.resource, x, y)
def set_fill_pattern_url(self, url):
"""Sets the URL to use as a fill pattern for filling objects. Only local
URLs ("#identifier") are supported at this time. These local URLs are
normally created by defining a named fill pattern with
Drawing.push_pattern & Drawing.pop_pattern.
:param url: URL to use to obtain fill pattern.
:type url: :class:`basestring`
.. versionadded:: 0.4.0
"""
if not isinstance(url, string_type):
raise TypeError('expecting basestring, not ' + repr(url))
if url[0] != '#':
raise ValueError('value not a relative URL, '
'expecting "#identifier"')
okay = library.DrawSetFillPatternURL(self.resource, binary(url))
if okay == 0:
# ThrowDrawException(DrawError,"URLNotFound",fill_url)
self.raise_exception()
def set_stroke_pattern_url(self, url):
"""Sets the pattern used for stroking object outlines. Only local
URLs ("#identifier") are supported at this time. These local URLs are
normally created by defining a named stroke pattern with
Drawing.push_pattern & Drawing.pop_pattern.
:param url: URL to use to obtain stroke pattern.
:type url: :class:`basestring`
.. versionadded:: 0.4.0
"""
if not isinstance(url, string_type):
raise TypeError('expecting basestring, not ' + repr(url))
if url[0] != '#':
raise ValueError('value not a relative URL, '
'expecting "#identifier"')
okay = library.DrawSetStrokePatternURL(self.resource, binary(url))
if okay == 0:
# ThrowDrawException(DrawError,"URLNotFound",fill_url)
self.raise_exception()
def skew(self, x=None, y=None):
"""Skews the current coordinate system in the horizontal direction if
``x`` is given, and vertical direction if ``y`` is given.
:param x: Skew horizontal direction
:type x: :class:`~numbers.Real`
:param y: Skew vertical direction
:type y: :class:`~numbers.Real`
.. versionadded:: 0.4.0
"""
if x is not None:
library.DrawSkewX(self.resource, float(x))
if y is not None:
library.DrawSkewY(self.resource, float(y))
def translate(self, x=None, y=None):
"""Applies a translation to the current coordinate system which moves
the coordinate system origin to the specified coordinate.
:param x: Skew horizontal direction
:type x: :class:`~numbers.Real`
:param y: Skew vertical direction
:type y: :class:`~numbers.Real`
.. versionadded:: 0.4.0
"""
if x is None or y is None:
raise TypeError('Both x & y coordinates need to be defined')
library.DrawTranslate(self.resource, float(x), float(y))
def get_font_metrics(self, image, text, multiline=False):
"""Queries font metrics from the given ``text``.
:param image: the image to be drawn
:type image: :class:`~wand.image.Image`
:param text: the text string for get font metrics.
:type text: :class:`basestring`
:param multiline: text is multiline or not
:type multiline: `boolean`
"""
if not isinstance(image, Image):
raise TypeError('image must be a wand.image.Image instance, not '
+ repr(image))
if not isinstance(text, string_type):
raise TypeError('text must be a string, not ' + repr(text))
if multiline:
font_metrics_f = library.MagickQueryMultilineFontMetrics
else:
font_metrics_f = library.MagickQueryFontMetrics
if isinstance(text, text_type):
if self.text_encoding:
text = text.encode(self.text_encoding)
else:
text = binary(text)
result = font_metrics_f(image.wand, self.resource, text)
args = (result[i] for i in xrange(13))
return FontMetrics(*args)
def viewbox(self, left, top, right, bottom):
"""Viewbox sets the overall canvas size to be recorded with the drawing
vector data. Usually this will be specified using the same size as the
canvas image. When the vector data is saved to SVG or MVG formats, the
viewbox is use to specify the size of the canvas image that a viewer
will render the vector data on.
:param left: the left most point of the viewbox.
:type left: :class:`~numbers.Integral`
:param top: the top most point of the viewbox.
:type top: :class:`~numbers.Integral`
:param right: the right most point of the viewbox.
:type right: :class:`~numbers.Integral`
:param bottom: the bottom most point of the viewbox.
:type bottom: :class:`~numbers.Integral`
.. versionadded:: 0.4.0
"""
if not isinstance(left, numbers.Integral):
raise TypeError('left must be an integer, not ' + repr(left))
if not isinstance(top, numbers.Integral):
raise TypeError('top must be an integer, not ' + repr(top))
if not isinstance(right, numbers.Integral):
raise TypeError('right must be an integer, not ' + repr(right))
if not isinstance(bottom, numbers.Integral):
raise TypeError('bottom must be an integer, not ' + repr(bottom))
library.DrawSetViewbox(self.resource, left, top, right, bottom)
def __call__(self, image):
return self.draw(image)
def _list_to_point_info(points):
"""
Helper method to convert a list of tuples to ``const * PointInfo``
:param points: a list of tuples
:type points: `list`
:returns: tuple of point length and c_double array
:rtype: `tuple`
:raises: `TypeError`
.. versionadded:: 0.4.0
"""
if not isinstance(points, list):
raise TypeError('points must be a list, not ' + repr(points))
point_length = len(points)
tuple_size = 2
point_info_size = point_length * tuple_size
# Allocate sequence of memory
point_info = (ctypes.c_double * point_info_size)()
for double_index in xrange(0, point_info_size):
tuple_index = double_index // tuple_size
tuple_offset = double_index % tuple_size
point_info[double_index] = ctypes.c_double(
points[tuple_index][tuple_offset]
)
return (point_length, point_info)
""":mod:`wand.exceptions` --- Errors and warnings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This module maps MagickWand API's errors and warnings to Python's native
exceptions and warnings. You can catch all MagickWand errors using Python's
natural way to catch errors.
.. seealso::
`ImageMagick Exceptions <http://www.imagemagick.org/script/exception.php>`_
.. versionadded:: 0.1.1
"""
class WandException(Exception):
"""All Wand-related exceptions are derived from this class."""
class WandWarning(WandException, Warning):
"""Base class for Wand-related warnings."""
class WandError(WandException):
"""Base class for Wand-related errors."""
class WandFatalError(WandException):
"""Base class for Wand-related fatal errors."""
class WandLibraryVersionError(WandException):
"""Base class for Wand-related ImageMagick version errors.
.. versionadded:: 0.3.2
"""
#: (:class:`list`) A list of error/warning domains, these descriptions and
#: codes. The form of elements is like: (domain name, description, codes).
DOMAIN_MAP = [
('ResourceLimit',
'A program resource is exhausted e.g. not enough memory.',
(MemoryError,),
[300, 400, 700]),
('Type', 'A font is unavailable; a substitution may have occurred.', (),
[305, 405, 705]),
('Option', 'A command-line option was malformed.', (), [310, 410, 710]),
('Delegate', 'An ImageMagick delegate failed to complete.', (),
[315, 415, 715]),
('MissingDelegate',
'The image type can not be read or written because the appropriate; '
'delegate is missing.',
(ImportError,),
[320, 420, 720]),
('CorruptImage', 'The image file may be corrupt.',
(ValueError,), [325, 425, 725]),
('FileOpen', 'The image file could not be opened for reading or writing.',
(IOError,), [330, 430, 730]),
('Blob', 'A binary large object could not be allocated, read, or written.',
(IOError,), [335, 435, 735]),
('Stream', 'There was a problem reading or writing from a stream.',
(IOError,), [340, 440, 740]),
('Cache', 'Pixels could not be read or written to the pixel cache.',
(), [345, 445, 745]),
('Coder', 'There was a problem with an image coder.', (), [350, 450, 750]),
('Module', 'There was a problem with an image module.', (),
[355, 455, 755]),
('Draw', 'A drawing operation failed.', (), [360, 460, 760]),
('Image', 'The operation could not complete due to an incompatible image.',
(), [365, 465, 765]),
('Wand', 'There was a problem specific to the MagickWand API.', (),
[370, 470, 770]),
('Random', 'There is a problem generating a true or pseudo-random number.',
(), [375, 475, 775]),
('XServer', 'An X resource is unavailable.', (), [380, 480, 780]),
('Monitor', 'There was a problem activating the progress monitor.', (),
[385, 485, 785]),
('Registry', 'There was a problem getting or setting the registry.', (),
[390, 490, 790]),
('Configure', 'There was a problem getting a configuration file.', (),
[395, 495, 795]),
('Policy',
'A policy denies access to a delegate, coder, filter, path, or resource.',
(), [399, 499, 799])
]
#: (:class:`list`) The list of (base_class, suffix) pairs (for each code).
#: It would be zipped with :const:`DOMAIN_MAP` pairs' last element.
CODE_MAP = [
(WandWarning, 'Warning'),
(WandError, 'Error'),
(WandFatalError, 'FatalError')
]
#: (:class:`dict`) The dictionary of (code, exc_type).
TYPE_MAP = {}
for domain, description, bases, codes in DOMAIN_MAP:
for code, (base, suffix) in zip(codes, CODE_MAP):
name = domain + suffix
locals()[name] = TYPE_MAP[code] = type(name, (base,) + bases, {
'__doc__': description,
'wand_error_code': code
})
del name, base, suffix
""":mod:`wand.font` --- Fonts
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. versionadded:: 0.3.0
:class:`Font` is an object which takes the :attr:`~Font.path` of font file,
:attr:`~Font.size`, :attr:`~Font.color`, and whether to use
:attr:`~Font.antialias`\ ing. If you want to use font by its name rather
than the file path, use TTFQuery_ package. The font path resolution by its
name is a very complicated problem to achieve.
.. seealso::
TTFQuery_ --- Find and Extract Information from TTF Files
TTFQuery builds on the `FontTools-TTX`_ package to allow the Python
programmer to accomplish a number of tasks:
- query the system to find installed fonts
- retrieve metadata about any TTF font file
- this includes the glyph outlines (shape) of individual code-points,
which allows for rendering the glyphs in 3D (such as is done in
OpenGLContext)
- lookup/find fonts by:
- abstract family type
- proper font name
- build simple metadata registries for run-time font matching
.. _TTFQuery: http://ttfquery.sourceforge.net/
.. _FontTools-TTX: http://sourceforge.net/projects/fonttools/
"""
import numbers
from .color import Color
from .compat import string_type, text
__all__ = 'Font',
class Font(tuple):
"""Font struct which is a subtype of :class:`tuple`.
:param path: the path of the font file
:type path: :class:`str`, :class:`basestring`
:param size: the size of typeface. 0 by default which means *autosized*
:type size: :class:`numbers.Real`
:param color: the color of typeface. black by default
:type color: :class:`~wand.color.Color`
:param antialias: whether to use antialiasing. :const:`True` by default
:type antialias: :class:`bool`
.. versionchanged:: 0.3.9
The ``size`` parameter becomes optional. Its default value is
0, which means *autosized*.
"""
def __new__(cls, path, size=0, color=None, antialias=True):
if not isinstance(path, string_type):
raise TypeError('path must be a string, not ' + repr(path))
if not isinstance(size, numbers.Real):
raise TypeError('size must be a real number, not ' + repr(size))
if color is None:
color = Color('black')
elif not isinstance(color, Color):
raise TypeError('color must be an instance of wand.color.Color, '
'not ' + repr(color))
path = text(path)
return tuple.__new__(cls, (path, size, color, bool(antialias)))
@property
def path(self):
"""(:class:`basestring`) The path of font file."""
return self[0]
@property
def size(self):
"""(:class:`numbers.Real`) The font size in pixels."""
return self[1]
@property
def color(self):
"""(:class:`wand.color.Color`) The font color."""
return self[2]
@property
def antialias(self):
"""(:class:`bool`) Whether to apply antialiasing (``True``)
or not (``False``).
"""
return self[3]
def __repr__(self):
return '{0.__module__}.{0.__name__}({1})'.format(
type(self),
tuple.__repr__(self)
)
This source diff could not be displayed because it is too large. You can view the blob instead.
""":mod:`wand.resource` --- Global resource management
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is the global resource to manage in MagickWand API. This module
implements automatic global resource management through reference counting.
"""
import contextlib
import ctypes
import warnings
from .api import library
from .compat import string_type
from .exceptions import TYPE_MAP, WandException
__all__ = ('genesis', 'terminus', 'increment_refcount', 'decrement_refcount',
'Resource', 'DestroyedResourceError')
def genesis():
"""Instantiates the MagickWand API.
.. warning::
Don't call this function directly. Use :func:`increment_refcount()` and
:func:`decrement_refcount()` functions instead.
"""
library.MagickWandGenesis()
def terminus():
"""Cleans up the MagickWand API.
.. warning::
Don't call this function directly. Use :func:`increment_refcount()` and
:func:`decrement_refcount()` functions instead.
"""
library.MagickWandTerminus()
#: (:class:`numbers.Integral`) The internal integer value that maintains
#: the number of referenced objects.
#:
#: .. warning::
#:
#: Don't touch this global variable. Use :func:`increment_refcount()` and
#: :func:`decrement_refcount()` functions instead.
#:
reference_count = 0
def increment_refcount():
"""Increments the :data:`reference_count` and instantiates the MagickWand
API if it is the first use.
"""
global reference_count
if reference_count:
reference_count += 1
else:
genesis()
reference_count = 1
def decrement_refcount():
"""Decrements the :data:`reference_count` and cleans up the MagickWand
API if it will be no more used.
"""
global reference_count
if not reference_count:
raise RuntimeError('wand.resource.reference_count is already zero')
reference_count -= 1
if not reference_count:
terminus()
class Resource(object):
"""Abstract base class for MagickWand object that requires resource
management. Its all subclasses manage the resource semiautomatically
and support :keyword:`with` statement as well::
with Resource() as resource:
# use the resource...
pass
It doesn't implement constructor by itself, so subclasses should
implement it. Every constructor should assign the pointer of its
resource data into :attr:`resource` attribute inside of :keyword:`with`
:meth:`allocate()` context. For example::
class Pizza(Resource):
'''My pizza yummy.'''
def __init__(self):
with self.allocate():
self.resource = library.NewPizza()
.. versionadded:: 0.1.2
"""
#: (:class:`ctypes.CFUNCTYPE`) The :mod:`ctypes` predicate function
#: that returns whether the given pointer (that contains a resource data
#: usuaully) is a valid resource.
#:
#: .. note::
#:
#: It is an abstract attribute that has to be implemented
#: in the subclass.
c_is_resource = NotImplemented
#: (:class:`ctypes.CFUNCTYPE`) The :mod:`ctypes` function that destroys
#: the :attr:`resource`.
#:
#: .. note::
#:
#: It is an abstract attribute that has to be implemented
#: in the subclass.
c_destroy_resource = NotImplemented
#: (:class:`ctypes.CFUNCTYPE`) The :mod:`ctypes` function that gets
#: an exception from the :attr:`resource`.
#:
#: .. note::
#:
#: It is an abstract attribute that has to be implemented
#: in the subclass.
c_get_exception = NotImplemented
#: (:class:`ctypes.CFUNCTYPE`) The :mod:`ctypes` function that clears
#: an exception of the :attr:`resource`.
#:
#: .. note::
#:
#: It is an abstract attribute that has to be implemented
#: in the subclass.
c_clear_exception = NotImplemented
@property
def resource(self):
"""Internal pointer to the resource instance. It may raise
:exc:`DestroyedResourceError` when the resource has destroyed already.
"""
if getattr(self, 'c_resource', None) is None:
raise DestroyedResourceError(repr(self) + ' is destroyed already')
return self.c_resource
@resource.setter
def resource(self, resource):
# Delete the existing resource if there is one
if getattr(self, 'c_resource', None):
self.destroy()
if self.c_is_resource(resource):
self.c_resource = resource
else:
raise TypeError(repr(resource) + ' is an invalid resource')
increment_refcount()
@resource.deleter
def resource(self):
self.c_destroy_resource(self.resource)
self.c_resource = None
@contextlib.contextmanager
def allocate(self):
"""Allocates the memory for the resource explicitly. Its subclasses
should assign the created resource into :attr:`resource` attribute
inside of this context. For example::
with resource.allocate():
resource.resource = library.NewResource()
"""
increment_refcount()
try:
yield self
except:
decrement_refcount()
raise
def destroy(self):
"""Cleans up the resource explicitly. If you use the resource in
:keyword:`with` statement, it was called implicitly so have not to
call it.
"""
del self.resource
decrement_refcount()
def get_exception(self):
"""Gets a current exception instance.
:returns: a current exception. it can be ``None`` as well if any
errors aren't occurred
:rtype: :class:`wand.exceptions.WandException`
"""
severity = ctypes.c_int()
desc = self.c_get_exception(self.resource, ctypes.byref(severity))
if severity.value == 0:
return
self.c_clear_exception(self.wand)
exc_cls = TYPE_MAP[severity.value]
message = desc.value
if not isinstance(message, string_type):
message = message.decode(errors='replace')
return exc_cls(message)
def raise_exception(self, stacklevel=1):
"""Raises an exception or warning if it has occurred."""
e = self.get_exception()
if isinstance(e, Warning):
warnings.warn(e, stacklevel=stacklevel + 1)
elif isinstance(e, Exception):
raise e
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.destroy()
def __del__(self):
try:
self.destroy()
except DestroyedResourceError:
pass
class DestroyedResourceError(WandException, ReferenceError, AttributeError):
"""An error that rises when some code tries access to an already
destroyed resource.
.. versionchanged:: 0.3.0
It becomes a subtype of :exc:`wand.exceptions.WandException`.
"""
""":mod:`wand.sequence` --- Sequences
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. versionadded:: 0.3.0
"""
import collections
import contextlib
import ctypes
import numbers
from .api import libmagick, library
from .compat import binary, xrange
from .image import BaseImage, ImageProperty
from .version import MAGICK_VERSION_INFO
__all__ = 'Sequence', 'SingleImage'
class Sequence(ImageProperty, collections.MutableSequence):
"""The list-like object that contains every :class:`SingleImage`
in the :class:`~wand.image.Image` container. It implements
:class:`collections.Sequence` prototocol.
.. versionadded:: 0.3.0
"""
def __init__(self, image):
super(Sequence, self).__init__(image)
self.instances = []
def __del__(self):
for instance in self.instances:
if instance is not None:
instance.c_resource = None
@property
def current_index(self):
"""(:class:`numbers.Integral`) The current index of
its internal iterator.
.. note::
It's only for internal use.
"""
return library.MagickGetIteratorIndex(self.image.wand)
@current_index.setter
def current_index(self, index):
library.MagickSetIteratorIndex(self.image.wand, index)
@contextlib.contextmanager
def index_context(self, index):
"""Scoped setter of :attr:`current_index`. Should be
used for :keyword:`with` statement e.g.::
with image.sequence.index_context(3):
print(image.size)
.. note::
It's only for internal use.
"""
index = self.validate_position(index)
tmp_idx = self.current_index
self.current_index = index
yield index
self.current_index = tmp_idx
def __len__(self):
return library.MagickGetNumberImages(self.image.wand)
def validate_position(self, index):
if not isinstance(index, numbers.Integral):
raise TypeError('index must be integer, not ' + repr(index))
length = len(self)
if index >= length or index < -length:
raise IndexError(
'out of index: {0} (total: {1})'.format(index, length)
)
if index < 0:
index += length
return index
def validate_slice(self, slice_, as_range=False):
if not (slice_.step is None or slice_.step == 1):
raise ValueError('slicing with step is unsupported')
length = len(self)
if slice_.start is None:
start = 0
elif slice_.start < 0:
start = length + slice_.start
else:
start = slice_.start
start = min(length, start)
if slice_.stop is None:
stop = 0
elif slice_.stop < 0:
stop = length + slice_.stop
else:
stop = slice_.stop
stop = min(length, stop or length)
return xrange(start, stop) if as_range else slice(start, stop, None)
def __getitem__(self, index):
if isinstance(index, slice):
slice_ = self.validate_slice(index)
return [self[i] for i in xrange(slice_.start, slice_.stop)]
index = self.validate_position(index)
instances = self.instances
instances_length = len(instances)
if index < instances_length:
instance = instances[index]
if (instance is not None and
getattr(instance, 'c_resource', None) is not None):
return instance
else:
number_to_extend = index - instances_length + 1
instances.extend(None for _ in xrange(number_to_extend))
wand = self.image.wand
tmp_idx = library.MagickGetIteratorIndex(wand)
library.MagickSetIteratorIndex(wand, index)
image = library.GetImageFromMagickWand(wand)
exc = libmagick.AcquireExceptionInfo()
single_image = libmagick.CloneImages(image, binary(str(index)), exc)
libmagick.DestroyExceptionInfo(exc)
single_wand = library.NewMagickWandFromImage(single_image)
single_image = libmagick.DestroyImage(single_image)
library.MagickSetIteratorIndex(wand, tmp_idx)
instance = SingleImage(single_wand, self.image, image)
self.instances[index] = instance
return instance
def __setitem__(self, index, image):
if isinstance(index, slice):
tmp_idx = self.current_index
slice_ = self.validate_slice(index)
del self[slice_]
self.extend(image, offset=slice_.start)
self.current_index = tmp_idx
else:
if not isinstance(image, BaseImage):
raise TypeError('image must be an instance of wand.image.'
'BaseImage, not ' + repr(image))
with self.index_context(index) as index:
library.MagickRemoveImage(self.image.wand)
library.MagickAddImage(self.image.wand, image.wand)
def __delitem__(self, index):
if isinstance(index, slice):
range_ = self.validate_slice(index, as_range=True)
for i in reversed(range_):
del self[i]
else:
with self.index_context(index) as index:
library.MagickRemoveImage(self.image.wand)
if index < len(self.instances):
del self.instances[index]
def insert(self, index, image):
try:
index = self.validate_position(index)
except IndexError:
index = len(self)
if not isinstance(image, BaseImage):
raise TypeError('image must be an instance of wand.image.'
'BaseImage, not ' + repr(image))
if not self:
library.MagickAddImage(self.image.wand, image.wand)
elif index == 0:
tmp_idx = self.current_index
self_wand = self.image.wand
wand = image.sequence[0].wand
try:
# Prepending image into the list using MagickSetFirstIterator()
# and MagickAddImage() had not worked properly, but was fixed
# since 6.7.6-0 (rev7106).
if MAGICK_VERSION_INFO >= (6, 7, 6, 0):
library.MagickSetFirstIterator(self_wand)
library.MagickAddImage(self_wand, wand)
else:
self.current_index = 0
library.MagickAddImage(self_wand,
self.image.sequence[0].wand)
self.current_index = 0
library.MagickAddImage(self_wand, wand)
self.current_index = 0
library.MagickRemoveImage(self_wand)
finally:
self.current_index = tmp_idx
else:
with self.index_context(index - 1):
library.MagickAddImage(self.image.wand, image.sequence[0].wand)
self.instances.insert(index, None)
def append(self, image):
if not isinstance(image, BaseImage):
raise TypeError('image must be an instance of wand.image.'
'BaseImage, not ' + repr(image))
wand = self.image.wand
tmp_idx = self.current_index
try:
library.MagickSetLastIterator(wand)
library.MagickAddImage(wand, image.sequence[0].wand)
finally:
self.current_index = tmp_idx
self.instances.append(None)
def extend(self, images, offset=None):
tmp_idx = self.current_index
wand = self.image.wand
length = 0
try:
if offset is None:
library.MagickSetLastIterator(self.image.wand)
else:
if offset == 0:
images = iter(images)
self.insert(0, next(images))
offset += 1
self.current_index = offset - 1
if isinstance(images, type(self)):
library.MagickAddImage(wand, images.image.wand)
length = len(images)
else:
delta = 1 if MAGICK_VERSION_INFO >= (6, 7, 6, 0) else 2
for image in images:
if not isinstance(image, BaseImage):
raise TypeError(
'images must consist of only instances of '
'wand.image.BaseImage, not ' + repr(image)
)
else:
library.MagickAddImage(wand, image.sequence[0].wand)
self.instances = []
if offset is None:
library.MagickSetLastIterator(self.image.wand)
else:
self.current_index += delta
length += 1
finally:
self.current_index = tmp_idx
null_list = [None] * length
if offset is None:
self.instances[offset:] = null_list
else:
self.instances[offset:offset] = null_list
def _repr_png_(self):
library.MagickResetIterator(self.image.wand)
repr_wand = library.MagickAppendImages(self.image.wand, 1)
length = ctypes.c_size_t()
blob_p = library.MagickGetImagesBlob(repr_wand,
ctypes.byref(length))
if blob_p and length.value:
blob = ctypes.string_at(blob_p, length.value)
library.MagickRelinquishMemory(blob_p)
return blob
else:
return None
class SingleImage(BaseImage):
"""Each single image in :class:`~wand.image.Image` container.
For example, it can be a frame of GIF animation.
Note that all changes on single images are invisible to their
containers until they are :meth:`~wand.image.BaseImage.close`\ d
(:meth:`~wand.resource.Resource.destroy`\ ed).
.. versionadded:: 0.3.0
"""
#: (:class:`wand.image.Image`) The container image.
container = None
def __init__(self, wand, container, c_original_resource):
super(SingleImage, self).__init__(wand)
self.container = container
self.c_original_resource = c_original_resource
self._delay = None
@property
def sequence(self):
return self,
@property
def index(self):
"""(:class:`numbers.Integral`) The index of the single image in
the :attr:`container` image.
"""
wand = self.container.wand
library.MagickResetIterator(wand)
image = library.GetImageFromMagickWand(wand)
i = 0
while self.c_original_resource != image and image:
image = libmagick.GetNextImageInList(image)
i += 1
assert image
assert self.c_original_resource == image
return i
@property
def delay(self):
"""(:class:`numbers.Integral`) The delay to pause before display
the next image (in the :attr:`~wand.image.BaseImage.sequence` of
its :attr:`container`). It's hundredths of a second.
"""
if self._delay is None:
container = self.container
with container.sequence.index_context(self.index):
self._delay = library.MagickGetImageDelay(container.wand)
return self._delay
@delay.setter
def delay(self, delay):
if not isinstance(delay, numbers.Integral):
raise TypeError('delay must be an integer, not ' + repr(delay))
elif delay < 0:
raise ValueError('delay cannot be less than zero')
self._delay = delay
def destroy(self):
if self.dirty:
self.container.sequence[self.index] = self
if self._delay is not None:
container = self.container
with container.sequence.index_context(self.index):
library.MagickSetImageDelay(container.wand, self._delay)
super(SingleImage, self).destroy()
def __repr__(self):
cls = type(self)
if getattr(self, 'c_resource', None) is None:
return '<{0}.{1}: (closed)>'.format(cls.__module__, cls.__name__)
return '<{0}.{1}: {2} ({3}x{4})>'.format(
cls.__module__, cls.__name__,
self.signature[:7], self.width, self.height
)
""":mod:`wand.version` --- Version data
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can find the current version in the command line interface:
.. sourcecode:: console
$ python -m wand.version
0.0.0
$ python -m wand.version --verbose
Wand 0.0.0
ImageMagick 6.7.7-6 2012-06-03 Q16 http://www.imagemagick.org
$ python -m wand.version --config | grep CC | cut -d : -f 2
gcc -std=gnu99 -std=gnu99
$ python -m wand.version --fonts | grep Helvetica
Helvetica
Helvetica-Bold
Helvetica-Light
Helvetica-Narrow
Helvetica-Oblique
$ python -m wand.version --formats | grep CMYK
CMYK
CMYKA
.. versionadded:: 0.2.0
The command line interface.
.. versionadded:: 0.2.2
The ``--verbose``/``-v`` option which also prints ImageMagick library
version for CLI.
.. versionadded:: 0.4.1
The ``--fonts``, ``--formats``, & ``--config`` option allows printing
additional information about ImageMagick library.
"""
from __future__ import print_function
import ctypes
import datetime
import re
import sys
try:
from .api import libmagick, library
except ImportError:
libmagick = None
from .compat import binary, string_type, text
__all__ = ('VERSION', 'VERSION_INFO', 'MAGICK_VERSION',
'MAGICK_VERSION_INFO', 'MAGICK_VERSION_NUMBER',
'MAGICK_RELEASE_DATE', 'MAGICK_RELEASE_DATE_STRING',
'QUANTUM_DEPTH', 'configure_options', 'fonts', 'formats')
#: (:class:`tuple`) The version tuple e.g. ``(0, 1, 2)``.
#:
#: .. versionchanged:: 0.1.9
#: Becomes :class:`tuple`. (It was string before.)
VERSION_INFO = (0, 4, 2)
#: (:class:`basestring`) The version string e.g. ``'0.1.2'``.
#:
#: .. versionchanged:: 0.1.9
#: Becomes string. (It was :class:`tuple` before.)
VERSION = '{0}.{1}.{2}'.format(*VERSION_INFO)
if libmagick:
c_magick_version = ctypes.c_size_t()
#: (:class:`basestring`) The version string of the linked ImageMagick
#: library. The exactly same string to the result of
#: :c:func:`GetMagickVersion` function.
#:
#: Example::
#:
#: 'ImageMagick 6.7.7-6 2012-06-03 Q16 http://www.imagemagick.org'
#:
#: .. versionadded:: 0.2.1
MAGICK_VERSION = text(
libmagick.GetMagickVersion(ctypes.byref(c_magick_version))
)
#: (:class:`numbers.Integral`) The version number of the linked
#: ImageMagick library.
#:
#: .. versionadded:: 0.2.1
MAGICK_VERSION_NUMBER = c_magick_version.value
_match = re.match(r'^ImageMagick\s+(\d+)\.(\d+)\.(\d+)(?:-(\d+))?',
MAGICK_VERSION)
#: (:class:`tuple`) The version tuple e.g. ``(6, 7, 7, 6)`` of
#: :const:`MAGICK_VERSION`.
#:
#: .. versionadded:: 0.2.1
MAGICK_VERSION_INFO = tuple(int(v or 0) for v in _match.groups())
#: (:class:`datetime.date`) The release date of the linked ImageMagick
#: library. The same to the result of :c:func:`GetMagickReleaseDate`
#: function.
#:
#: .. versionadded:: 0.2.1
MAGICK_RELEASE_DATE_STRING = text(libmagick.GetMagickReleaseDate())
#: (:class:`basestring`) The date string e.g. ``'2012-06-03'`` of
#: :const:`MAGICK_RELEASE_DATE_STRING`. This value is the exactly same
#: string to the result of :c:func:`GetMagickReleaseDate` function.
#:
#: .. versionadded:: 0.2.1
MAGICK_RELEASE_DATE = datetime.date(
*map(int, MAGICK_RELEASE_DATE_STRING.split('-')))
c_quantum_depth = ctypes.c_size_t()
libmagick.GetMagickQuantumDepth(ctypes.byref(c_quantum_depth))
#: (:class:`numbers.Integral`) The quantum depth configuration of
#: the linked ImageMagick library. One of 8, 16, 32, or 64.
#:
#: .. versionadded:: 0.3.0
QUANTUM_DEPTH = c_quantum_depth.value
del c_magick_version, _match, c_quantum_depth
def configure_options(pattern='*'):
"""
Queries ImageMagick library for configurations options given at
compile-time.
Example: Find where the ImageMagick documents are installed::
>>> from wand.version import configure_options
>>> configure_options('DOC*')
{'DOCUMENTATION_PATH': '/usr/local/share/doc/ImageMagick-6'}
:param pattern: A term to filter queries against. Supports wildcard '*'
characters. Default patterns '*' for all options.
:type pattern: :class:`basestring`
:returns: Directory of configuration options matching given pattern
:rtype: :class:`collections.defaultdict`
"""
if not isinstance(pattern, string_type):
raise TypeError('pattern must be a string, not ' + repr(pattern))
pattern_p = ctypes.create_string_buffer(binary(pattern))
config_count = ctypes.c_size_t(0)
configs = {}
configs_p = library.MagickQueryConfigureOptions(pattern_p,
ctypes.byref(config_count))
cursor = 0
while cursor < config_count.value:
config = configs_p[cursor].value
value = library.MagickQueryConfigureOption(config)
configs[text(config)] = text(value.value)
cursor += 1
return configs
def fonts(pattern='*'):
"""
Queries ImageMagick library for available fonts.
Available fonts can be configured by defining `types.xml`,
`type-ghostscript.xml`, or `type-windows.xml`.
Use :func:`wand.version.configure_options` to locate system search path,
and `resources <http://www.imagemagick.org/script/resources.php>`_
article for defining xml file.
Example: List all bold Helvetica fonts::
>>> from wand.version import fonts
>>> fonts('*Helvetica*Bold*')
['Helvetica-Bold', 'Helvetica-Bold-Oblique', 'Helvetica-BoldOblique',
'Helvetica-Narrow-Bold', 'Helvetica-Narrow-BoldOblique']
:param pattern: A term to filter queries against. Supports wildcard '*'
characters. Default patterns '*' for all options.
:type pattern: :class:`basestring`
:returns: Sequence of matching fonts
:rtype: :class:`collections.Sequence`
"""
if not isinstance(pattern, string_type):
raise TypeError('pattern must be a string, not ' + repr(pattern))
pattern_p = ctypes.create_string_buffer(binary(pattern))
number_fonts = ctypes.c_size_t(0)
fonts = []
fonts_p = library.MagickQueryFonts(pattern_p,
ctypes.byref(number_fonts))
cursor = 0
while cursor < number_fonts.value:
font = fonts_p[cursor].value
fonts.append(text(font))
cursor += 1
return fonts
def formats(pattern='*'):
"""
Queries ImageMagick library for supported formats.
Example: List supported PNG formats::
>>> from wand.version import formats
>>> formats('PNG*')
['PNG', 'PNG00', 'PNG8', 'PNG24', 'PNG32', 'PNG48', 'PNG64']
:param pattern: A term to filter formats against. Supports wildcards '*'
characters. Default pattern '*' for all formats.
:type pattern: :class:`basestring`
:returns: Sequence of matching formats
:rtype: :class:`collections.Sequence`
"""
if not isinstance(pattern, string_type):
raise TypeError('pattern must be a string, not ' + repr(pattern))
pattern_p = ctypes.create_string_buffer(binary(pattern))
number_formats = ctypes.c_size_t(0)
formats = []
formats_p = library.MagickQueryFormats(pattern_p,
ctypes.byref(number_formats))
cursor = 0
while cursor < number_formats.value:
value = formats_p[cursor].value
formats.append(text(value))
cursor += 1
return formats
if __doc__ is not None:
__doc__ = __doc__.replace('0.0.0', VERSION)
del libmagick
if __name__ == '__main__':
options = frozenset(sys.argv[1:])
if '-v' in options or '--verbose' in options:
print('Wand', VERSION)
try:
print(MAGICK_VERSION)
except NameError:
pass
elif '--fonts' in options:
for font in fonts():
print(font)
elif '--formats' in options:
for supported_format in formats():
print(supported_format)
elif '--config' in options:
config_options = configure_options()
for key in config_options:
print('{:24s}: {}'.format(key, config_options[key]))
else:
print(VERSION)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment