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
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:
Becomes:
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.