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

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Hobbit
Posts: 3
Joined: 2017-10-25T10:07:32-07:00
Authentication code: 1151

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

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

Thanks for the update snibgo. I had forgotten about using just 1200x without the height.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
Hobbit
Posts: 3
Joined: 2017-10-25T10:07:32-07:00
Authentication code: 1151

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

Post by Hobbit »

Thank you to all for your help. It works perfectly now.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply