Page 1 of 1

Cropping unknown amount from left edge to leave a given-width image

Posted: 2017-10-28T21:28:46-07:00
by Hobbit
Hi, I haven't managed to do this despite reading the user guide and looking online for examples:

I have images of varying width that I am trying to crop so that the remaining image is the right-most 1220 pixels. The source images vary between 1224 and 1226 pixels wide.

When I run this command:

magick.exe ..\1.png -gravity East -crop 1220 1_cropped.png

It produces two images, an image consisting of the LEFT-most 1220 pixels, and an image consisting of the right-most 4-6 pixels.

What am I doing wrong?

Re: Cropping unknown amount from left edge to leave a given-width image

Posted: 2017-10-28T21:44:55-07:00
by fmw42
With -crop, if you do not specify offsets, it will try to crop into as many parts as it can. See http://www.imagemagick.org/Usage/crop/#crop_tile

Thus you need to specify the height and zero offsets. Thus you need to capture the height. You can do that in IM 7 using -set option to create an in-line variable that can be reused. See the section "Using Set "option:" to Define an Artifact" at http://www.imagemagick.org/Usage/basics/#define

You should also remove the virtual canvas that would be saved for PNG output using +repage. See http://www.imagemagick.org/Usage/basics/#virtual_canvas

So

Code: Select all

magick.exe 1.jpg -set option:ht "%[fx:h]" -gravity east -crop "1220x%[ht]+0+0" +repage 1_cropped.png
See also
http://www.imagemagick.org/Usage/transform/#fx_escapes for the %[fx:...]
http://www.imagemagick.org/script/escape.php for the h for height parameter

Re: Cropping unknown amount from left edge to leave a given-width image

Posted: 2017-10-29T00:50:10-07:00
by snibgo
As Fred says, although the height isn't required. It can be left out, with nothing (not even a space) between "x" and "+", like this:

Code: Select all

magick ..\1.jpg -gravity East -crop 1220x+0+0 +repage x.png
"-gravity east" is what gives us the right-most pixels, instead of the left-most.

Re: Cropping unknown amount from left edge to leave a given-width image

Posted: 2017-10-29T11:04:16-07:00
by fmw42
Thanks for the update snibgo. I had forgotten about using just 1200x without the height.

Re: Cropping unknown amount from left edge to leave a given-width image

Posted: 2017-10-29T12:22:23-07:00
by GeeMack
Hobbit wrote: 2017-10-28T21:28:46-07:00When I run this command:

Code: Select all

magick.exe ..\1.png -gravity East -crop 1220 1_cropped.png
It produces two images, an image consisting of the LEFT-most 1220 pixels, and an image consisting of the right-most 4-6 pixels.
Check out the suggestions by fmw42 and snibgo above for the best explanations for using "-crop" to get what you want. You can also use "-extent" to get those results in a simple, one-shot operation almost exactly the way you were trying to use "-crop". Try this...

Code: Select all

magick ..\1.png -gravity east -extent 1220x 1_cropped.png
That would give you the eastern-most 1220xH image from your input. It works the same as the "-crop" operator in that you can omit the height argument like "-extent 1220x", or use zero for the height like "-extent 1220x0", and the output will be a single image even if you don't include any offsets.

Re: Cropping unknown amount from left edge to leave a given-width image

Posted: 2017-10-29T12:55:20-07:00
by snibgo
For opaque input images, "-extent" is fine. If the input has transparency, it will also be composited over the background (by default), which can be a little confusing.

Re: Cropping unknown amount from left edge to leave a given-width image

Posted: 2017-10-29T13:45:04-07:00
by Hobbit
Thank you to all for your help. It works perfectly now.

Re: Cropping unknown amount from left edge to leave a given-width image

Posted: 2017-11-08T16:33:43-07:00
by anthony
The biggest difference between -extent and -crop is that -extent will not only crop an image but expand it to. That is if the image was smaller than 1220 pixels, it will expand the image by 1220 pixels, that is why it needs to 'overlay' the image onto a background. Crop on the other hand will NEVER expand an image (only make it smaller), so if the image is not 1220 pixels wide, crop will short-circuit and do nothing, leaving the image the same (smaller) width.

As for the background. if you use -background none then extent will overlay on a transparent background, making no change to any transparency in the original image. As such a transparent background, (or if the image has not transparency) will only have an effect if the image needs to be made larger.

It is all in the ImageMage Usage Examples. Extent..
http://www.imagemagick.org/Usage/crop/#extent