Mask using polygon, then crop to known coordinates

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
jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Mask using polygon, then crop to known coordinates

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

Re: Mask using polygon, then crop to known coordinates

Post by snibgo »

jmaeding wrote:... but it does not work
What's wrong? Posting sample input and output images would help.
snibgo's IM pages: im.snibgo.com
jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: Mask using polygon, then crop to known coordinates

Post 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?
James Maeding
Civil Engineer / Programmer
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Mask using polygon, then crop to known coordinates

Post 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.
Last edited by Bonzo on 2017-08-30T09:23:59-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Mask using polygon, then crop to known coordinates

Post 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.
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Mask using polygon, then crop to known coordinates

Post by Bonzo »

snibgo posted as I was editing my answer to add more detail.
jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: Mask using polygon, then crop to known coordinates

Post 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.
James Maeding
Civil Engineer / Programmer
jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: Mask using polygon, then crop to known coordinates

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

Re: Mask using polygon, then crop to known coordinates

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply