imagemagick – Force GraphicsMagick to Resize Image to Specific Width

image manipulationimagemagick

I'm batch processing a folder of image to a uniform width with GraphicsMagick (though I assume the same would apply to ImageMagick):

$ gm convert -resize 1000x in.jpg out.jpg

But when I inspect some of the images, I get 999px instead of 1000px.

$ gm identify -verbose out.jpg

==> Image: out.jpg
==> Format: JPEG (Joint Photographic Experts Group JFIF format
==>   Geometry: 999x591

Is there a way to enforce the right width? I assume it's doing some estimation to preserve aspect ratios.

Best Answer

Figured this out, you use an exclamation mark to enforce the elected size. From the GraphicsMagick docs:

Append an exclamation point to the geometry to force the image size to exactly the size you specify. For example, if you specify 640x480! the image width is set to 640 pixels and height to 480.

So from the example above:

$ gm convert -resize 1000x! in.jpg out.jpg

This results in an image that's exactly 1000 pixels wide:

$ gm identify -verbose out.jpg

==> Image: out.jpg
==> Format: JPEG (Joint Photographic Experts Group JFIF format
==>   Geometry: 1000x591
Related Question