Page 1 of 1

Mask using polygon, then crop to known coordinates

Posted: 2017-08-29T18:07:00-07:00
by jmaeding
A while back, I got some great help here understanding how to merge images.
I am on a simpler task now, which is to mask out pixels using a polygon, then crop that to a known rectangle.
I have this (runs as windows batch file) and it works, but does not crop as I did not tell it to yet:
"C:\Program Files\ImageMagick-7.0.5-Q8\magick.exe" ^
( "C:\Users\james\Desktop\Kir\granite.png" ^
( -size 128x128 xc:black -fill white -stroke white -draw "polygon 0,0 0,64 64,64 64,0" ) -alpha off -compose CopyOpacity -composite ^
) ^
-compose over -layers merge ^
"C:\Users\james\Desktop\Kir\Final.jpg"

I want to do this, but it does not work:
"C:\Program Files\ImageMagick-7.0.5-Q8\magick.exe" ^
( "C:\Users\james\Desktop\Kir\granite.png" ^
( -size 128x128 xc:black -fill white -stroke white -draw "polygon 0,0 0,64 64,64 64,0" ) -alpha off -compose CopyOpacity -composite ^
) ^
-compose over -layers merge ^
-crop 100x100+0+0 ^
"C:\Users\james\Desktop\Kir\Final.jpg"

I got close doing a couple other ways, but liked the merge method. Just not sure how to organize the code to crop it.
thanks

Re: Mask using polygon, then crop to known coordinates

Posted: 2017-08-30T01:13:02-07:00
by snibgo
jmaeding wrote:... but it does not work
What's wrong? Posting sample input and output images would help.

Re: Mask using polygon, then crop to known coordinates

Posted: 2017-08-30T09:07:57-07:00
by jmaeding
Sorry, I should have said - it outputs 4 images...but.... I just retested my own code, the one in my post with the -crop, and it works now.
As I wrote the post, I added +0+0 to the crop statement just to be more explicit.
I had never tested that as I thought the default was no shift on crop rectangle.
Turns out that fixed it, it only makes one image and its correct.
I just rechecked the IM web page for -crop, and they never omit the +x+x part, my bad.
I have no idea why we get 4 images if that is not there, what does "64x64" parameter of crop mean without the +x+x?

Re: Mask using polygon, then crop to known coordinates

Posted: 2017-08-30T09:22:17-07:00
by Bonzo
Without the +x+x it creates as many images it can at the 64x64 dimension from the original.

https://www.imagemagick.org/script/comm ... s.php#crop
If the x and y offsets are omitted, a set of tiles of the specified geometry, covering the entire input image, is generated. The rightmost tiles and the bottom tiles are smaller if the specified geometry extends beyond the dimensions of the input image.

Re: Mask using polygon, then crop to known coordinates

Posted: 2017-08-30T09:22:36-07:00
by snibgo
As Bonzo says. See the documentation http://www.imagemagick.org/script/comma ... s.php#crop:
If the x and y offsets are omitted, a set of tiles of the specified geometry, covering the entire input image, is generated.
+0+0 are the x and y offsets. Omit that, and you get a set of images.

Re: Mask using polygon, then crop to known coordinates

Posted: 2017-08-30T09:24:52-07:00
by Bonzo
snibgo posted as I was editing my answer to add more detail.

Re: Mask using polygon, then crop to known coordinates

Posted: 2017-08-30T09:55:23-07:00
by jmaeding
oh wow, I hit the Launch button instead of the Lunch button and ended up on the moon....(Far Out Space Nuts...)
That would have been funny if I was testing on some larger images.

Re: Mask using polygon, then crop to known coordinates

Posted: 2017-08-30T13:29:10-07:00
by jmaeding
btw, I just noticed another issue that may have been fooling me in tests.
When you do a windows batch, and use ^ for line continuation, you must add a space at beginning of next line.
Programmers expect "white space" to be irrelevant, so that one bit me today pretty good.

Re: Mask using polygon, then crop to known coordinates

Posted: 2017-08-30T13:52:37-07:00
by snibgo
Well, you need a space either at the start of the next line, or the end of the previous line (before the ^). The IM parser needs at least one space between elements.