Batch crop images to fixed height (CLI) - imagemagick

I have images of variable height and width, and I want to crop them all to be 200 pixels high, without any resizing. i.e. their width will remain the same, and height will be 200px. I want to use "north" gravity for this.
Any CLI tool that allows batch processing would be OK - imagemagick/mogrify, gm, etc. I've tried to figure this out but can't find any option to just leave width unspecified.

mogrify *.png -gravity North -extent x200 -path ./complete
The mogrify utility would suite your batch task with -gravity, -extent, and -path options:

You could leave the width unspecified by, well, leaving the width unspecified, and use extent to do the cropping:
convert in.png -extent x200 out.png
or, say,
mogrify -extent x200 *.png
For the wizard example image, that would turn this into this. You can see more resizing examples on the examples site, as well as some different ways of doing cropping.

Related

crop / swap parts of image with magick mogrify

I am trying to crop and swap different parts of a big 800x800 image and re-create 800x800 image using imagemagick with this command.
magick mogrify titli.gif -crop 2x4# +repage -reverse -append -path converted titli.gif
my problem is "-append" creates tall image (400x1600) & "+append" creates wide image (3200x200)
How can I get a large image of original size 800x800 but with cropped and swapped (reversed) parts set in "mosaic or tiled" style...
If I understand the question, you shouldn't need "mogrify" to do that. Just "magick" should accomplish that task.
It looks like you'll have to crop the image into 8 pieces, reverse them, and "-append" them vertically as you've done.
Then after that, and in the same command, you'll need to crop that result in half vertically and "+append" those two pieces horizontally to get the 800x800 output.
This example command shows how it works...
magick in.png -crop 2x4# -reverse -append -crop 1x2# +append out.png
If you're doing any more operations within the same command you'll probably want to use "+repage" after the "+append" to reset the image geometry back to WxH+0+0.

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<' *

ImageMagick: get biggest square out of image

I do have thousands of images in different sizes; now I would like to get the biggest square out of it that's possible without transparent/black background. Of course, the ratio should be kept, if it's e.g. a landscape image all height should be in the destination image but the left and right should be cropped; for portrait images, the other way round.
How's that possible?
I think you mean this. If you start with a landscape image bean.jpg:
magick bean.jpg -gravity center -extent "%[fx:h<w?h:w]x%[fx:h<w?h:w]" result.jpg
If you start with a portrait image, scooby.jpg:
magick scooby.jpg -gravity center -extent "%[fx:h<w?h:w]x%[fx:h<w?h:w]" result2.jpg
The part inside the double-quotes is the interesting part. It is basically setting the extent of the image, like:
-extent 100x100
where 100 is the width and the height. Rather than that though, I am using a calculated expression which tests whether the height (h) is less than the width (w) using a ternary operator. That results in taking whatever is smaller out of the current height and width as the new height and width, So there are really two calculated expressions in there, with an x between them, similar to 100x100.
Note that this method requires ImageMagick v7 or better - i.e. it uses the magick command rather than v6's convert command. If you have v6, you need to use more steps. First, get the width and the height of the image, then choose the smaller of the two and then issue a convert command with the gravity and extent both set. In bash:
# Get width and height
read w h < <(identify -format "%w %h" scooby.jpg)
# Check them
echo $w,$h
272,391
# Set `n` to lesser of width and height
n=$w
[ $h -lt $n ] && n=$h
# Now do actual crop
convert scooby.jpg -gravity center -extent "${n}x${n}" result.jpg
If you have thousands to do, I would suggest using GNU Parallel if you are on macOS or Linux. If you are on Windows, sorry, you'll need a loop and be unable to easily use all your CPU cores.
I have not tested the following, so only try it out on a small, COPIED, sample of a few files:
# Create output directory
mkdir output
# Crop all JPEG files, in parallel, storing result in output directory
parallel --dry-run magick {} -gravity center -extent "%[fx:h<w?h:w]x%[fx:h<w?h:w]" output/{} ::: *.jpg
If the commands look good, remove the --dry-run part to do it for real.
If you're using ImageMagick v7, Mark Setchell has provided a simple method above (or below). If you're using IMv6, you can crop the largest center square from any image using a command lke this...
convert input.png -set option:distort:viewport "%[fx:min(w,h)]x%[fx:min(w,h)]" \
-distort affine "%[fx:w>h?(w-h)/2:0],%[fx:w<h?(h-w)/2:0] 0,0" output.png
That sets the output viewport size to the largest square you can crop from the input image. Then it adjusts the position of the input image so it is centered within that square viewport.
This command should work from a command prompt or script on most *nix systems. If you're using Windows, replace that continued line backslash "\" with a caret "^". If you're using a BAT script in Windows you'll also have to make all the single percent signs "%" into doubles "%%".
You can also simply change "convert" to "magick" to run this command using IMv7.
I find this easier to remember:
convert in.png -gravity Center -extent 1:1 out.png

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

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