Trim images with polygon and combine

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

Trim images with polygon and combine

Post by jmaeding » 2018-01-11T10:39:39-07:00

I want to take several images, trim each by some polygon, and paste them together at various locations.
To make the simplest example I could, take logo.jpg, trim to a 90x90 square, and put 4 of them together to form a 2x2 tiles square.
In my example, the first "tile" stays put, it is not moved.
The others get moved with affine +distort, then I crop at the end.

What you will see in my example could be summarized like this:
magick.exe
(trim logo.jpg by some polygon)
((trim logo.jpg by some polygon) distort affine to some location with pixels outside trim area as transparent)
((trim logo.jpg by some polygon) distort affine to some location with pixels outside trim area as transparent)
((trim logo.jpg by some polygon) distort affine to some location with pixels outside trim area as transparent)
merge them all in the order above, and crop to some size using some offset.

Here is the actual code for a console batch file:

"C:\Program Files\ImageMagick-7.0.5-Q8\magick.exe" ^
( "logo.jpg" ( -size 123x118 xc:black -fill white -stroke white -draw "polygon 23,18 23,90 90,90 90,18 23,18" ) ^
-alpha off -compose CopyOpacity -composite ) ^
( ( "logo.jpg" ( -size 123x118 xc:black -fill white -stroke white -draw "polygon 23,18 23,90 90,90 90,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,90 90,90 90,18 23,18" ) ^
-alpha off -compose CopyOpacity -composite ) -alpha set -virtual-pixel transparent +distort affine "0,0 -90,90 0,118 -90,208" ) ^
( ( "logo.jpg" ( -size 123x118 xc:black -fill white -stroke white -draw "polygon 23,18 23,90 90,90 90,18 23,18" ) ^
-alpha off -compose CopyOpacity -composite ) -alpha set -virtual-pixel transparent +distort affine "0,0 0,-90 0,118 0,208" ) ^
-compose over -layers merge -crop 180x180-67+23 +repage "C:\Users\james\Desktop\NewLogos.jpg"

I should be getting 4 squares nicely next to each other in 2x2 tiled pattern.
But it fails. I think I am getting two things wrong:
1) the pixels outside my trim area are not becoming transparent
2) my understanding of the virtual canvas must be off. The distort is not doing what I expected.

any help appreciated, I am a big IM fan.
James Maeding
Civil Engineer / Programmer

User avatar
fmw42
Posts: 23678
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim images with polygon and combine

Post by fmw42 » 2018-01-11T11:04:05-07:00

I suggest you take one command at a time and see what result you get. You are not trimming by adding the alpha channel nor making the rest of the alpha channel transparent. Your black background to the white polygon likely needs to be as large as your logo.jpg image for one thing.

"C:\Program Files\ImageMagick-7.0.5-Q8\magick.exe" ^
( "logo.jpg" ( -size 123x118 xc:black -fill white -stroke white -draw "polygon 23,18 23,90 90,90 90,18 23,18" ) ^
-alpha off -compose CopyOpacity -composite +write tmp1.png ) ^
null:

Do this for each of your four parts until you get each part looking the way it should. Then composite them. Then go back and combine all the commands into one command.

jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: Trim images with polygon and combine

Post by jmaeding » 2018-01-11T11:28:00-07:00

Good idea. How about we just see if we can get two images to work, so 1 row of 2 images.
So just trimming one image works correct. I get the logo with a white border.
When I added the 1st additional image, it almost behaved, but the distort did not shift the image -90 pixels like I expected.
It did give a final image width of 214, so that is original + 91 pixels, but there is a white vertical strip about 10 pixels wide between the two trimmed pieces.
That tells me something is up with the virtual canvas.
I thought:
1) trimming the first image does not affect its canvas. I did not +repage it.
2) when I trim the second image, its virtual canvas is also the same.
3) when I distort affine the second image, the from coords use its virtual canvas, and the to coordinates use the virtual canvas of first image.

I must be incorrect on item 3.
Here was my code:
"C:\Program Files\ImageMagick-7.0.5-Q8\magick.exe" ^
"C:\Program Files\ImageMagick-7.0.5-Q8\images\logo.jpg" ( -size 123x118 xc:black -fill white -stroke white -draw "polygon 23,18 23,90 90,90 90,18 23,18" ) -alpha off -compose CopyOpacity -composite ^
( ( "C:\Program Files\ImageMagick-7.0.5-Q8\images\logo.jpg" ( -size 123x118 xc:black -fill white -stroke white -draw "polygon 23,18 23,90 90,90 90,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 "C:\Users\james\Desktop\NewLogos.jpg"

I also played with -alpha on in the -compose part of the trim. I think that will be needed later but I need to get through this first test first.
James Maeding
Civil Engineer / Programmer

User avatar
fmw42
Posts: 23678
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim images with polygon and combine

Post by fmw42 » 2018-01-11T11:38:01-07:00

Do each command separately and see if they work as desired. There could be some setting that needs to be reset. If you just want to shift and image by 90 pixels, you can use -roll or +distort SRT. You can also trim each part and then use -set page for each image before the layers merge. Not sure if that helps or not.

If I have not made a mistake, it looks to me that your polygon is masking the same area each time and not being shifted to get other parts of your image. If you want to capture different parts of the logo.jpg, then you either need to shift the logo image or shift the mask image, not the result after adding the same polygon to the alpha channel.

User avatar
fmw42
Posts: 23678
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim images with polygon and combine

Post by fmw42 » 2018-01-11T11:55:29-07:00

P.S. You can also use clones or mpr: to duplicate your logo.jpg in memory, so you do not have to read it 4 times.

User avatar
fmw42
Posts: 23678
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim images with polygon and combine

Post by fmw42 » 2018-01-11T12:08:27-07:00

Try this for two sections. Unix syntax

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 -set page +0+0 +write tmp1.png \) \
\( mpr:img \( mpr:mask -roll +90+90 \) -alpha off -compose copy_opacity -composite -set page +25+25 +write tmp2.png \) \
-compose over -background none -layers merge -trim +repage NewLogos.png

jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: Trim images with polygon and combine

Post by jmaeding » 2018-01-11T13:01:13-07:00

I see what you are doing with the mpr:, but not +25+25. Why would you do that in this situation?
I would think you might only reset the 0,0 of canvas if expanding it in negative directions, but you did positive.

I made some progress on my previous tests. I realized my trim coords were off, so fixed it to "23,18 23,113 113,113 113,18 23,18".
Once I did that, this works:
"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 -crop 180x180-67+18 +repage "C:\Users\james\Desktop\NewLogos.jpg"

but the moment I add in another image, it is not respecting the trim of it. It gets distorted to right location, but it overwrites the other tiles with its extra edge. This is exactly the problem I am seeing on my real use. The 2nd image added one is not trimmed.
Then if you add a 3rd, it really is messed up. It seems to be scaled huge and overwrites the right half.
I'll play with the set page some more.
James Maeding
Civil Engineer / Programmer

jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: Trim images with polygon and combine

Post by jmaeding » 2018-01-11T14:04:24-07:00

oh, I see the problem. I thought +distort meant "expand virtual canvas as needed", but that is incorrect.
Instead +distort tries to fit things somehow. From the IM web page:

"The other form of the operator, "+distort" (Added to IM v6.3.5-7), will attempt resize the distorted image so it will contain the whole of the input image (if possible), much like what the older Rotate and Shearing operators do. As such you may need to make judicious use of the "+repage" attribute setting operator to clear or adjust that offset before using the 'best-fit' "+distort" form of the General Distortion Operator. You also may need to use it after if the virtual canvas and offset is not required. See also Removing Canvas/Page Geometry."

I do not want fit going on, so I bet the easiest way around this is to set up an initial image that is big enough to contain the others. I can do that as I already have to figure out the new extents relative to the original, and in which direction.
James Maeding
Civil Engineer / Programmer

jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: Trim images with polygon and combine

Post by jmaeding » 2018-01-11T15:29:34-07:00

The more I study and test, the more I think I am doing it correct.
I did a test to just tile unclipped images, worked perfect:
"magick.exe" ^
"logo.jpg" ^
( "logo.jpg" -alpha set -virtual-pixel transparent +distort affine "0,0 -123,0 0,118 -123,118" ) ^
( "logo.jpg" -alpha set -virtual-pixel transparent +distort affine "0,0 -123,118 0,118 -123,236" ) ^
( "logo.jpg" -alpha set -virtual-pixel transparent +distort affine "0,0 0,118 0,118 0,236" ) ^
-compose over -layers merge +repage "C:\Users\james\Desktop\NewLogos.jpg"

So while I thought +distort was a problem, the examples and my tests show it does not "self" scale fit things.

The next suspect is the trimming operation, as it uses "-alpha off -compose CopyOpacity -composite"
Maybe -compose or -composite are messing with the canvas coords. I need them to reference the original image, not repage.
This might be some implied behavior by dealing with more than 2 layers.

I only used -Distort in the past so never ran into this before.
James Maeding
Civil Engineer / Programmer

User avatar
fmw42
Posts: 23678
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim images with polygon and combine

Post by fmw42 » 2018-01-11T15:51:04-07:00

I simply use +25+25 as the offset between the two parts when doing the -layers merge. You can offset them as you want. I am having a hard time understanding what you want. Perhaps you can make an example in Photoshop or GIMP and post that.

jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: Trim images with polygon and combine

Post by jmaeding » 2018-01-11T16:20:46-07:00

I would love to post pics, but have to relearn how to add them to this forum.
I think the simplest way to explain what I want is I have 4 images that I know their unclipped locations, as well as the clipping polygon coords.
I want to clip them and then composite to one image.
They must end up exactly where the coordinates say to.
The image info is coming from images I have in Autocad, which have "clips" on them.
I am making a tool to "combine images" and I want the end result to replace the 4 (or whatever there is) images with one.

I think I am super close on this, there just must be some detail tripping things up.
Maybe its +distort getting kicked into "fit" mode, I don't get what triggers that.
James Maeding
Civil Engineer / Programmer

User avatar
fmw42
Posts: 23678
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim images with polygon and combine

Post by fmw42 » 2018-01-11T16:44:43-07:00

To add pictures, you must upload to some free hosting service what won't change your format (such as dropbox.com) and then put the URLs here.

Imagemagick can use clip paths, if that helps. I am still not sure I understand. Do you want to just blank out the areas between each section? Or crop each section and append them together? Or something else. A picture would really help here.

jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: Trim images with polygon and combine

Post by jmaeding » 2018-01-11T17:01:17-07:00

will do. I agree this is nuts without pics.
James Maeding
Civil Engineer / Programmer

jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: Trim images with polygon and combine

Post by jmaeding » 2018-01-11T17:32:25-07:00

Lets try this.
Image

On the lower image with all rotated stuff, the final image would be a rectangle of course. Any extra area added would be white.
Does that help a bit?
thx
James Maeding
Civil Engineer / Programmer

jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: Trim images with polygon and combine

Post by jmaeding » 2018-01-11T17:38:54-07:00

Here is a real situation with several maps I want to stitch. I am zoomed out a bit but these are not square to eachother, and the clips are not simple rectangles but may have 6 or more points involved.
Image

I'd think if I can figure out how to add images to posts, I could get the actual software working :)
James Maeding
Civil Engineer / Programmer

Post Reply