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?
Cropping unknown amount from left edge to leave a given-width image
- 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
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
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
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
http://www.imagemagick.org/Usage/transform/#fx_escapes for the %[fx:...]
http://www.imagemagick.org/script/escape.php for the h for height parameter
-
- 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
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:
"-gravity east" is what gives us the right-most pixels, instead of the left-most.
Code: Select all
magick ..\1.jpg -gravity East -crop 1220x+0+0 +repage x.png
snibgo's IM pages: im.snibgo.com
- 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
Thanks for the update snibgo. I had forgotten about using just 1200x without the height.
- 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
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...Hobbit wrote: ↑2017-10-28T21:28:46-07:00When I run this command:
It produces two images, an image consisting of the LEFT-most 1220 pixels, and an image consisting of the right-most 4-6 pixels.Code: Select all
magick.exe ..\1.png -gravity East -crop 1220 1_cropped.png
Code: Select all
magick ..\1.png -gravity east -extent 1220x 1_cropped.png
-
- 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
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
Re: Cropping unknown amount from left edge to leave a given-width image
Thank you to all for your help. It works perfectly now.
- 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
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
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/
https://imagemagick.org/Usage/