Page 1 of 2
Clip image and paste into another in one step?
Posted: 2017-03-26T12:13:46-07:00
by jmaeding
I believe this can be done in three steps by:
1) make black and white mask image using stencil and coordinate list of closed "clipping" shape.
2) turn unwanted pixels to transparent using mask
3) Paste "mask clipped" image into another at desired location and angle using distort
Can that be done in one step?
I don't mind 3, but it will be slower and I am dealing with huge images (aerial photos).
thanks
Re: Clip image and paste into another in one step?
Posted: 2017-03-26T12:16:04-07:00
by fmw42
Can you provide an example input set of images with the clip path included in one? Please always provide your IM version and platform/OS since syntax differs.
Yes, it can be done in one command line probably using parenthesis process and mpr: or clones.
Re: Clip image and paste into another in one step?
Posted: 2017-03-27T11:23:06-07:00
by jmaeding
Oh, sorry I am on windows 10 using IM 6.6.8.
I could use the latest if it helped.
I am writing in C# using interop to do code like this:
ImageMagickObject.MagickImage img = new ImageMagickObject.MagickImage();
object[] args2 = args.ToArray();
object ret1 = img.Convert(args2);
All I am doing is building a list of arguments for the convert method, so you could also treat it like I will be making a dos batch file using convert.exe.
I can post example images, but really its probably better to just use 2 generic jpg's, one all read and one all blue.
The first must be cropped to a hexagon shape, then pasted into the second at some point, and rotated 45 deg.
What I am doing is taking one photo, cropping it using an arbitrary closed polygonal boundary, and pasting it into another at a specific point, scale, and rotation. The distort command does the last part easily as I calculate where the 4 corners of pasted image will be in original.
I just don't know how to do this in layers instead of separate images (something like that).
thx
Re: Clip image and paste into another in one step?
Posted: 2017-03-27T11:29:17-07:00
by fmw42
It would be best for you to provide an example with the clip path in the file. Provide both inputs and if possible and example of what you want for the result. You can post to any free hosting service such as dropbox.com and put the URL here. I do not have any images with clip paths to test.
Re: Clip image and paste into another in one step?
Posted: 2017-03-27T11:33:41-07:00
by fmw42
Are you asking to use an image with clip path included (such as in Tiff) or do you just have a binary image that represents the mask you want to use? Or perhaps just a list of points that define the outline of the mask area?
Re: Clip image and paste into another in one step?
Posted: 2017-03-27T11:48:03-07:00
by snibgo
jmaeding wrote:... I am on windows 10 using IM 6.6.8.
IM v6.6.8 is old. Very very old. Prehistoric. If it works, that's great, but suggestions we make may not work in such an old version.
jmaeding wrote:What I am doing is taking one photo, cropping it using an arbitrary closed polygonal boundary, and pasting it into another at a specific point, scale, and rotation. The distort command does the last part easily as I calculate where the 4 corners of pasted image will be in original.
So you want help with the first part, cropping to an arbitrary polygonal boundary?
By "cropping", I assume you want to make transparent all the pixels outside the boundary. I would do this with "-compose CopyOpacity -composite". For that, you need an mask image that is white inside the boundary, and black outside the boundary.
To make the mask, you could use -draw "polygon x0,y0 ... xn,yn".
Re: Clip image and paste into another in one step?
Posted: 2017-03-27T14:42:12-07:00
by fmw42
If you post your example images and the 3 steps as 3 command lines, we can help you make it into one command line. The way to combine steps is to use parenthesis processing and clones or mpr:. See
http://www.imagemagick.org/Usage/basics/#parenthesis
Re: Clip image and paste into another in one step?
Posted: 2017-03-28T18:00:23-07:00
by jmaeding
So I updated to the latest, and have tried a few things that have me to the last step.
Once I get the individual steps down, I think using parens will get to one statement.
I am still concerned about speed on all this, as I am pasting images 5000x5000 pixels into 30,000x40,000 pixel image.
I'm ok if that takes like a minute, but need to be careful about dong anything twice.
I was able to make the mask image using coordinates - great.
I was able to use that to trim the image that is going to be pasted - great.
I now need to paste the image into another, and I know the where the 4 corners should go in image coordinates.
I believe distort is the right operation, but all the examples use the original image itself or a virtual canvas that gets repaged at the end.
I want to use an existing image to paste into, and that image must not change size. Pixels that land off it are gone.
What is the distort (or other) way to do that?
thanks
Re: Clip image and paste into another in one step?
Posted: 2017-03-28T18:53:32-07:00
by snibgo
"30,000x40,000 pixel" is large. Assuming v6 Q16, this needs 9.6 GB of memory. If you don't have that much available, it will use disk, which will be slow. If your computer doesn't have much more memory in total (eg 12 GB), make sure you are not running anything else, eg web browsers.
It seems that "-distort Perspective" is suitable. See
http://www.imagemagick.org/script/comma ... hp#distort You can give it coordinates of the corners of the input image, and where you want those four corners to go. So it needs 16 numbers.
So the overall command would be something like:
Code: Select all
convert ^
largeimage.tiff ^
( maskedimage.tiff -distort Perspective "{16_numbers}" ) ^
-compose Over Composite ^
out.tiff
Instead of "maskedimage.tiff", you could have an expression that takes an image and applies a mask. But while developing the workflow, it is often easier to write multiple converts, one per stage.
Re: Clip image and paste into another in one step?
Posted: 2017-03-28T18:59:11-07:00
by fmw42
If you want the image cropped after rotating, just use -distort SRT. If you want the image to expand when rotating use +distort SRT. I would do the rotation using distort onto a transparent background. Then you can compose that image onto your other image using -gravity and -geometry to control the placement using the upper left corner of the of the rotated image.
But you may be better by using +distort and the desired angle and start x,y and final x,y even if not on the original image. The transparent background will expand and the system will know where the coordinates are to be if you do a -layer merge +repage onto the other image.
see
http://www.imagemagick.org/Usage/layers/#convert
Again if you can provide an example of the two images and where you want one placed relative to the other, we can help better. Even a diagram would be helpful.
Re: Clip image and paste into another in one step?
Posted: 2017-03-28T19:05:11-07:00
by fmw42
You can use more that 4 point pairs in -distort perspective as in snibgo's code. You can also do the same using -distort Affine (also with more than 3 point pairs. When you do that, it will do a least square fit. However, if you want 3 (affine) or 4 (perspective) points to perfectly match, then limit it to those 3 or 4 points.
Note that doing perspective or affine, in general, you could get some skew in your result. But if your points in the second image are spaced properly relative to those in the first image, then using only 3 or 4 points should be fine. Just do not pick them all in a line.
(It would be nice to have a version of distort Affine that was limited to pure rotation, scale and translation via control points)
Re: Clip image and paste into another in one step?
Posted: 2017-03-29T08:23:37-07:00
by jmaeding
great replies all! I will try today.
I have 64 gigglebytes of memory and decent xeon CPU for my CAD laptop.
Funny because I bet gamers laugh at the term "CAD laptop" these days.
Autodesk makes an image editing tool called Raster Design that runs on autocad.
It will not handle the large image I mentioned. You have no idea how sweet it is to replace tools like Raster Design with our own, as then they run on Bricscad and other non adesk platforms. Reliance on Autodesk software is expensive these days since they raised prices a bunch and are going to rental only.
Re: Clip image and paste into another in one step?
Posted: 2017-03-29T09:14:27-07:00
by snibgo
jmaeding wrote:64 gigglebytes
You should be laughing, with that much memory.
Re: Clip image and paste into another in one step?
Posted: 2017-03-29T15:18:32-07:00
by jmaeding
I finally got this to paste the two images together at proper coordinates:
"C:\Program Files\ImageMagick-7.0.5-Q16\magick.exe" "C:\Temp\Orig.JPG" ( "C:\Temp\Clipped.png" -distort Affine "0,0 200,200 0,1440 400,800" ) -background none -compose over -layers merge +repage "C:\Temp\Final.jpg"
I used affine instead as I am only doing SRT type operation.
I could not get this approach to work:
convert largeimage.tiff ( maskedimage.tiff -distort Perspective "{16_numbers}" ) -compose Over Composite out.tiff
Specific code for me was:
"C:\Program Files\ImageMagick-7.0.5-Q16\magick.exe" "C:\Temp\Orig.JPG" ( "C:\Temp\Clipped.png" -distort Affine "0,0 200,200 0,1440 400,800" ) -compose Over Composite "C:\Temp\Final.jpg"
It gave an error saying "unable to open image 'Composite'..." so that parameter is out of place I think.
I have not tried on the super large images yet, but am wondering if -layers merge is better or worse than just -compose, though I could not get -compose by itself to work.
Re: Clip image and paste into another in one step?
Posted: 2017-03-29T15:19:37-07:00
by jmaeding
...You should be laughing, with that much memory.
With much power comes great responsibility....
I do laugh every time I say "gigglebyte", can hardly type this right now...gigglebyte gigglebyte gigglebyte