Python 使用PyVIP连接多个大型图像
我正试图找出如何通过python将多个图像与VIP连接起来。我在一个文件夹中有30个(但可以超过600个)条纹png文件,它们的分辨率为854x289920(所有分辨率都相同) 如果我尝试用MemmoryError将它们水平连接在一起,python中的PIL将立即消失。所以我搜索了一下,找到了VIP,他们可以做我需要的两件事,加入图像,并根据结果制作深度缩放图像 不幸的是,我不知道如何在python中正确地水平连接它们Python 使用PyVIP连接多个大型图像,python,image,image-processing,vips,Python,Image,Image Processing,Vips,我正试图找出如何通过python将多个图像与VIP连接起来。我在一个文件夹中有30个(但可以超过600个)条纹png文件,它们的分辨率为854x289920(所有分辨率都相同) 如果我尝试用MemmoryError将它们水平连接在一起,python中的PIL将立即消失。所以我搜索了一下,找到了VIP,他们可以做我需要的两件事,加入图像,并根据结果制作深度缩放图像 不幸的是,我不知道如何在python中正确地水平连接它们 我在数组中有一个来自文件夹的图像列表,但我如何循环遍历它们并将合并的图像顺序
我在数组中有一个来自文件夹的图像列表,但我如何循环遍历它们并将合并的图像顺序写入磁盘 我还是用一些问题解决了:
import pyvips
list_of_pictures = []
for x in os.listdir(source):
list_of_pictures.append(source + x)
image = None
for i in list_of_pictures:
tile = pyvips.Image.new_from_file(i, access="sequential")
image = tile if not image else image.join(tile, "horizontal")
image.write_to_file(save_to)
是的,制作带有连接图片的tif。。。但是霍莉牛!原始图片是png(30x)加起来4.5GB,结果tiff是25GB!为什么会有这么大的尺寸差异呢?这似乎也适用于打开大量图像并在其上执行joinarray操作,以便它们彼此相邻。谢谢@user894763
import os
import pyvips
# natsort helps with sorting the list logically
from natsort import natsorted
source = r"E:/pics/"
output = r"E:/out/"
save_to = output + 'final' + '.tif'
# define list of pictures we are going to get from folder
list_of_pictures = []
# get the
for x in os.listdir(source):
list_of_pictures.append(source + x)
# list_of_pictures now contains all the images from folder including full path
# since os.listdir will not guarantee proper order of files we use natsorted to do it
list_of_pictures = natsorted(list_of_pictures)
array_images = []
image = None
# lets create array of the images for joining, using sequential so it use less ram
for i in list_of_pictures:
tile = pyvips.Image.new_from_file(i, access="sequential")
array_images.append(tile)
# Join them, across is how many pictures there be next to each other, so i just counted all pictures in array with len
out = pyvips.Image.arrayjoin(array_images, across=len(list_of_pictures))
# write it out to file....
out.write_to_file(save_to, Q=95, compression="lzw", bigtiff=True)
仅供参考,您也可以在命令行中执行此操作。尝试:
vips arrayjoin "a.png b.png c.png" mypyr.dz --across 3
将水平连接三个PNG图像,并将结果保存为名为mypyr
的深度缩放金字塔。arrayjoin文档具有以下所有选项:
您可以通过将金字塔生成器参数括在.dz
后面的方括号中来提供这些参数
vips arrayjoin "a.png b.png c.png" mypyr.dz[overlap=0,container=zip] --across 3
在Windows上,deepzoom金字塔的编写速度可能非常慢,因为Windows不喜欢创建文件,也不喜欢庞大的目录。如果您使用
container=zip
编写,VIP将直接创建一个包含金字塔的.zip文件。这使金字塔的创建速度提高了大约4倍。好的,这里有一些压缩选项:图像。写入文件(保存到,Q=95,compression=“lzw”,bigtiff=True)
其中有效选项是(jpeg,deflate,packbits,ccittfax4,lzw)jpeg=如果超过4GB,deflate=crash,packbits=13.2GB(45s制作),ccittfax4=wbuffer_write:write失败,lzw=5.88GB(151s制造)
到目前为止,lzw压缩似乎提供了最佳的结果大小vise,我没有注意到结果中的任何图像退化。您可以使用arrayjoin
在一个步骤中加入图像数组。对于大量的源图像,它会工作得更好。您可以直接写入深度缩放棱锥体,只需将最后一行替换为image.dzsave(“mypyr”)
。您也可以使用write_to_file
写入深度缩放棱锥体:只需使用.dz
作为后缀。例如:image.write_to_文件(“mypyr.dz[suffix=.png]”
@user894763谢谢,我想我将首先生成一个巨大的图像,然后从那里继续。。。但是arrayjoin看起来很有趣,我只是不确定我是否正确理解了参数。假设我有30个图像,并且希望它们彼此相邻地进行阵列连接,我会这样做:pyvips.Image.arrayjoin(图像列表,交叉=30)
?我指的是Cross参数。save上的Q
param仅用于JPG压缩,对LZW没有任何影响。使用compression=“deflate”
和predictor=“horizontal”
可能会看到最好的结果。正如我所说,我将直接编写金字塔,而不是保存中间层,这样会快得多。