Warm tip: This article is reproduced from serverfault.com, please click

imagemagick-使用python在图块中转换和裁剪图像

(imagemagick - convert and crop image in tiles with python)

发布于 2015-02-13 09:57:35

我想在python中平铺JPG图像。通常我使用imageMagick ..所以我比魔杖似乎能完成这项工作...

但是我无法翻译

 convert -crop 256x256 +repage big_image.jpg tiles_%d.jpg

有人可以帮我吗?

Questioner
delaye
Viewed
11
emcconville 2015-02-14 00:00:14

Python的魔杖库提供了独特的裁剪方法。使用wand.image.Image[left:right, top:bottom]可以对新的虚拟像素图像进行切片。

from wand.image import Image

with Image(filename="big_image.jpg") as img:
i = 0
for h in range(0, img.height, 256):
    for w in range(0, img.width, 256):
        w_end = w + 256
        h_end = h + 256
        with img[w:w_end, h:h_end] as chunk:
            chunk.save(filename='tiles_{0}.jpg'.format(i))
        i += 1

上面的代码会生成许多与以下+repage选项匹配的图块图像

convert -crop 256x256 +repage big_image.jpg tiles_%d.jpg