Page 1 of 1

Crop image with 4 coordinates and a specific target size

Posted: 2017-06-29T09:30:45-07:00
by maqe
I have this image: https://imagga.com/static/images/taggin ... 76_640.jpg which I want to crop a size of 500x300. The width and height is the target size after the crop has been applied.

The coordinates I want to crop the image with size 100x100 by is 0,0 (top,left) and 639, 384 (bottom,right). When the crop is done I expect the image to be 500x300 cropped with the coordinates above. I have tried two different commands with no success.

This is the first command I have tried:

Code: Select all

convert http://imagga.com/static/images/tagging/wind-farm-538576_640.jpg -crop 500x300+0,0+639,384 +repage out.jpg
This is what the result looks like:

Image

The other command I have tried looks like this:

Code: Select all

convert http://imagga.com/static/images/tagging/wind-farm-538576_640.jpg -crop 0x0+0,0+639,384 +repage -resize 500x300 out.jpg
This is what the result looks like from the command above:

Image

How should the command be written to produce the correct result?
This is how the correct cropping should look

Image

Re: Crop image with 4 coordinates and a specific target size

Posted: 2017-06-29T09:47:50-07:00
by maqe
This command seems to work.

Code: Select all

convert http://imagga.com/static/images/tagging/wind-farm-538576_640.jpg -resize 500x300^ +repage -crop 500x300+0+0 +repage out.jpg
Is this the correct way of doing this with the result I want?

This is the result:

Image

Re: Crop image with 4 coordinates and a specific target size

Posted: 2017-06-29T10:01:27-07:00
by fmw42
First of all ImageMagick does not use top-left to bottom-right coordinates to crop. It uses WxH+Xoff+Yoff, where W=width, H=height and Xoff,Yoff are the top left coordinates. In ImageMagick 6, you must compute W and H separately from your point pair. In ImageMagick 7 you can do that inline, but not with the syntax you have.

Second, your coordinates 0,0 (top,left) and 639, 384 (bottom,right) do not compute to W=500 and H=300.

It looks like you want to crop to your coordinates and then resize. Note that the width=640 since 0 to 639 is 640 pixels including the 0th pixel. Similarly for height. This will work in both IM 6 and IM 7, since I have precomputed the width and height.

# this will have size 499x300

Code: Select all

convert wind-farm-538576_640.jpg -crop 640x385+0+0 +repage -resize 500x300 out1.jpg
# this will distort by 1 pixel to make it exactly 500x300

Code: Select all

convert wind-farm-538576_640.jpg -crop 640x385+0+0 +repage -resize 500x300! out2.jpg
In IM 7, you could do

Code: Select all

magick wind-farm-538576_640.jpg -crop "%[fx:639-0+1]"x"%[fx:384-0+1]"+0+0 +repage -resize 500x300 out1.jpg