如何防止Pyvips拉伸图像

时间:2019-12-18 16:16:49

标签: python image image-processing image-resizing vips

我正在尝试使用pyvips将多个图像连接在一起,但是在此过程中,图像的宽度增加了一倍以上,而且我似乎无法找出原因

这是我的代码的关键部分:

files = ['small_images\\'+filename for filename in os.listdir('small_images')]
files = natsorted(files)

images = [pyvips.Image.new_from_file(filename, access="sequential") for filename in files]
[print(f'width: {image.width}, height: {image.height}') for image in images]

raster = pyvips.Image.arrayjoin(images,across=len(images))
print(f'raster width: {raster.width}, raster height: {raster.height}')

raster.write_to_file(f'rasters.png')

5个文件的预期输出应为:

width: 115, height: 1449

width: 44, height: 1449

width: 226, height: 1449

width: 74, height: 1449

width: 35, height: 1449

raster width: 494, raster height: 1449

但是我的实际输出是:

width: 115, height: 1449

width: 44, height: 1449

width: 226, height: 1449

width: 74, height: 1449

width: 35, height: 1449

raster width: 1130, raster height: 1449

图片:https://imgur.com/a/IDuRtIK

这是什么原因造成的?

1 个答案:

答案 0 :(得分:2)

arrayjoin仅处理规则的图像网格-它会选择图像集中的最大宽度和高度,然后成为间距。就像表格布局一样。

如果要加入从左到右不同宽度的一系列图像,则需要进行一系列的加入。例如:

output = images[0]
for image in images[1:]:
    output = output.join(image, 'horizontal', expand=True)

您可以设置对齐方式,间距,背景等,请参见:

https://libvips.github.io/libvips/API/current/libvips-conversion.html#vips-join