Python-VIPS 是否支持分配其部分图像?
Does Python-VIPS support assignment to part of its image ?
我一直在使用 Python 3 和 Numpy 来处理图像处理任务,在该任务中我将小图块组合成一个大而完整的图像。
我会这样做:
canvas = np.zeros((max_y + tilesize_y, max_x + tilesize_x, 3), dtype='uint8')
其中 max_x
、max_y
是单个图像块的最大起始坐标。然后我会像这样将图像粘贴到这个大 canvas 中:
canvas[jj['YStart']: jj['YStart'] + tilesize_y, jj['XStart']: jj['XStart'] + tilesize_x] = temp_img
其中 jj
是文件列表的一个条目,记录了每个图块所属的位置。我想知道是否可以在 VIPS 中使用 Python 实现类似的操作?
VIPS没有破坏性操作:只能新建镜像,不能修改已有镜像。这个限制就是为什么 vips 可以做自动并行化和操作缓存之类的事情。
在幕后,它有一些额外的机制来使它不像听起来那么低效。您可以这样解决您的问题:
#!/usr/bin/python
import sys
import random
from gi.repository import Vips
# make vips leaktest itself .. this also reports a memory high-water mark
# you'll get a small speedup if you comment this out
Vips.leak_set(True)
composite = Vips.Image.black(10000, 10000)
for filename in sys.argv[1:]:
tile = Vips.Image.new_from_file(filename, access = Vips.Access.SEQUENTIAL)
x = random.randint(0, 10000)
y = random.randint(0, 10000)
composite = composite.insert(tile, x, y)
composite.write_to_file("output.tif")
这里有所有 vips 运营商的可搜索列表:
http://www.vips.ecs.soton.ac.uk/supported/current/doc/html/libvips/func-list.html
插入文档在这里:
虽然您似乎为每次迭代都制作了一个新的(巨大的)图像,但实际上在您的背后 vips 将共享图像并且只创建它需要的位。此外,在打开时设置 "sequential" 提示意味着 vips 可以在写入最终 tiff 时流式传输子图像。
运行 像这样:
$ time ./insert.py ~/pics/k*.jpg
memory: high-water mark 53.81 MB
real 0m1.913s
user 0m0.939s
sys 0m0.266s
$ ls ~/pics/k*.jpg | wc
8 8 278
那就是粘贴 8 张大的 jpg 图片。报告的内存使用是针对像素缓冲区的,它不包括所有内存。如果您尝试混合粘贴 RGB 和 RGBA 图像,此脚本将会中断,您需要添加一些内容来处理 alpha 通道。
(其实还有破坏性的粘贴操作:
http://www.vips.ecs.soton.ac.uk/supported/current/doc/html/libvips/libvips-draw.html#vips-draw-image
它适用于确实需要修改图像的画盒式程序,不太适合一般用途)
我一直在使用 Python 3 和 Numpy 来处理图像处理任务,在该任务中我将小图块组合成一个大而完整的图像。
我会这样做:
canvas = np.zeros((max_y + tilesize_y, max_x + tilesize_x, 3), dtype='uint8')
其中 max_x
、max_y
是单个图像块的最大起始坐标。然后我会像这样将图像粘贴到这个大 canvas 中:
canvas[jj['YStart']: jj['YStart'] + tilesize_y, jj['XStart']: jj['XStart'] + tilesize_x] = temp_img
其中 jj
是文件列表的一个条目,记录了每个图块所属的位置。我想知道是否可以在 VIPS 中使用 Python 实现类似的操作?
VIPS没有破坏性操作:只能新建镜像,不能修改已有镜像。这个限制就是为什么 vips 可以做自动并行化和操作缓存之类的事情。
在幕后,它有一些额外的机制来使它不像听起来那么低效。您可以这样解决您的问题:
#!/usr/bin/python
import sys
import random
from gi.repository import Vips
# make vips leaktest itself .. this also reports a memory high-water mark
# you'll get a small speedup if you comment this out
Vips.leak_set(True)
composite = Vips.Image.black(10000, 10000)
for filename in sys.argv[1:]:
tile = Vips.Image.new_from_file(filename, access = Vips.Access.SEQUENTIAL)
x = random.randint(0, 10000)
y = random.randint(0, 10000)
composite = composite.insert(tile, x, y)
composite.write_to_file("output.tif")
这里有所有 vips 运营商的可搜索列表:
http://www.vips.ecs.soton.ac.uk/supported/current/doc/html/libvips/func-list.html
插入文档在这里:
虽然您似乎为每次迭代都制作了一个新的(巨大的)图像,但实际上在您的背后 vips 将共享图像并且只创建它需要的位。此外,在打开时设置 "sequential" 提示意味着 vips 可以在写入最终 tiff 时流式传输子图像。
运行 像这样:
$ time ./insert.py ~/pics/k*.jpg
memory: high-water mark 53.81 MB
real 0m1.913s
user 0m0.939s
sys 0m0.266s
$ ls ~/pics/k*.jpg | wc
8 8 278
那就是粘贴 8 张大的 jpg 图片。报告的内存使用是针对像素缓冲区的,它不包括所有内存。如果您尝试混合粘贴 RGB 和 RGBA 图像,此脚本将会中断,您需要添加一些内容来处理 alpha 通道。
(其实还有破坏性的粘贴操作:
http://www.vips.ecs.soton.ac.uk/supported/current/doc/html/libvips/libvips-draw.html#vips-draw-image
它适用于确实需要修改图像的画盒式程序,不太适合一般用途)