Page 1 of 2
Replace Region in Picture with Another Picture
Posted: 2012-11-05T10:06:19-07:00
by GeorgeIoak
I'm looking for a way to define a region on a picture and then replace that region with another picture. The region to be replaced could be skewed so I'm thinking I need something to define the following:
1) Start location x1, y1
2) End location x2, y2
3) Skew Angle?
Then based on the start and end location use that to scale the image to be inserted.
Then skew the scaled imaged.
Finally insert the skewed and scaled image
I'm going to need to do this to a bunch of images so I'm looking for a way to define and automate this. The "template" pictures will not change but the inserted image will. I could either manually define the above suggested items or each template picture could be created with a solid color which we then look for and replace with the skewed and scaled image.
I'm brand new to IM so any helpful advise would be greatly appreciated!
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T10:51:51-07:00
by Bonzo
More information required: IM version - operating system - coding program shell, batch php etc.
You can either cut a hole in the larger image and composite it over the second image or if you want all of the second image just composite it over the large image.
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T11:02:28-07:00
by fmw42
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T12:40:44-07:00
by GeorgeIoak
More information required: IM version - operating system - coding program shell, batch php etc.
Sorry, I assumed at this point that this was a generic enough question to not be needed. I can use any version but would assume it's best to use the most recent. This would run on Debian and would either script or php code, whichever is better suited to this task.
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T13:03:45-07:00
by Bonzo
There are so many different ways to use Imagemagick and it is always being updated
An overlaid photo example on my site:
http://www.rubblewebs.co.uk/imagemagick ... ample1.php
An example with a "hole" in the top image:
http://www.rubblewebs.co.uk/imagemagick ... example=56
All with php which is the method I prefer.
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T13:05:37-07:00
by fmw42
See my bash unix script, skew, at the link below. Just compute the offset in x or y. Then feed it to the script using a transparent virtual-pixel (default). Then composite the image over the region you want to fill. You may need to compute the correct composite coordinates based upon the top left of the resulting image coordinate or use -gravity center and the center of the skewed image coordinate from the center of the background image.
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T13:24:59-07:00
by GeorgeIoak
Thanks those both look promising. The "hole" example is along the lines of what I was thinking (but with some additional processing the make it look more real). Skew definitely will be needed. I'm not an image person so forgive me if I fumble. Skew almost looks more like a rotate with a different perspective. Like if you have picture laying flat on a desk and you walk around the desk looking at the picture. What happens if the picture is then stood up on a desk (I guess this would be a rotation in Z axis?). I think in flying they call this pitch and yaw so I would need to define both of those wouldn't I if I wanted to get the most realistic final image?
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T13:27:55-07:00
by Bonzo
The best bet would be to post an example of what you are after - using photoshop, Gimp or whatever if possible.
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T14:35:38-07:00
by GeorgeIoak
and example would be something like this,
http://cdn.slashgear.com/wp-content/upl ... 40x303.jpg
We would want to change what is being displayed on the tablet using a stored picture
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T15:18:23-07:00
by Bonzo
Quick rough reply as off to bed now.
I added a blur to the mask so it was not such a sharp edge but it disapears on the final image!
You would need to sort your dimensions out and probably write some code to calculate variables.
fmw42 will probably improve on this as he is cleverer than me
Written in php as that is what I use but you will get the idea.
Code: Select all
// Resize and shape the image for the screen
$cmd = " ( IMG_5745.jpg -thumbnail 200x ) -matte -virtual-pixel transparent ".
" -interpolate Spline ".
" -distort BilinearForward \"0,0 25,0 200,0 300,18 200,67 280,80 0,100 -80,100\" ";
exec("convert $cmd screen.png");
echo "<img src='screen.png'>";
// Create the mask with a blurred edge
$cmd = " -size 540x303 xc:none -fill black ".
" -draw \"polyline 185,74 439,103 362,312 123,210 \" -channel A -blur 0x2 ";
exec("convert $cmd mask.png");
echo "<img src='mask.png'>";
// Cut the hole out of the top image
$cmd = " -compose Dst_Out mask.png -gravity center original_tablet.jpg -matte ";
exec("composite $cmd result_dst_out1.png");
echo "<img src='top.png'>";
// Put the top image over the bottom
$cmd = " top.png screen.png -gravity center -composite ";
exec("convert $cmd output_temp.jpg");
echo "<img src='output_temp.jpg'>";
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T15:22:20-07:00
by Bonzo
If you are using a standard image you would only need to do steps 2 and 3 once.
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T15:24:08-07:00
by GeorgeIoak
Thanks! I'm dig into this and do some playing around to see what I can do. PHP is fine for me at this point. Have a good night...
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T15:33:59-07:00
by fmw42
Your example is basically a 4 pt perspective distortion. See
http://www.imagemagick.org/Usage/distorts/#perspective. So to create the "skewed" image, you need to provide the input and output corner coordinates. The rest is as discussed above to composite the perspective distorted image (with outside being transparent) over the large image at the correct coordinates.
Specify the output coordinates as the coordinates of the skewed hole and use +distort perspective. That should make the output image the size needed so that it overlays without needing and offset calculations. Then use -compose over -layers merge to composite them. That is disucssed at
http://www.imagemagick.org/Usage/distorts/#box3d, though you are only using a single image and not 3D. It should work fine.
If you provide an example of your background image and the foreground image that you want to distort over the background and the points in the background to correspond to the corners of the foreground, then we can probably give you the code.
convert background \( foreground -virtual-pixel transparent +distort perspective "xs1,ys1 xd1,yd1 .... xs4,ys4 xd4,yd4" \) -compose over -layers merge result image.
where xs,ys are the corners of your foreground image and xd,yd are the corresponding locations in the background image where the foreground corners should match after the distortion.
If on windows, the remove the \
see
http://www.imagemagick.org/Usage/windows/
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T16:09:51-07:00
by GeorgeIoak
OK, you've given me a good start and things to look at and understand. I'll read through the examples again to get a good understanding and try a few tests on my own.
This is probably a dumb question but what's the easiest way to find the x,y locations of a point in the picture to feed into the IM commands? For instance what method would you use to get the 4 corners of the screen in the example picture I referenced?
Re: Replace Region in Picture with Another Picture
Posted: 2012-11-05T19:50:49-07:00
by fmw42
GeorgeIoak wrote:OK, you've given me a good start and things to look at and understand. I'll read through the examples again to get a good understanding and try a few tests on my own.
This is probably a dumb question but what's the easiest way to find the x,y locations of a point in the picture to feed into the IM commands? For instance what method would you use to get the 4 corners of the screen in the example picture I referenced?
I would measure them in GIMP or Photoshop (or any other GUI image display app).
If you are on Linux or Mac, you can get coordinates by using the IM command display yourimage. Then hold down the option key and left mouse click and hold down on the picture. A window will open showing coordinates while you drag the mouse around the image.