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

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.

Related

ImageMagick: I need an option to mirror an image to the right of itself combined into one file

I need to take an image and append a mirror image of itself to the right (or left). It's got to do with mapping surfaces around spheres where some surface maps do not have left and right sides that "match up". (Digression: Is there is a name for the characteristic of an image whose right and left sides can be placed next to each other, wrapped around a cylinder or sphere, seamlessly?). Appending a mirror image to itself is a quick and dirty way to fix this problem in an environment where the image is chaotic and the viewer won't notice the trick being played.
This ImageMagick command almost does the trick but the problem is, it extends the image vertically as well as horizontally but leaves the vertical extent as an empty transparency. I just want a horizontal, mirror image extension:
convert original.jpg -background transparent -extent 200% \( +clone -flop \) -composite mirror.jpg
I need the -extent option to only extend in the horizontal direction. The ImageMagick documentation doesn't cover percentage values for -extent, only ratios like -extent 4:3 or absolute dimensions like -extent 800x600 but -extent 200% isn't in the documentation. Something like -extent 200% right is what I want. I have many images to run this transformation on and do not want to manually enter dimensions.

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.

ImageMagick: Resize image (rectangle to square); keep aspect ratio of main object

I have portrait and landscape JPEG images.
I want to make square thumbnails with white background from all of them. I need to keep the aspect ratio of all images and reduce the larger border to 200px.
I want to use ImageMagick (CLI) but I don't know how to do that. Any idea ?
Here is the individual images :
I think you need this:
convert -background white -gravity center \
input.jpg -resize 200x200 -extent 200x200 result.jpg

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.

How do I batch resize all images to sqares with Image Magick?

OK. I have a horizontal image, let's say 2000 x 300. I want to overwrite that image and create a new one that is 2000 x 2000 with the old image center in the middle and vertically, with white space around it. I also want to do this in batch with many different images of different sizes. Thanks in advance.
Use -resize to resize the image to 2000x2000, -background to set the background to white, -gravity to center the original image on the canvas, and -extent to resize the canvas itself (to cause the image to sit in the centre if it wasn't square originally).
convert [imagename] -resize 2000x2000 -background white \
-gravity center -extent 2000x2000 [imagename]

Resources