Page 1 of 3
Fit one image on its place on another image
Posted: 2010-05-28T16:41:50-07:00
by Fludimir
I have a set of pictures, like this one
http://i5.fastpic.ru/big/2010/0529/d6/8 ... 18aed6.jpg . And I have another custom picture, some photo as example, which I wish to put onto first picture, to make women hold in hands not white paper but my photo.
Can I fit my image(photo) onto the white paper in main image by using coords of the corners of the white paper ? Or maybe somebody advice me some other better way to do it? I'm not going to made this photorealistically, so I "simply" want this not to be striking. Yes, ImageMagick have a lot of different distorsion filters, but it's hardly for me to choose which is better for use in my situation. And I have a mid-lot number of main images, so it would be sadly to spend a couple of hours for each of them.
Sorry if my question is stupid, I didn't use ImageMagick before, but I hope I'll find it useful and will use it further

Re: Fit one image on its place on another image
Posted: 2010-05-28T16:51:03-07:00
by fmw42
yes, you can do that very easily by using -distort perspective with four corners of the image you want to put into the picture shown and the four corners of the white area:
see
http://www.imagemagick.org/Usage/distorts/#perspective
see
http://www.imagemagick.org/Usage/distor ... rol_points for the ordering of the control points
Note however, that if the aspect ratio of the image you want to put onto the white rectangle is not appropriate, then you may see some distortion due aspect ratio change, so that the image may look at bit compressed or expanded on one or the other dimension.
Re: Fit one image on its place on another image
Posted: 2010-05-28T17:12:20-07:00
by Fludimir
Thanks, I tried and get maybe a half of the expected result. Can you please help me with imagemagick options,to fix this..
http://i5.fastpic.ru/big/2010/0529/b5/9 ... 2993b5.jpg
My commandline was
Code: Select all
convert bg.jpg ( bull.jpg -distort Perspective "0,0 435,19 475,0 911,195 475,595 802,957 0,595 268,863" ) -composite result.jpg
Re: Fit one image on its place on another image
Posted: 2010-05-28T17:13:20-07:00
by fmw42
please provide a link to bull image so I have both images
also try adding
-virtual-pixel transparent -background none before the -distort
and see if that helps
Are you on Windows or Unix?
Re: Fit one image on its place on another image
Posted: 2010-05-28T17:16:01-07:00
by Fludimir
Re: Fit one image on its place on another image
Posted: 2010-05-28T17:19:05-07:00
by Fludimir
virtual-pixel transparent -background none before the -distort
I tried this, nothing changed
Sorry, incorrect commandline. After adding options before -distort I'm getting this
http://i5.fastpic.ru/big/2010/0529/9c/6 ... 139f9c.jpg
Currently I'm working on windows, but in future this will be web application on unix server
Re: Fit one image on its place on another image
Posted: 2010-05-28T17:35:26-07:00
by fmw42
This works. The key is that the second image must be the same size as the first and padded with transparency on the bottom and right sides. I did not know that was necessary or recall that fact. But it works.
convert bg.jpg \
\( bull.jpg -background none -gravity northwest -extent 1920x1088 -virtual-pixel transparent -distort perspective \
"0,0 434,19 475,0 910,194 475,595 803,957 0,595 270,864 " \) \
-compose over -composite bg_bull.jpg
Re: Fit one image on its place on another image
Posted: 2010-05-28T17:43:54-07:00
by Fludimir
Hmm, I tried +distort [options..] instead of -distort , so my commandline is
Code: Select all
convert bg.jpg ( bull.jpg -virtual-pixel transparent -background none +distort Perspective "0,0 435,19 475,0 911,195 475,595 802,957 0,595 268,863" ) -composite result.jpg
And took this
http://i5.fastpic.ru/big/2010/0529/ac/b ... e507ac.jpg
Maybe there is not a bug but some feature?..
I'm using ImageMagick-6.6.2-Q16-windows.zip "Portable Win32 static at 16 bits-per-pixel" , if you need this info
Re: Fit one image on its place on another image
Posted: 2010-05-28T17:45:22-07:00
by fmw42
Yes, I found my error. See my edited post just prior to yours.
Note I am on a Mac and am using Unix syntax. so remove the backslashes before the parens and replace the line ending backslash with ^ for windows, if I recall correctly.
This also works without having to extend the image (using +distort and -layers merge)
convert bg.jpg \
\( bull.jpg -background none -virtual-pixel transparent +distort perspective \
"0,0 434,19 475,0 910,194 475,595 803,957 0,595 270,864 " \) \
-layers merge bg_bull2.jpg
Re: Fit one image on its place on another image
Posted: 2010-05-28T17:57:29-07:00
by Fludimir
Oh, perfect, vary many thanks

Now I have a good point to work from it tomorrow (there is four a.m. in my place) But can you advice how to make transparent that area on my image, what currently overlays fingers?
te I am on a Mac and am using Unix syntax. so remove the backslashes before the parens and replace the line ending backslash with ^ for windows, if I recall correctly.
Yes, I'm changing your commandline for myself
Re: Fit one image on its place on another image
Posted: 2010-05-28T18:07:02-07:00
by fmw42
But can you advice how to make transparent that area on my image, what currently overlays fingers?
You will have to draw a binary mask to capture the fingers over the card. Then composite the original image and the perspective covered image using the mask so that the fingers from the original show back from the original onto the perspective generated image.
see
http://www.imagemagick.org/Usage/compose/#mask
for 3 image composite with the last image being a mask
Re: Fit one image on its place on another image
Posted: 2010-05-29T02:44:01-07:00
by Fludimir
Thanks. I've created simple alpha mask, here is it
http://i5.fastpic.ru/big/2010/0529/0c/b ... b3d70c.jpg [mask1.jpg]
But where in comandline I have to add mask1.jpg, or some other option? I tried to add mask1.jpg as third image parameter, tried other parametes such a clip-mask , but didn't get expected results - I simply get mask as image composed over, or something worse. How to use my mask.jpg as the alpha mask for bull.jpg before distortion and composing on bg image? Maybe I have first to combine bull.jpg and mask.jpg into png/tga file with alpha channel, and then use it?
And other question. Here is coords for image perspective distiortion
+distort Perspective "0,0 435,17 475,0 912,192 475,595 802,962 0,595 268,865"
Odd pairs of numbers are coords of corners of the bull.jpg , calculated by its dimensions. Can ImageMagick insert this values automatically?
Re: Fit one image on its place on another image
Posted: 2010-05-29T05:29:43-07:00
by Fludimir
I've looked to my images and see that I have to create alpha mask to every bg image (bg.jpg), instead of one time to a bull.jpg . Here is an example of mask
http://i5.fastpic.ru/big/2010/0529/56/4 ... ccd156.jpg for bg.jpg
So I need to take bg.jpg , compose over it bull.jpg in correct positions, and than - clip composed bull.jpg using the mask mentioned above. And how can I do it?
Re: Fit one image on its place on another image
Posted: 2010-05-29T08:28:14-07:00
by snibgo
Using a slightly different technique to fmw:
Code: Select all
convert bg.jpg -alpha set bgmask.jpg -alpha copy -channel A -negate -compose copy-opacity -composite fingers.png
for /F "usebackq" %%S in (`convert bull.jpg -format "ww=%%w\nhh=%%h" info:`) DO set %%S
set /A wm1=%ww%-1
set /A hm1=%hh%-1
convert bg.jpg ^
( bull.jpg -background none -virtual-pixel transparent ^
+distort perspective ^
"0,0 435,17 %ww%,0 912,192 %ww%,%hh% 802,962 0,%hh% 268,865" ) ^
-layers merge ^
fingers.png -composite ^
bg_bull2.jpg
"bgmask.jpg" is one of the images you supplied.
"fingers.png" contains just the fingers, with everything else transparent. You can see in the final image that fingers.png isn't perfect (because bgmask.jpg isn't either). You could use Gimp to erase and un-erase as necessary. For more realism, you could add the shadows as a partly transparent black.
I think (but I'm not sure) the perspective parameters should be the corner coordinates, so we need to subtract one from the height and width.
If you don't need fingers.png for other images, you can roll the two converts into a single command:
Code: Select all
convert bg.jpg ^
( -clone 0 ^
( bull.jpg -background none -virtual-pixel transparent ^
+distort perspective ^
"0,0 435,17 %wm1%,0 912,192 %wm1%,%hm1% 802,962 0,%hm1% 268,865" ) ^
-layers merge ^
) ^
( -clone 0 -alpha set bgmask.jpg -alpha copy -channel A -negate -compose copy-opacity -composite ) ^
-delete 0 ^
-compose over -composite ^
bg_bull2.jpg
Re: Fit one image on its place on another image
Posted: 2010-05-29T09:49:53-07:00
by Fludimir
Thanks for your answer, but I saw it only after finding simple way by myself

Here is my commandline now:
convert bg.jpg ( bull.jpg -virtual-pixel transparent -background none -gravity northwest -extent 1920x1088 -distort Perspective "0,0 435,17 475,0 912,192 475,595 802,962 0,595 268,865" ) bgmask.jpg -composite result.jpg
Yes, current alpha mask is bad, it was created only for testing purposes, now I can see it in action and make better. Also thanks for the way of getting image dimensions
Can anybody advise how to process overlapping image for better fit in background? I think I have to decrease contrast slightly, and to add some tint of blue - because white paper has light-blue tint on my image. Can I decrease output levels (0-255 => 16-235 - PC-to-TV, as example) with ImageMagick ? In docs I haven't found how to do it, only auto-levels and brightness/contrast.
Is there a simple way of making light shadow on distorted image, according to the way of perspective distorting, when parts that are farther must be more shadowed then nearest parts? I do not need it very much but it would be good to have it done