5

I'm using graphicsmagick to resize an image to a thumbnail, but it's adding a white surrounding border padding.

enter image description here

The command I'm using is:

gm convert matrix.jpg -resize "80x80>" -gravity center -extent 80x80 thumbnail.jpeg

As you can see, there is some white padding around the image, but I don't want this. Ideally I'd like (the entire image not just a crop of it) to fill the desired 80x80 output size.

How can this be achieved in either imagemagick or graphicsmagick?

bob_cobb
  • 2,229
  • 11
  • 49
  • 109
  • *the entire image not just a crop of it* You prefer to alter the aspect ratio? – A.L Jun 22 '14 at 19:36
  • Please add an example of the expected image in your question. You can use free tools such as Paint.NET, GIMP, etc. – A.L Jun 24 '14 at 15:30

2 Answers2

3

I used ImageMagick with this image. This solution requires to know the size of the input image.

Input

Input

The image has 145 pixels horizontally and 200 pixels vertically.

Crop

Crop from the top of the image:

convert -crop 145x145+0+0 -resize 80x80 matrix.jpg thumbnail.jpeg

I used 145x145 in order to extract a square from the original image. +0+0 is the offset of the extracted square, hereby the top left.

Crop

Crop with the center of the image:

convert -crop 145x145+0+27 -resize 80x80 matrix.jpg thumbnail.jpeg

Crop

The vertical offset is set to 27 because we have to remove 55 (200 - 145) pixels on top or bottom, so we need to remove 27 (55 ÷ 2) pixels on the top and 28 pixels on the bottom.

Crop with the bottom of the image:

convert -crop 145x145+0+55 -resize 80x80 matrix.jpg thumbnail.jpeg

Crop

Resizing without crop

convert -resize 80x80\! matrix.jpg thumbnail.jpeg

The ! flag (escaped with \! as suggested in the documentation) after the resize parameters forces ImageMagick to ignore the aspect ratio.

Ignore aspect ratio

A.L
  • 10,259
  • 10
  • 67
  • 98
  • Is there a reason why using the `convert -resize 80x80!` squishes it so much? Any way to fix that? It's definitely squished (in height) vs the original's aspect ratio. – bob_cobb Jun 23 '14 at 03:17
  • 1
    @bob_cobb : I have the same result if I use GIMP to resize the image from 145×200 to 80×80. You can't resize a rectangular image to a square image without squishing it. – A.L Jun 23 '14 at 16:34
3

If you want to keep the original aspect ratio, without image distortion, you can use the ImageMagick -trim option to get rid of the white padding:

convert "matrix.jpg" -resize "80x80" -gravity center -extent 80x80 -trim "thumbnail.jpg"

This will produce a 58x80 uncropped image with the same aspect ratio as the original. It is 58x80 because ImageMagick uses the larger dimension of the original to compute the scale factor (in this case 80/200) and scales the smaller dimension by that same factor to preserve aspect ratio.

If you want an uncropped image of exactly 80x80 pixels, this is a different aspect ratio than the original. The output image will have distortion, and @AL's resizing without crop option will work.

convert "matrix.jpg" -resize "80x80!" -gravity center -extent 80x80 "thumbnail.jpg"

Tested in Windows 7 with ImageMagick 6.8.9. @AL syntax is probably Linux.

paisanco
  • 4,098
  • 6
  • 27
  • 33