[magick-users] RE: Best Way To Take An Image Of Any Ratio and Make A Square Out Of It (1:1)

Fred Weinhaus fmw at alink.net
Wed Jan 16 11:51:57 PST 2008


The following seems to work for me and is a bit simpler. I tried four 
images of sizes: 180x118, 142x216, 50x33 and 33x50.


convert image.jpg -thumbnail x60 -resize '60x<'  \
	-gravity center -crop 60x60+0+0 +repage image_60x60c.jpg

In either case (your method or mine above), you are losing a little 
resolution (making the result a bit blurry) for images that are 
taller than wide, as the thumbnail step resizes the image so that the 
larger dimension is 60 and then the subsequent resize expands the 
image so that the smaller dimension is 60.  Thus the image has been 
over-shrunk and then is made larger again.

So another way to do it is to script finding the image's width and 
height and then do the right thing for the correct dimension so that 
the smaller dimension is key and the re-expand is not needed.

width=`identify -format "%w" image.jpg`
height=`identify -format "%h" image.jpg`

if [ $width -gt $height ]
then
	convert image.jpg -thumbnail x60   \
	-gravity center -crop 60x60+0+0 +repage image_60x60c.jpg
else
	convert image.jpg -thumbnail 60x   \
	-gravity center -crop 60x60+0+0 +repage image_60x60c.jpg


Or you can do the following

size=60
width=`identify -format "%w" image.jpg`
height=`identify -format "%h" image.jpg`
[ $width -gt $height ] && factor="x${size}" || factor="${size}x"
convert image.jpg -thumbnail $factor   \
	-gravity center -crop ${size}x${size}+0+0 +repage image_60x60c.jpg







>Hey guys and gals,
>This is what I am doing now to take an image of any ratio and make a 60x60
>thumbnail out of it.
>
>  > convert -size 240x240 test.jpg -thumbnail x120 -resize '120x<' -resize 50%
>-gravity center -crop 60x60+0+0 +repage test_60x60.jpg
>
>I was wondering if their was a much simpler way of doing this?
>
>Thanks :-)
>
>--
>John Kopanas
>john at kopanas.com


More information about the Magick-users mailing list