ImageMagick resizing to an abstract rectangle - imagemagick

I'm given an image of unknown size. I want to shrink it such that it will fit in either 640x480 or 480x640, preserving aspect ratio. I want the image to shrink the minimum amount (e.g. the result should be the maximum size which fits in either 640x480 or 480x640).
For example, if I have a 800x800 image, it should shrink to 480x480. If I have a 500x1000 image, it should shrink to 320x640. Similarly, 1000x500 should become 640x320.
Can I do this in ImageMagick in one command? No cropping should occur, and the aspect ratio of the original image should be preserved. Thanks!

It's easy to resize an image without cropping and preserving the aspect ratio, but I don't think that you'll be able to achieve your either/or in a single command.
From the geometry specification docs, resizing an overlarge image to 640x480 is easy:
convert input.png -resize 640x480> output.png
That will only resize if necessary, won't crop, and will preserve the aspect ratio.
Depending on your input images, you might be able to use the area operator:
convert input.png -resize $((640*480))# output.png
But that will only work if all the input images already have either a 4/3 or 3/4 aspect ratio.
I think your best bet is a short shell script:
wider_than_tall=`identify -format %w/%h input.png`
if (( $wider_than_tall )); then
convert input.png -resize 640x480> output.png
else
convert input.png -resize 480x640> output.png
fi

Related

How to resize an image to a certain height in ImageMagick 7?

I need to resize an image and the result image should have 300 pixels in height. The image must keep the aspect ratio of the original image.
To specify the height and keep the aspect ratio, use -resize x300
magick convert -resize x300 in.jpg out.jpg
For Windows users:
"C:\Program Files\ImageMagick-7.0.8-Q16\magick.exe" convert -resize x300 in.jpg out.jpg
(change the "7.0.8-Q16" with your own version of ImageMagick)
For more ways to specify the target size, see https://imagemagick.org/script/command-line-processing.php#geometry

ImageMagick: resize with borders in batch

I have a few images I want to convert in batch using ImageMagick.
All images should be converted to a specific width, lets say 500px.
If an image's width is larger than 500px, scale it down and preserve the aspect ratio.
If an image's width is less than 500px, add borders to the sides to make it 500px wide.
The first one is possible using magick mogrify -resize 500x *, but this also scales images with a smaller width up. How can I prevent that from happening and add borders instead?
I figured I could just add -background black -gravity center but I don't know how to prevent upscaling.
Solved it using two separate commands for now:
Resize images larger than 500px wide:
magick mogrify -resize '500x>' *
Add borders to images smaller than 500px wide:
magick mogrify -gravity center -extent '735x<' *

How to crop to at least 16:9 with imagemagick

I have images of unknown size and ratios. I wish to take a centred 16:9 crop.
If all the images were known 4:3, then it's easy: 9/16 รท 3/4 = 0.75 so I simply set the height to 75% of the original like this:
convert photo.jpg -gravity center -crop '100%x75%' +repage photo16x9.jpg
However, if the photo is already 16:9 (or even wider), I don't want to crop it, and if it is 'taller' than 16:9 I want to crop it only by the amount necessary to achieve a 16:9 crop.
This would be also easier if I wanted to scale to a known width or height (example question for that). But I'm looking to leave as much of the original image data in place as poss.
Therefore I'm looking for a way to crop the height to a factor of the image's width, with a cut-off.
I hoped this might work
convert photo.jpg -gravity center \
-crop '100%x[fx:9/16*w]' +repage photo16x9.jpg
but alas it seems the [fx:...] is not allowed in a -crop argument.
Hacking a bit I found somewhere that I could not for the life of me understand(!) also failed, but I'll list it here to show research effort!
convert photo.jpg -set option:distort:viewport \
'[fx: w]x[fx: w*9/16 ]+0+0' -filter point \
-distort SRT 0 +repage photo16x9.jpg
(I realise that neither attempts above cover the case when the image is already wider than 16:9, but if the [fx:...] thing worked I could achieve that by using the ternary operator, so I kept the examples simple.)
Maybe you can just calculate the aspect ratio and use that to make a decision. Create two test images, one 15x9 and one 17x9:
convert -size 15x9 xc:black 15x9.png
convert -size 17x9 xc:black 17x9.png
Now ask ImageMagick to tell you if the aspect ratio is wider than 16:9 or less than or equal to 16:9:
convert 15x9.png -format "%[fx:(w/h)>16/9]" info:
0
convert 17x9.png -format "%[fx:(w/h)>16/9]" info:
1

Fitting images by specifying the new aspect ratio

Can ImageMagick's convert append white or black bars to maintain aspect ratio after specifying just the aspect ratio?
More concretely
Suppose I have a 2000x1000 widescope image and I would like to compute a new image that has an aspect ratio of 4:3 to fit, say a TV. I can do
convert input.png -background black -extent 2000x1500 -gravity center output.jpg
But here I have manually chosen 2000x1500 to produce an extra 250x2 pixels of blakc. Can I ask convert to:
change the aspect ratio to 4:3
not lose any pixels; not interpolate any pixels
center the image
?
If it's also possible to chose the background color as the dominant color in the image (as in iTunes 11), do mention how.
Convert does not have the built-in capability to pad an image out to a given aspect ratio, so you will need to script this. Here is how this might be done in bash:
#!/bin/bash -e
im="$1"
targetaspect="$2"
read W H <<< $(identify -ping -format "%w %h" "$im")
curaspect=$(bc <<< "scale=10; $W / $H")
echo "current-aspect: $curaspect; target-aspect: $targetaspect"
comparison=$(bc <<< "$curaspect > $targetaspect")
if [[ "$comparison" = "1" ]]; then
targeth=$(bc <<< "scale=10; $W / $targetaspect")
targetextent="${W}x${targeth%.*}"
else
targetw=$(bc <<< "scale=10; $H * $targetaspect")
targetextent="${targetw%.*}x$H"
fi
echo convert "$im" -background black \
-gravity center -extent "$targetextent" \
output.jpg
Call this script with the input image and the target aspect ratio given as a floating point number (for example, 4/3 = 1.333):
$ do-aspect input.png 1.333
Notes:
bc is used for floating point math, because bash itself has only integer arithmetic.
Note that -gravity center is on the final command line before -extent. This is because gravity is a setting while extent is an operator. Settings should always precede the operators that they affect, or else convert will do unexpected things when your commands start to get more complicated.
When you're happy with the results of the program, you can either copy and execute its output, or just remove the echo from before the final convert.
To your question about finding the dominant color of an image, there are different ways of doing that, but one common method is to resize the image to 1x1 and output the color of the resultant pixel - it will be the average color of the whole image:
convert input.png -resize 1x1 -format '%[pixel:p[0,0]]' info:
You can do other processing on the image before resizing to get a different heuristic.

Imagemagick: Convert to fixed height, proportional width

Using Imagemagick, I'd like to convert a batch of PNGs to a fixed height of 1080px and a proportional width. With proportional I mean this: If the original Image is scaled down 16.8% to 1080px, the width also needs to be scaled down by 16.8%.
Any way of using convert without having to calculate the exact geometry before (using identify and some bash calculation shenanigans) ?
Try this:
convert -resize x1080 {from_path} {to_path}
Image geometry is an option described to use with -resize
xheight Height given, width automagically selected to preserve aspect ratio.
So you only have to specify the height
There is one additional example. give it some values to the resize parameters and it'll automatically resize your image. Plus you can chose other parameters (gravity center or crop etc.).
convert image_example: \
-resize x160 -resize '160x<' -resize 50% \
-gravity center -crop 80x80+0+0 +repage image_example.jpg
Cheers

Resources