Creating a thumbnail of specified resolution

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
hollymcr
Posts: 9
Joined: 2009-12-01T03:56:35-07:00
Authentication code: 8675309

Creating a thumbnail of specified resolution

Post by hollymcr »

I need to create the best quality thumbnail I can, of a given size, from an original image. For the sake of simplicity assume a square thumbnail although that won't always be the case.

So let's say I want a 100x100 thumbnail from a 1024x768 image. What I want to do is crop the image to 768x768 (same aspect ratio as the desired thumbnail), using "center" gravity. Then I want to resize the result to 100x100.

Is there a general way to do this without making assumptions about the original image size or aspect ratio, and without going through multple steps?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Creating a thumbnail of specified resolution

Post by Bonzo »

Try:

Code: Select all

convert input.jpg -gravity center -crop 768x768+0+0 -resize 100x100 output.jpg
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating a thumbnail of specified resolution

Post by fmw42 »

add +repage after -crop and use -thumbnail rather than -resize so as to strip the headers and profiles, etc. to make the result smaller.
hollymcr
Posts: 9
Joined: 2009-12-01T03:56:35-07:00
Authentication code: 8675309

Re: Creating a thumbnail of specified resolution

Post by hollymcr »

Almost perfect! (And a lot further than I got on my own, so huge thanks!)

Is it possible to handle the generalised case where I don't know the original image size? Ie I don't know that 768 is the smaller of the two dimensions?

For example, can I start by resizing so that the smallest dimension is 100, then crop to 100x100?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating a thumbnail of specified resolution

Post by fmw42 »

see the ^ symbol for the geometry at http://www.imagemagick.org/script/comma ... p#geometry to resize to the smaller dimension. If on windows, see http://www.imagemagick.org/Usage/windows/

convert image -resize "100x100^" -gravity center -crop 100x100+0+0 +repage result
hollymcr
Posts: 9
Joined: 2009-12-01T03:56:35-07:00
Authentication code: 8675309

Re: Creating a thumbnail of specified resolution

Post by hollymcr »

That's perfect, thanks!

I had got pretty close but not quite there on my own.

I'd say that for most purposes,
convert image -resize "100x100^" -gravity center -crop 100x100+0+0 +repage result
is the best general way to create a thumbnail, isn't it? (100x100 can be changed to whatever size wanted, of-course.)
Post Reply