Ghilas BELHADJ
Ghilas BELHADJ

Reputation: 14096

Slice image with Wand

I want to extract multiple parts of an image with Wand.

I've just found a function for cropping (in-place) the image img.crop(left, top, right, bottom) but note the slicing one as they say in the doc.

Note

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

Upvotes: 0

Views: 752

Answers (1)

emcconville
emcconville

Reputation: 24419

Take a look at the test_slice_crop method in the test directory for examples.

with Image(filename='source.jpg') as img:
    with img[100:200, 100:200] as cropped:
        # The `cropped' is an instance if wand.image.Image,
        # and can be manipulated independently of `img' instance.
        pass

Edit

For completion, slice is a built-in function in python to represent a set of iterations (i.e. a[start:stop:step]). In , this is used to allow short-hand matrix iterations

wand_instance[x:width, y:height]

Here's an example of generating 10px columns...

from wand.image import Image

with Image(filename="rose:") as rose:
    x = 0
    chunk_size = 10
    while True:
        try:
            with rose[x:x+chunk_size, 0:rose.height] as chunk:
                chunk.save(filename='rose_{0}.png'.format(x))
                x += chunk_size
        except IndexError:
            break

sliced image

Upvotes: 2

Related Questions