Imagemagick: Convert to fixed height, proportional width - imagemagick

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

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

ImageMagick resizing to an abstract rectangle

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

Resize image by length with image magick

I'm trying to resize a image by its length, the width should remain. Is sounds quite easy, but somehow i don't get it. I'm using it together with PHP via the command line.
I'm using ImageMagick 6.7.3-9
By reading the docs, i would do the following:
/usr/local/bin/mogrify -resize x200! myimg.png
That gives me:
mogrify: negative or zero image size `bbb.png' # error/resize.c/ResizeImage/2570.
My goal is, that if bbb.png is w:400 h:400 i can resize it to w:400 h:200.
mogrify -resize x200 //Height with new width
mogrify -resize 200 //Width with new height
mogrify -resize 200! //New Width, old height
Can someone explain me, what I am doing wrong? Thanks
Known broken, now fixed. Fix should be in ImageMagick-6.7.4-0 Beta available 2011-12-08.
I have not found your Image Geometry fromat -resize x200! in documentation. I think you should use
xheight
or
widthxheight!
I.e.: -resize x200 or -resize 400x200!.

Resources