Image composition

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
pablobhz
Posts: 51
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Image composition

Post by pablobhz » 2017-04-11T08:04:41-07:00

Hello everyone.
I'm trying to composite one image on the top of the another.

However, when i use the following code:

Code: Select all

            using (MagickImage finalImage = new MagickImage("backgroundimage.png"))
            {
                
                using (MagickImage spotImage = new MagickImage("imagetocomposite.png"))
                {
                    spotImage.Composite(finalImage, Gravity.Center);
                    spotImage.Write("finalimage.png");
                }                                             

            }   
Nothing happens, the background image doesn't have the image i want to composite.

I also tried to do it using command line (for testing):

Code: Select all

composite backgroundimage.png imagetocomposite.png -gravity center finalresult.png
Didn't worked as well.

Am i missing something ?
Thanks

snibgo
Posts: 9379
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image composition

Post by snibgo » 2017-04-11T08:47:56-07:00

I don't use Magick.NET, but your code seems to composite finalImage over spotImage. I suspect you want this the other way round.

Your command doesn't contain the keyword "-composite" so it won't do a composite.
snibgo's IM pages: im.snibgo.com

pablobhz
Posts: 51
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Image composition

Post by pablobhz » 2017-04-11T14:41:28-07:00

snibgo wrote:
2017-04-11T08:47:56-07:00
I don't use Magick.NET, but your code seems to composite finalImage over spotImage. I suspect you want this the other way round.

Your command doesn't contain the keyword "-composite" so it won't do a composite.
Yeah, that was incorrect. I inverted the command line, but, the result was the same :(
I also checked on Adobe Photoshop image coordinates ( to see if i was missing something ) - it was okay, the lines should be there.

put this image:
http://i.imgur.com/LX1FNLa.png (large image) (img1)
over this image
http://i.imgur.com/LpbkJgf.jpg (large image) (img2)

So the light red stripes will appear on img2
However, they're not appearing.

I've fixed my command:
convert spotimage.png finalimage.png -gravity center -composite finalresult.png
(You told me i forgot the -composite).
But it didn't produced the expected result. I'm looking for this:
http://i.imgur.com/TEkkhlO.png
(ignore the fact its resized - the images i'm working with always have the same size).

If you have a clue, tell me.
I've done composition with MagickNET and ImageMagick before, and it worked with no problems. I just adapted the code to fit the current need but it didn't worked as expected.

snibgo
Posts: 9379
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image composition

Post by snibgo » 2017-04-11T15:10:27-07:00

Both of your images are entirely opaque. With no transparency, you will only see the edges of the bottom image where it is larger than the top image.
snibgo's IM pages: im.snibgo.com

pablobhz
Posts: 51
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Image composition

Post by pablobhz » 2017-04-11T15:12:42-07:00

snibgo wrote:
2017-04-11T15:10:27-07:00
Both of your images are entirely opaque. With no transparency, you will only see the edges of the bottom image where it is larger than the top image.
I'm sorry but I don't know that much about images .
I have to add some kind of mask / transpareci before trying to composite , that's it ?

pablobhz
Posts: 51
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Image composition

Post by pablobhz » 2017-04-11T15:38:15-07:00

The opacity level means how much an image is transparent , right ?
So , I must first composite them in some kind of white background (both) and then try to composite them (spot and final)?

snibgo
Posts: 9379
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image composition

Post by snibgo » 2017-04-11T16:59:20-07:00

It's like placing one panting over another one, or a sheet of paper over another one. If the one on top has no transparency, you can't see through it to the one on the bottom.

I suggest you read the usage pages, especially http://www.imagemagick.org/Usage/compose/#over
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22081
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Image composition

Post by fmw42 » 2017-04-11T17:07:09-07:00

You can do a 3 image composite where the last image is grayscale mask. The mask brightness then controls the blending of the two images. Or you can create two masks. One for the first image and the second for the second image. Then put each mask into the alpha channel of the corresponding image using -compose copy_opacity -composite. Then -flatten or -layers merge to put one image over the other having the transparency control the blending. See the link reference by user snibgo and the whole page at http://www.imagemagick.org/Usage/compose/#compose

pablobhz
Posts: 51
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Image composition

Post by pablobhz » 2017-04-11T17:14:24-07:00

As soon I get home ill read and try what was suggested .
Thank you both for the attention and help . I figured out what you guys said , really makes sense . Both images have a white background basically , makes no sense compose one above the other .

Another approach would be copy the non-white pixels from one image to the other . However , I'm having some trouble with PixelCollection on MagickNET (not updating )(I created an topic for that ).

My idea is like :
Load final image and spot image
The images have same width and height so no need for two loops
Check on the spot image if the pixel isn't white .
Copy this exact pixel tongue final image
Or
Set channels on the final image , corresponding to the spot image coordinates , to the desired color .

I don't know if I made myself clear . It's just another approach .

pablobhz
Posts: 51
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Image composition

Post by pablobhz » 2017-04-12T19:57:38-07:00

fmw42 wrote:
2017-04-11T17:07:09-07:00
You can do a 3 image composite where the last image is grayscale mask. The mask brightness then controls the blending of the two images. Or you can create two masks. One for the first image and the second for the second image. Then put each mask into the alpha channel of the corresponding image using -compose copy_opacity -composite. Then -flatten or -layers merge to put one image over the other having the transparency control the blending. See the link reference by user snibgo and the whole page at http://www.imagemagick.org/Usage/compose/#compose
I managed to convert the white pixels from one image to transparent pixels.
But the composition doesn't seem to work still - now it produces an output that is basically an image with black background.
Resulting image:
http://i.imgur.com/pfaLgET.png

new image i'm using to compose (with transparent background):
http://i.imgur.com/Ur8we0z.png

User avatar
fmw42
Posts: 22081
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Image composition

Post by fmw42 » 2017-04-12T23:08:59-07:00

Read my post carefully. If you want to use 3 images for composition, you need to make the third one, the mask, as grayscale not transparent.

If you want to use transparency, then at least one image needs to have an alpha channel and then you do not use the mask. The mask is the image put into one of the image's alpha channel using -compose copy_opacity -composite.

Sorry, I do not use Magick.Net.

If you want us to show you how to do that in command line mode, then provide the two input images and one mask image.

snibgo
Posts: 9379
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image composition

Post by snibgo » 2017-04-13T03:23:24-07:00

The image http://i.imgur.com/Ur8we0z.png is opaque lines in a transparent background. So it can be composited over LpbkJgf.jpg with no mask needed:

Code: Select all

convert LpbkJgf.jpg Ur8we0z.png -composite x.png
snibgo's IM pages: im.snibgo.com

pablobhz
Posts: 51
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Image composition

Post by pablobhz » 2017-04-17T06:50:39-07:00

snibgo wrote:
2017-04-13T03:23:24-07:00
The image http://i.imgur.com/Ur8we0z.png is opaque lines in a transparent background. So it can be composited over LpbkJgf.jpg with no mask needed:

Code: Select all

convert LpbkJgf.jpg Ur8we0z.png -composite x.png
It worked as you said.
However, the composition isn't working as expected. It produces an image with a black background only.

ImageMagick composition worked as expected.

I'm usually doing operations on ImageMagick before doing on MagickNet :)
This way i can see results easier

EDIT: Nevermind about the MagickNET problem i mentioned.
After reading docs and trying a few things, found that i should use the "CompositeOperator.over"

In fact i found an example on MagickNEt docs and tried :)

Thanks in advance for all the help !

Post Reply