【问题标题】:How to prevent Pyvips from stretching images如何防止 Pyvips 拉伸图像
【发布时间】:2019-12-18 16:16:49
【问题描述】:

我正在尝试使用 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

这是什么原因造成的?

【问题讨论】:

  • 请出示您的图片 - 可能有不同的高度吗?
  • 如果您不在字符串中使用 {filename} 等变量,则不必在字符串中使用前缀 f
  • @MarkSetchell 是的,它们的高度变化高达 50 像素,从 1400 像素到 1450 像素。为什么这会导致它们被水平拉伸?
  • 目前我只能猜测,因为我看不到你的图片。
  • @MarkSetchell 这是图像示例,我已将它们更新为相同的高度以消除潜在问题:imgur.com/a/IDuRtIK

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-08-19
    • 1970-01-01
    • 2012-01-30
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    相关资源
    最近更新 更多