Search code examples
pythonimageimage-processingimage-resizingvips

How to prevent Pyvips from stretching images


I'm trying to join multiple images together using pyvips, but during the process the width of the image more than doubles, and I can't seem to figure out why

Here's the key part of my code:

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')

Expected output for 5 files should be:

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

But my actual output is:

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

Images: https://i.sstatic.net/bCqPV.jpg

What's causing this?


Solution

  • arrayjoin only does regular grids of images -- it'll pick the maximum width and height in your set of images, and that becomes the spacing. It's like a table layout.

    If you want to join a series of images of varying width left to right, you need to do a series of joins. For example:

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

    You can set alignment, spacing, background, etc., see:

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