Set background color on composite

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
rogercampos
Posts: 6
Joined: 2012-08-26T13:00:20-07:00
Authentication code: 67789

Set background color on composite

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Set background color on composite

Post 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".
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Set background color on composite

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
rogercampos
Posts: 6
Joined: 2012-08-26T13:00:20-07:00
Authentication code: 67789

Re: Set background color on composite

Post by rogercampos »

Solved now, thank you all for your help and advices!
rogercampos
Posts: 6
Joined: 2012-08-26T13:00:20-07:00
Authentication code: 67789

Re: Set background color on composite

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Set background color on composite

Post 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
snibgo's IM pages: im.snibgo.com
Post Reply