Page 1 of 1

Set background color on composite

Posted: 2013-04-06T12:52:02-07:00
by rogercampos
Hi, I've been trying this for several hours with no success, I hope you can help me with it. I want to cut out some parts of an image based on another mask image (black/white). To do this I use dst_in composition, but my problem is that I cannot control what happens with the removed pixels. They appear in black, I want them to be white. If I set "alpha set" and the destination image to png format, then the removed pixels are transparent, but I need the destination file to be a jpeg. The command I'm using is this (ImageMagick 6.8.0-9):

Code: Select all

composite -compose dst-in /the_mask.png /some_image.jpg result.jpg
Some example images to ilustrate it:

- The original image: Image
- The mask image: Image
- The result image: Image

Re: Set background color on composite

Posted: 2013-04-06T13:26:06-07:00
by snibgo

Code: Select all

convert prova.jpg -alpha on maskcirc.png -compose Dst_In -composite -background Pink -alpha remove out.jpg
JPEGs don't have a transparency channel, so I need to give it one before "-compose Dst_In -composite" will work.

That leaves a transparent area outside the circle. When converted to JPEG, this would turn black, so I use "-background Pink -alpha remove" to turn it pink instead.

I use pink because it's easy to see what is happening when I view the file. You would use "white".

Re: Set background color on composite

Posted: 2013-04-07T18:58:35-07:00
by anthony
Also note that your mask is a boolean mask. that is it purely on/off, with no smoothing anti-aliasing around the edges.

Boolean masks are required for some image formats like GIF, and special forms of PNG, but generally only used for small icon-sized images, not larger images like this.

You can use -draw" to generate a smooth mask to use, so you get a smooth antialiased edge to your masked image.

But as mentioned JPG can not handle transparency, NOR is it recommended for any image except the final product as it uses a lossy compression that WILL DEGRADE THE IMAGE.
http://www.imagemagick.org/Usage/formats/#jpg

Finally after masking.. If you are keeping the transparency (by saving to say PNG) I suggest you also include

Code: Select all

-background black -alpha background
before saving. This should not effect the look of the PNG image, but will make the saved file a lot smaller by setting all transparent pixels to fully-transparent black.

See IM Examples, Alpha Background for details
http://www.imagemagick.org/Usage/maskin ... background

Re: Set background color on composite

Posted: 2013-04-09T02:45:59-07:00
by rogercampos
Solved now, thank you all for your help and advices!

Re: Set background color on composite

Posted: 2013-04-16T00:43:12-07:00
by rogercampos
Hi again, after been trying the proposed solution for a while I find now that the

Code: Select all

-alpha remove
directive has some important gotchas, it doesn't work on CMYK images, or maybe I'm doing something wrong.

The complete command I'm using is this one:

Code: Select all

convert input_image.jpg -alpha on mask.png -compose Dst_In -composite -background White -alpha remove out.jpg
While this works on other images, look what happens when used with this image here, which is CMYK with a japan coated attached profile:

Image

The result is this, with completely converted colors:

Image

However, when we strip the

Code: Select all

-alpha remove
part the generated images looks like it's supposed to, this one: (apart from the background black color, which I wanted to be white):

Image

Am I doing something wrong?

Re: Set background color on composite

Posted: 2013-04-16T08:17:08-07:00
by snibgo
You need to convert the CMYK to sRGB:

Code: Select all

convert test5.jpg -colorspace sRGB -alpha on mask.png -compose Dst_In -composite -background White -alpha remove out.jpg