Page 2 of 2

Re: Trim images with polygon and combine

Posted: 2018-01-11T18:41:15-07:00
by fmw42
I'd think if I can figure out how to add images to posts, I could get the actual software working
This forum does not allow images to be posted directly. But you can use the same concept as before by posting to some other host and putting the URLs here. Or zip all your files and post a link to the zipped files.

Re: Trim images with polygon and combine

Posted: 2018-01-11T18:55:26-07:00
by fmw42
I think my method shows you what to do. You need to make each image have a transparent background and then crop to the bounds you set by your polygons, different for each image. Then you need to figure out how to shift each image to align them as desired. This is the coordinates you need to use in the -set page +X+Y.

Alternately, you need to composite each image into an image the size of the final result so they are placed where you want. Then mask each one with different polygons to mask out the remaining areas. Then composite all the masked images together, since they will be in the right positions already.

Re: Trim images with polygon and combine

Posted: 2018-01-12T11:16:06-07:00
by fmw42
If you add -trim to my previous code, I think that will be more what you want. Each piece is then cropped and -set page tell it where to be placed on a larger canvas. In this case, they overlap

magick logo.jpg -write mpr:img +delete \
\( -size 640x480 xc:black -fill white -stroke white -draw "polygon 23,18 23,90 90,90 90,18 23,18" -write mpr:mask +delete \) \
\( mpr:img \( mpr:mask \) -alpha off -compose copy_opacity -composite -trim -set page +0+0 +write tmp1.png \) \
\( mpr:img \( mpr:mask -roll +90+90 \) -alpha off -compose copy_opacity -composite -trim -set page +25+25 +write tmp2.png \) \
-compose over -background none -layers merge -trim +repage NewLogos.png

Re: Trim images with polygon and combine

Posted: 2018-01-12T11:35:50-07:00
by jmaeding
well, the problem with that is I know where the images get placed based on corners of the untrimmed image.
That is why I don't want to modify the size of the originals, just mask out pixels outside my polygons.
I reread what the meaning of "+distort tries to fit..." meant, and think it does what I originally thought after all.
It only expands the virtual canvas to "fit" the distorted image.
It does not somehow modify the distortion location, so that issue is gone.

That leaves the trimming portion of the code or -compose as suspects.
The trimming part works fine if I just add one image to the main image (2 total).
So that makes me think the layers merge is not doing what I expect.

Doesn't layers merge use a "main" virtual canvas based on the first image in the list?
It seems like its not, as if the canvas 0,0 location is changing as things are merged in so I am getting wrong results after the first is merged in.
Any thoughts on that?
I'm reading on it currently but others must have hit this before.

Re: Trim images with polygon and combine

Posted: 2018-01-12T12:24:56-07:00
by fmw42
Layers merge creates its own size canvas enough to hold all the images as defined by their size and their given virtual-canvas (page) offsets

Another approach is to mask the images and specify the page offsets for the originals so that they are placed over each other as desired. Just layer merge them and crop as desired. But you still have the same issue. You need to mask each with a polygon appropriate to the region you want. Then you still need to offset them. +distort Affine or +distort SRT seems the hard way just to specify the offsets. Using -set page is much easier.

Re: Trim images with polygon and combine

Posted: 2018-01-12T12:38:22-07:00
by fmw42
With respect to your newspaper city photo, what you really need to do is pick 4 (or more) points for each image and corresponding points on a map of the region. Mask each region to make the images transparent elsewhere. Then use an affine or bilinear or perspective distort to register the masked image to a common coordinate system such as the map. The layers merge them.

Your issue right now is that you do not know how to align them together so that come edge to edge. That means trial and error. Also the distort may correct for rotation and scale and perspective.

Re: Trim images with polygon and combine

Posted: 2018-01-12T14:35:58-07:00
by jmaeding
I actually do have all the coordinates, I just meant I want to use the original image corners for the distort.
I am actually doing what your approach says, something is going wrong if more than two images are merged though.
Look at this, I merge 2 with this code:
"magick.exe" ^
( "logo.jpg" ( -size 123x118 xc:black -fill white -stroke white -draw "polygon 23,18 23,113 113,113 113,18 23,18" ) -alpha off -compose CopyOpacity -composite ) ^
( ( "logo.jpg" ( -size 123x118 xc:black -fill white -stroke white -draw "polygon 23,18 23,113 113,113 113,18 23,18" ) -alpha off -compose CopyOpacity -composite ) -alpha set -virtual-pixel transparent +distort affine "0,0 -90,0 0,118 -90,118" ) ^
-compose over -layers merge +repage "C:\Users\james\Desktop\NewLogos.jpg"
I get this (correct, worked perfect):
Image

If I do the exact same thing, but add another image in the row so 3 images, it does not mask the third image, code is:
"magick.exe" ^
( "logo.jpg" ( -size 123x118 xc:black -fill white -stroke white -draw "polygon 23,18 23,113 113,113 113,18 23,18" ) -alpha off -compose CopyOpacity -composite ) ^
( ( "logo.jpg" ( -size 123x118 xc:black -fill white -stroke white -draw "polygon 23,18 23,113 113,113 113,18 23,18" ) -alpha off -compose CopyOpacity -composite ) -alpha set -virtual-pixel transparent +distort affine "0,0 -90,0 0,118 -90,118" ) ^
( ( "logo.jpg" ( -size 123x118 xc:black -fill white -stroke white -draw "polygon 23,18 23,113 113,113 113,18 23,18" ) -alpha off -compose CopyOpacity -composite ) -alpha set -virtual-pixel transparent +distort affine "0,0 -180,0 0,118 -180,118" ) ^
-compose over -layers merge +repage "C:\Users\james\Desktop\NewLogos.jpg"
Image result is (incorrect as the left image is not masked at all):
Image

Why in the world did that left-most image not get masked?
If we can answer that, I think we found the problem.

Re: Trim images with polygon and combine

Posted: 2018-01-12T14:51:13-07:00
by jmaeding
BTW Fred, we get this working and I will paypal you $100. You have already gone way above the calling!

Re: Trim images with polygon and combine

Posted: 2018-01-12T15:47:36-07:00
by fmw42
Post your logo.jpg file or tell me how you created it. I need to start with your original.

Re: Trim images with polygon and combine

Posted: 2018-01-12T16:15:58-07:00
by fmw42
By the way, you must make the mask image size the same dimensions as the input. Otherwise, it will not make all of the outer region transparent. So your 123x118 needs to be the same size as your input. Since you did not provide that perhaps you already have.

Re: Trim images with polygon and combine

Posted: 2018-01-12T16:16:55-07:00
by jmaeding
Its from the install folder of IM.
You can grab a copy I posted here:
https://dn.hunsaker.com/?linkid=KZi4zr6 ... VD1e2/2KDA

Re: Trim images with polygon and combine

Posted: 2018-01-12T16:26:21-07:00
by fmw42
There is some kind of "leakage" of settings or geometry between parentheses. Something to do with the distort, I guess. I don't understand it yet. But the following works by add -respect-parenthesis to the command. I have reformatted it so that it always gets the size of the input and used mpr: to avoid reading the image more than once. Unix syntax.

Code: Select all

magick -respect-parenthesis logo.jpg +write img.png -write mpr:img +delete \
\( mpr:img -fill black -colorize 100 -fill white -stroke white \
    -draw "polygon 23,18 23,113 113,113 113,18 23,18" +write mask.png -write mpr:mask +delete \) \
\( mpr:img mpr:mask -alpha off -compose CopyOpacity -composite +write tmp1.png \) \
\( mpr:img mpr:mask -alpha off -compose CopyOpacity -composite -alpha set -virtual-pixel none \
    +distort affine "0,0 -90,0 0,118 -90,118" +write tmp2.png \) \
\( mpr:img mpr:mask -alpha off -compose CopyOpacity -composite -alpha set -virtual-pixel none \
    +distort affine "0,0 -180,0 0,118 -180,118" +write tmp3.png \) \
-compose over -background none -layers merge +repage NewLogos.png
I have added writes to save the intermediate files, so you can see each step. You can remove the extra writes, but not the ones for mpr:

Re: Trim images with polygon and combine

Posted: 2018-01-12T16:35:41-07:00
by jmaeding
BIIIINGOOOOOO!
Dang, guess we know why the word "magick" in the name, you pulled that out of a hat like a pro.
Unbelievable, good catch.
You have a paypal name? or email me at jmaeding **at** hunsaker **dot** com with location to send check if you prefer.
Or amazon gift card, you name it.
Well earned.