Libvips: vipsthumbnail resize with padding

Created on 23 Jan 2019  ·  4Comments  ·  Source: libvips/libvips

Hi,

Is there a way to resize an image to a square image, keeping the aspect ration and pad the rest?
Is there a way to to that centered?

Thanks

question

All 4 comments

Hello @doronAtuar,

vipsthumbnail won't do that, no. You'd need to run a second command, perhaps:

vipsthumbnail IMG_1503.JPG -s 500x500 -o temp.v
vips gravity temp.v thumb.jpg centre 500 500 --background "128 128 255"

So source image:

img_1503

Becomes:

thumb

Or in (for example) Python:

import pyvips

x = pyvips.Image.thumbnail("IMG_1503.JPG", 500, height=500)
x = x.gravity("centre", 500, 500, background=[128, 128, 255])
x.write_to_file("thumb.jpg")

Hey @jcupitt it works!
Thanks for the quick response!!

I know it's not a real benchmark but...
I'm converting images sized 1024x720 to 416x416 with white padding
with pyvips I manage to get 45 images resized per second
with IM its 30 images a second using convert

I think it would be probably faster if it was a cpp/c utility.

I had a quick go here. First, 100 test images:

$ for i in {1..100}; do vips crop ~/pics/k2.jpg $i.jpg 0 0 1024 720; done

Then two benchmarks:

#!/usr/bin/python3

import sys
import pyvips

SIZE = 416

for filename in sys.argv[1:]:
    x = pyvips.Image.thumbnail(filename, SIZE, height=SIZE)
    x = x.gravity("centre", SIZE, SIZE, background=255)
    x.write_to_file("tn_" + filename)
#!/bin/bash

size=416

for filename in $*; do
        convert $filename \
                -resize "${size}x${size}>" \
                -background white \
                -gravity center \
                -extent ${size}x${size} \
                tn_$filename
done

And on this 2015 laptop I get:

$ time ../padthumb.sh *.jpg
real    0m4.341s
user    0m10.856s
sys 0m1.091s

So 23 op/sec, and pyvips:

$ time ../padthumb.py *.jpg
real    0m1.927s
user    0m2.860s
sys 0m0.219s

51 op/sec.

I tried on a huge i7 desktop and got 34 op/sec for convert, and 76 op/sec for pyvips.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sonyarianto picture sonyarianto  ·  4Comments

AKlein920 picture AKlein920  ·  3Comments

Kyle-Kyle picture Kyle-Kyle  ·  4Comments

codecitizen picture codecitizen  ·  4Comments

harukizaemon picture harukizaemon  ·  4Comments