replace region of an image with another image

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?".
Post Reply
nowayfra
Posts: 6
Joined: 2012-02-25T15:47:44-07:00
Authentication code: 8675308

replace region of an image with another image

Post by nowayfra »

Hi, thats my first post here, so hello everybody.
Ok, i hope to make it short. I try to copy one image into another, in a way that my source image ( a small bitmap rectangle with an alpha channel) replaces all the pixel information (both color and alpha) of the second image. The second image also is a bitmap with color and alpha. The thing is i need to use convert for this task because it is part of the application this task is targeted for (correct regions of already existing image sequences rendered in Maya). So now all results i have got so far uses the alpha channel of my small rectangle to cut out the color of the rectangle and only replaces the masked areas of my rectangle over the second image.
So if convert is able to do this, what would the flags look like?
Thanks in advance!

edit: small error in the description
Last edited by nowayfra on 2012-02-25T17:27:45-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: replace region of an image with another image

Post by fmw42 »

Can you explain further? Do you want to keep the alpha channel of the background image everywhere, except where you put the smaller image so that where the smaller image exists, its alpha channel replaces that of the background image? Or do you have a background image that has no alpha or that you want removed before inserting the smaller image, so that the background alpha is off everywhere except where the small image in placed and there you get the small image's alpha?

what version of IM and on what platform?
nowayfra
Posts: 6
Joined: 2012-02-25T15:47:44-07:00
Authentication code: 8675308

Re: replace region of an image with another image

Post by nowayfra »

Yes, your first description is exactly what it should do, all of the background, alpha and color needs to be replaced by the region rectangle but all other pixels of the background outside the rectangle needs to be unaltered. The OS im using is windows and the imconvert.exe (so called here in the maya install dir) ranges from 6.2.4.8 (Maya2009) to 6.4.8 (Maya2012). Would this be possible with the older version or if not then with the newer one?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: replace region of an image with another image

Post by fmw42 »

One way to do that would be to remove and save the alpha channels from both images. Then composite the opaque color images where you want the smaller one placed. Then composite the two grayscale alpha images with the smaller one placed on the larger one. The put the resulting combined grayscale image into the alpha channel of the combined color images.

There may be an easier way. Have you tried:

convert largeimage smallimage -channel rgba -matte -geometry +X+Y -compose over -composite resultimage

or just

convert largeimage smallimage -geometry +X+Y -compose copy -composite resultimage

where +X+Y are the offsets of the top left corner of the small image with respect to its placement in the larger image.

can you provide links to your two images (and where you want the smaller one overlaying on the larger) so that we can test with them to get the right command line syntax that presumably might be compatible with your very ancient IM 6.2.x (which is about 500 versions old)
Last edited by fmw42 on 2012-02-26T13:25:06-07:00, edited 1 time in total.
nowayfra
Posts: 6
Joined: 2012-02-25T15:47:44-07:00
Authentication code: 8675308

Re: replace region of an image with another image

Post by nowayfra »

Here are two example images (test_base.tga, test_region.tga), packed with winrar: http://rghost.net/36723054

This is the base image with its alpha channel on the right:

Image


and this is the region with a new refracting lens object:

Image


now i crop the full size image with the region down to the region size

Code: Select all

imconvert.exe -crop 153x154+168+18 c:\test_region.tga c:\test_region_cropped.tga
result:
Image


using the the older imconvert version from Maya 2009, both of your lines gave me this:

Code: Select all

imconvert.exe c:\test_base.tga c:\test_region_cropped.tga -channel rgba -matte -geometry +168+18 -compose over -composite c:\test_result_1.tga
imconvert.exe c:\test_base.tga c:\test_region_cropped.tga -geometry +168+18 -compose over -composite c:\test_result_2.tga
Image


result for both commands with imconvert for Maya 2012:

Image


using the actual imageMagick version, composite.exe brings the correct result:

Code: Select all

composite.exe -compose Copy -geometry +168+18 c:\test_region_cropped.tga c:\test_base.tga -alpha set c:\test_result_3.tga
Image


is there something in convert that brings the similar result?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: replace region of an image with another image

Post by fmw42 »

composite.exe -compose Copy -geometry +168+18 c:\test_region_cropped.tga c:\test_base.tga -alpha set c:\test_result_3.tga
convert c:\test_base.tga c:\test_region_cropped.tga -geometry +168+18 -compose Copy -composite -alpha set c:\test_result_3.tga

Note the input images are swapped in their order when using convert -composite vs composite

see
http://www.imagemagick.org/Usage/compose/#compose
http://www.imagemagick.org/Usage/layers/#convert
nowayfra
Posts: 6
Joined: 2012-02-25T15:47:44-07:00
Authentication code: 8675308

Re: replace region of an image with another image

Post by nowayfra »

Thanks, this works with the newer version for Maya 2012, i can even skip "-alpha set" to get the right result. Because if i use the older version, imconvert says that "-alpha" is an unrecognized option.
So with the older version this command

Code: Select all

imconvert.exe c:\test_base.tga c:\test_region_cropped.tga -geometry +168+18 -compose Copy -composite c:\test_result_4.tga
gives this result

Image

now oddly the almost nonexisting alpha inside the lens is handled as it should but the completely unmasked color pixels of the region are omitted, as well as the unmasked pixels of the base image.
Maybe we now can do something with this result and the base image to get back the "sky"?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: replace region of an image with another image

Post by fmw42 »

imconvert.exe c:\test_base.tga c:\test_region_cropped.tga -geometry +168+18 -compose Copy -composite c:\test_result_4.tga
try

imconvert.exe c:\test_base.tga c:\test_region_cropped.tga -geometry +168+18 -compose Copy -matte -composite c:\test_result_4.tga

or

imconvert.exe c:\test_base.tga c:\test_region_cropped.tga -matte -geometry +168+18 -compose Copy -composite c:\test_result_4.tga

see
http://www.imagemagick.org/Usage/masking/#alpha_set
nowayfra
Posts: 6
Joined: 2012-02-25T15:47:44-07:00
Authentication code: 8675308

Re: replace region of an image with another image

Post by nowayfra »

Ok, i've got it:

Code: Select all

imconvert.exe c:\test_base.tga c:\test_region_cropped.tga -matte -geometry +168+18 -compose Copy -composite c:\test_result_5.tga
imconvert.exe c:\test_base.tga c:\test_region_cropped.tga +matte -geometry +168+18 -compose Copy -composite c:\test_result_6.tga
imconvert.exe c:\test_result_6.tga c:\test_result_5.tga -compose CopyOpacity -composite c:\test_result_7.tga
It wont win the beauty price but at least now it works with the old imconvert.
Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: replace region of an image with another image

Post by fmw42 »

nowayfra wrote:Ok, i've got it:

Code: Select all

imconvert.exe c:\test_base.tga c:\test_region_cropped.tga -matte -geometry +168+18 -compose Copy -composite c:\test_result_5.tga
imconvert.exe c:\test_base.tga c:\test_region_cropped.tga +matte -geometry +168+18 -compose Copy -composite c:\test_result_6.tga
imconvert.exe c:\test_result_6.tga c:\test_result_5.tga -compose CopyOpacity -composite c:\test_result_7.tga
You should be able to do this all in one command using clones and parenthesis processing.

imconvert.exe c:\test_base.tga c:\test_region_cropped.tga ^
( -clone 0 -clone 1 -matte -geometry +168+18 -compose Copy -composite ) ^
( -clone 0 -clone 1 +matte -geometry +168+18 -compose Copy -composite ) ^
-delete 0,1 +swap +matte -compose copy_opacity -composite c:\test_result_7.tga


see
http://www.imagemagick.org/Usage/basics/#image_seq
nowayfra
Posts: 6
Joined: 2012-02-25T15:47:44-07:00
Authentication code: 8675308

Re: replace region of an image with another image

Post by nowayfra »

This nesting seems to work much faster and i would prefer it in my code, but your example returns the cropped region..?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: replace region of an image with another image

Post by fmw42 »

nowayfra wrote:This nesting seems to work much faster and i would prefer it in my code, but your example returns the cropped region..?
try resetting the compose method to over before the final -compose copy_opacity

imconvert.exe c:\test_base.tga c:\test_region_cropped.tga ^
( -clone 0 -clone 1 -matte -geometry +168+18 -compose Copy -composite ) ^
( -clone 0 -clone 1 +matte -geometry +168+18 -compose Copy -composite ) ^
-delete 0,1 +swap +matte -compose over -compose copy_opacity -composite c:\test_result_7.tga

or

imconvert.exe c:\test_base.tga c:\test_region_cropped.tga ^
( -clone 0 -clone 1 -matte -geometry +168+18 -compose Copy -composite ) ^
( -clone 0 -clone 1 +matte -geometry +168+18 -compose Copy -composite ) ^
-delete 0,1 +swap -compose over -compose copy_opacity -composite c:\test_result_7.tga

My command should reproduce your 3 commands, but I am not on Windows, so I may have missed something where the syntax is different.

If that does not work, try removing the +swap, but I am pretty sure it is needed.
Post Reply