wand.image — Image objects

Opens and manipulates images. Image objects can be used in with statement, and these resources will be automatically managed (even if any error happened):

with Image(filename='pikachu.png') as i:
    print 'width =', i.width
    print 'height =', i.height
wand.image.FILTER_TYPES = ('undefined', 'point', 'box', 'triangle', 'hermite', 'hanning', 'hamming', 'blackman', 'gaussian', 'quadratic', 'cubic', 'catrom', 'mitchell', 'lanczos', 'bessel', 'sinc')

(tuple) The list of filter types.

  • 'undefined'
  • 'point'
  • 'box'
  • 'triangle'
  • 'hermite'
  • 'hanning'
  • 'hamming'
  • 'blackman'
  • 'gaussian'
  • 'quadratic'
  • 'cubic'
  • 'catrom'
  • 'mitchell'
  • 'lanczos'
  • 'bessel'
  • 'sinc'

See also

ImageMagick Resize Filters
Demonstrates the results of resampling three images using the various resize filters and blur settings available in ImageMagick, and the file size of the resulting thumbnail images.
class wand.image.Image(image=None, blob=None, file=None, filename=None)

An image object.

Parameters:
  • image (Image) – makes an exact copy of the image
  • blob (str) – opens an image of the blob byte array
  • file (file object) – opens an image of the file object
  • filename (basestring) – opens an image of the filename string
[left:right, top:bottom]

Crops the image by its left, right, top and bottom, and then returns the cropped one.

with img[100:200, 150:300] as cropped:
    # manipulated the cropped image
    pass

Like other subscriptable objects, default is 0 or its width/height:

img[:, :]        #--> just clone
img[:100, 200:]  #--> equivalent to img[0:100, 200:img.height]

Negative integers count from the end (width/height):

img[-70:-50, -20:-10]
#--> equivalent to img[width-70:width-50, height-20:height-10]
Returns :the cropped image
Rtype :Image
background_color

(wand.color.Color) The image background color. It can also be set to change the background color.

clone()

Clones the image. It is equivalent to call Image with image parameter.

with img.clone() as cloned:
    # manipulate the cloned image
    pass
Returns:the cloned new image
Return type:Image
close()

Closes the image explicitly. If you use the image object in with statement, it was called implicitly so don’t have to call it.

Note

It has the same functionality of destroy() method.

composite(image, left, top)

Places the supplied image over the current image, with the top left corner of image at coordinates left, top of the current image. The dimensions of the current image are not changed.

Parameters:
convert(format)

Converts the image format with the original image maintained. It returns a converted image instance which is new.

with img.convert('png') as converted:
    converted.save(filename='converted.png')
Parameters:format (basestring) – image format to convert to
Returns:a converted image
Return type:Image
Raises :ValueError when the given format is unsupported
crop(left=0, top=0, right=None, bottom=None, width=None, height=None, reset_coords=True)

Crops the image in-place.

+--------------------------------------------------+
|              ^                         ^         |
|              |                         |         |
|             top                        |         |
|              |                         |         |
|              v                         |         |
| <-- left --> +-------------------+  bottom       |
|              |             ^     |     |         |
|              | <-- width --|---> |     |         |
|              |           height  |     |         |
|              |             |     |     |         |
|              |             v     |     |         |
|              +-------------------+     v         |
| <--------------- right ---------->               |
+--------------------------------------------------+
Parameters:
  • left (numbers.Integral) – x-offset of the cropped image. default is 0
  • top (numbers.Integral) – y-offset of the cropped image. default is 0
  • right (numbers.Integral) – second x-offset of the cropped image. default is the width of the image. this parameter and width parameter are exclusive each other
  • bottom (numbers.Integral) – second y-offset of the cropped image. default is the height of the image. this parameter and height parameter are exclusive each other
  • width (numbers.Integral) – the width of the cropped image. default is the width of the image. this parameter and right parameter are exclusive each other
  • height (numbers.Integral) – the height of the cropped image. default is the height of the image. this parameter and bottom parameter are exclusive each other
  • reset_coords (bool) – optional flag. If set, after the rotation, the coordinate frame will be relocated to the upper-left corner of the new image. By default is True.

Note

If you want to crop the image but not in-place, use slicing operator.

format

(basestring) The image format.

If you want to convert the image format, just reset this property:

assert isinstance(img, wand.image.Image)
img.format = 'png'

It may raise ValueError when the format is unsupported.

See also

ImageMagick Image Formats
ImageMagick uses an ASCII string known as magick (e.g. GIF) to identify file formats, algorithms acting as formats, built-in patterns, and embedded profile types.
height

(numbers.Integral) The height of this image.

make_blob(format=None)

Makes the binary string of the image.

Parameters:format (basestring) – the image format to write e.g. 'png', 'jpeg'. it is omittable
Returns:a blob (bytes) string
Return type:str
Raises :ValueError when format is invalid
mimetype

(basestring) The MIME type of the image e.g. 'image/jpeg', 'image/png'.

quantum_range

(int) The maxumim value of a color channel that is supported by the imagemgick library.

reset_coords()

Reset the coordinate frame of the image so to the upper-left corner is (0, 0) again (crop and rotate operations change it).

resize(width=None, height=None, filter='triangle', blur=1)

Resizes the image.

Parameters:
  • width (numbers.Integral) – the width in the scaled image. default is the original width
  • height (numbers.Integral) – the height in the scaled image. default is the original height
  • filter (basestring, numbers.Integral) – a filter type to use for resizing. choose one in FILTER_TYPES. default is 'triangle'
  • blur (numbers.Real) – the blur factor where > 1 is blurry, < 1 is sharp. default is 1
rotate(degree, background=None, reset_coords=True)

Rotates the image. It takes a background color for degree that isn’t a multiple of 90.

Parameters:
  • degree (numbers.Real) – a degree to rotate. multiples of 360 affect nothing
  • background (wand.color.Color) – an optional background color. default is transparent
  • reset_coords (bool) – optional flag. If set, after the rotation, the coordinate frame will be relocated to the upper-left corner of the new image. By default is True.
save(file=None, filename=None)

Saves the image into the file or filename. It takes only one argument at a time.

Parameters:
  • file (file object) – a file object to write to
  • filename (basename) – a filename string to write to
signature

(str) The SHA-256 message digest for the image pixel stream.

size

(tuple) The pair of (width, height).

transparentize(transparency)

Makes the image transparent by subtracting some percentage of the black color channel. The transparency parameter specifies the percentage.

Parameters:transparency (numbers.Real) – the percentage fade that should be performed on the image, from 0.0 to 1.0
wand

Internal pointer to the MagickWand instance. It may raise ClosedImageError when the instance has destroyed already.

watermark(image, transparency=0.0, left=0, top=0)

Transparentized the supplied image and places it over the current image, with the top left corner of image at coordinates left, top of the current image. The dimensions of the current image are not changed.

Parameters:
  • image (wand.image.Image) – the image placed over the current image
  • transparency (numbers.Real) – the percentage fade that should be performed on the image, from 0.0 to 1.0
  • left (numbers.Integral) – the x-coordinate where image will be placed
  • top (numbers.Integral) – the y-coordinate where image will be placed
width

(numbers.Integral) The width of this image.

class wand.image.Iterator(image=None, iterator=None)

Row iterator for Image. It shouldn’t be instantiated directly; instead, it can be acquired through Image instance:

assert isinstance(image, wand.image.Image)
iterator = iter(image)

It doesn’t iterate every pixel, but rows. For example:

for row in image:
    for col in row:
        assert isinstance(col, wand.color.Color)
        print col

Every row is a collections.Sequence which consists of one or more wand.color.Color values.

Parameters:image (Image) – the image to get an iterator
clone()

Clones the same iterator.

exception wand.image.ClosedImageError

An error that rises when some code tries access to an already closed image.

Related Topics

This Page