ImageMagick: how to minimally crop an image to a fixed aspect ratio regardless of the image's dimensions? - image

ImageMagick can minimally crop images from the center of an image at a certain aspect ratio.
For example, an image named "input-image.jpg" which is 3000px x 2000px can be minimally cropped, from the center of the image and at a 16:9 aspect ratio, by using the following command.
convert input-image.jpg -gravity center -crop 16:9 output-image.jpg
However, if the image has a larger height than width, that same convert command above will crop the image at the inverse aspect ratio of 9:16. For example, if the dimensions of the image is 2000px x 3000px instead of 3000px x 2000px, ImageMagick will crop the image at a 9:16 aspect ratio even-though a 16:9 aspect ratio is called for in the convert command
In ImageMagick, how do you crop an image, at the center of the image and at a certain aspect ratio, regardless of the input image's height and width?

Related

No-crop canvas increase to 2:1 aspect ratio with ImageMagick

I am in a situation where I have a set of images with variable width and height and I need to ensure that those images comply with an aspect ratio of 2:1.
The only assumption I can make is that those images have an aspect ratio greater than 2 so I need to expand the canvas.
With ImageMagick I can do this:
magick input.jpg -background black -gravity north -extent 2:1 output.jpg
But this crops instead of adding canvas as desired. Is there an option to avoid cropping, i.e. force image padding? Remember I do not know the image resolution beforehand.
Of course with a more complex script I can extract the image dimensions and compute the desired resolution but I am trying to keep things simple if possible.
P.S.: The images comes from a laser scanner and are going to be used in a 360 panorama viewer hence the need to meet an aspect ration without cropping.
With ImageMagick v7 you can do some inline calculations that will adjust the results of the -extent operation to pad any size input to a 2:1 ratio. See this command as an example...
magick input.png -gravity center -extent "%[fx:max(w,h*2)]x%[fx:max(w/2,h)]" result.png
That will pad any extra space with black. Add -background <somecolor> before the extent operation to change the padding color.

Resize image to X on long edge while preserving ratio

How do I resize an image to say 800px on the long edge while preserving the image ratio using rmagick?

Resize without whitespace surrounding on images using graphicsmagick or imagemagick

I'm using graphicsmagick to resize an image to a thumbnail, but it's adding a white surrounding border padding.
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?
I used ImageMagick with this image. This solution requires to know the size of the input image.
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 with the center of the image:
convert -crop 145x145+0+27 -resize 80x80 matrix.jpg thumbnail.jpeg
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
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.
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.

Crop thumbnail in imagemagick - specify center at X, Y position

I would like to generate thumbnails for my website. Now I am using the following to process folder (based on this answer):
mogrify -interlace Plane -thumbnail 280x210^
-gravity center -extent 280x210 -quality 85% *.jpg
This will create thumbnails with given size (resize and crop).
I also have single image version of this command:
convert test.jpg -interlace Plane -thumbnail 280x210^
-gravity center -extent 280x210 -quality 85% testout.jpg
I'm also using facedetect to get positions of faces from the original image (before creating thumbnails). For photos with faces I have calculated average X, Y position of faces center.
Now, I would like to use this position in the command above to crop pictures smarter. Currently image is cropped from both sides (-gravity center), but I would like to use my coordinates so faces are displayed when image is cropped.
Is there a way how I can feed this into imagemagick?
You could use -shave to remove part of the image, so that the face moves to the center, then apply your original command.

Image resizing, maintaing image size - beginner question

Im attempting to resize an image from 480x800 to 320x240. Below are the results. The original image has an oval shaped circle whereas the resized image has a more spherical shape. Is it possible to resize the original image so that the circle and rectangle are in their original proportions but smaller?
Can imagemagick or gimp (or other software) achieve this ?
Here's Imagemagick solution.
1) If you want large image to just fit 320x240 box and leave proportions, use:
convert test.png -size 320x240 resized.png
That will produce image sized 144x240.
http://www.imagemagick.org/Usage/resize/#resize
2) If you want large image to completely fill a specific image size, use:
convert test.png -resize 320x240\> \
-size 320x240 xc:blue +swap -gravity center -composite \
resized.jpg
That will produce image sized 320x240 with resized big image in center and blue fill on sides.
http://www.imagemagick.org/Usage/resize/#space_fill

Resources