用魔杖切片图像

Slice image with Wand

我想用 Wand 提取图像的多个部分。

我刚刚找到了一个用于裁剪(就地)图像的功能 img.crop(left, top, right, bottom) 但请注意他们所说的切片功能 in the doc

Note

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

查看测试目录中的 test_slice_crop 方法以获取示例。

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

编辑

为了完成,slice is a built-in function in python to represent a set of iterations (i.e. a[start:stop:step]). In ,这用于允许简写矩阵迭代

wand_instance[x:width, y:height]

下面是生成 10px 列的示例...

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