HamidIng
HamidIng

Reputation: 105

ImageMagick crop keeping the height

I'm trying to crop image by height with this command line:

convert 1053257.png -gravity South -crop 2910x3312+0+0 -background black +repage image-cropped-top.png

The generated image is not cropped correctly, as the dimensions after running the command are 2791 x 3312. The width is cropped as well!

Can anyone help with this?

Upvotes: 3

Views: 1987

Answers (2)

GeeMack
GeeMack

Reputation: 5385

With ImageMagick a problem like that can occur if you've done a "-trim" to the image before the crop. When you "-trim" an image it can still remember the original page dimensions from before the trim, then when you crop it, it uses those page dimensions as the starting reference for the crop. You probably need to "+repage" before the crop to start with fresh paging information. Try something like this...

convert 1053257.png -gravity South +repage -crop 2910x3312+0+0 +repage image-cropped-top.png

Upvotes: 3

Mark Setchell
Mark Setchell

Reputation: 207365

The general form is:

convert input.jpg -crop WIDTHxHEIGHT+0+0 result.jpg

If you want to crop to a specific width, say 1024, leaving the height unaffected:

convert image.jpg -crop 1024x+0+0 result.jpg

If you want to crop to a specific height, say 768, leaving the width unaffected - note the height is after the x:

convert image.jpg -crop x768+0+0 result.jpg

If you want to crop to a maximum width and height, say 1024 wide by 768 tall without distorting the aspect ratio:

convert image.jpg -crop 1024x768+0+0 result.jpg

If you want to crop to a specific width and height, say 1024 wide by 768 tall and are happy to allow gross distortions:

convert image.jpg -crop 1024x768+0+0\! result.jpg

Think of the exclamation mark as meaning "just do it!". Note that the backslash is only needed on Linux/Unix/macOS to escape the exclamation mark, you omit the backslash on Windows.


Note, if you are saving the cropped image in PNG format, you probably want to reset the page afterwards so the image "forgets" it used to be part of a larger image:

convert input.jpg -crop 1024x768+0+0 +repage result.png

Upvotes: 7

Related Questions