Combining Channel Images with custom colors

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
mogadanez
Posts: 6
Joined: 2014-07-14T05:48:37-07:00
Authentication code: 6789

Combining Channel Images with custom colors

Post by mogadanez »

I have set of grayscale ( negated?? see command bellow ) images, img_0 .... img_7
where first 4 is CMYK channels, and rest is custom spot colors channels.

I can combine CMYK channels with

Code: Select all

convert img_?.png  -set colorspace CMYK  -negate -combine  -colorspace RGB  result.png
it requires -negate option, without it result is very black.

I need 2 things:
1. Somehow split this command to separate CMYK channels to able combine them in different combinations

i'm trying follow command

Code: Select all

convert img_0.png -negate -colorspace CMYK img_0.png -compose CopyCyan  -composite img_1.png -compose CopyMagenta  -composite empty.png -compose CopyYellow   -negate  -composite -colorspace RGB   _result.png
it is more or less works, but there is 2 problems:
* need specify empty.png for skipped channel
* cant add black channel in such manner, result becomes black.
maybe more elegant solution present?

2. add to compose rest of channels with custom colors where colors defined as cmyk or rgb values.
here I have no ideas at all.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining Channel Images with custom colors

Post by snibgo »

A sample set of input images would help. Also say what the spot colours are. Preferably also an sRGB version of what the complete image should look like.

EDIT: In your commands, "-colorspace sRGB" would be better than "-colorspace RGB".
snibgo's IM pages: im.snibgo.com
mogadanez
Posts: 6
Joined: 2014-07-14T05:48:37-07:00
Authentication code: 6789

Re: Combining Channel Images with custom colors

Post by mogadanez »

image set: https://www.dropbox.com/sh/984qvm7z2qrw ... iDxza?dl=0
img_x.png - sources
spotcolors - result I want to achieve when all channels is enabled, or combination of boxes when only part of channels used.

last example, worked more or less fine with basic CMYK boxes:

Code: Select all

convert img_3.png -negate -colorspace CMYK \  
img_0.png -compose CopyCyan  -composite \
img_1.png -compose CopyMagenta  -composite \
empty.png -compose CopyYellow  -composite \
-negate -colorspace sRGB   _result.png
I put black layer as first image, and use empty( white image ) to toggle out layers

spot colors can be any, initially it is usually PANTONE colors, but I have CMYK or RGB analogs for each one, that is fine for me.
I understand that it not exact as original, but in my case it is fine.
so lets say that
img_4 => rgb(0, 94, 79)
img_4 => rgb(97, 96, 167);
img_4 => rgb(229, 76, 60)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining Channel Images with custom colors

Post by snibgo »

Okay, good. The file "spotcolors.png" doesn't contain pure cyan, magenta, yellow or even black. It doesn't even have pure white. So I suppose you want those colours in the output, instead of the pure colours. Is that correct? If it is correct then a simple sRGB->CMYK conversion won't work. You really have seven spot colours, four being roughly (but not exactly) cyan, magenta, yellow and black. A script would need to build the output using values from spotcolors.png.

The input files img_*.png contain only black and white, with no grays. Will this always be true?
img_4 => rgb(0, 94, 79)
img_4 => rgb(97, 96, 167);
img_4 => rgb(229, 76, 60)
I don't understand this. I suppose you mean images 4, 5 and 6, but the rgb values are not in the file (as reported by Gimp).
snibgo's IM pages: im.snibgo.com
mogadanez
Posts: 6
Joined: 2014-07-14T05:48:37-07:00
Authentication code: 6789

Re: Combining Channel Images with custom colors

Post by mogadanez »

https://www.dropbox.com/s/ll43uiikkmf34 ... 1.pdf?dl=0
this is source pdf. I can export separations as gray-scales.
now I need to make preview like in acrobat preview based on this grayscales
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining Channel Images with custom colors

Post by snibgo »

Okay. I'll assume you want pure white, cyan, magenta, yellow and black, but the bottom three should be spotcolours I've taken from "convert spotcolors.pdf sc.png":

rgb(100%,12%,9%)
rgb(7% 27% 11%)
rgb(15% 20% 100%)

I assume the inputs are black where you want the colour, white otherwise, with no gray. Windows BAT syntax:

Code: Select all

%IM%convert ^
  img_0.png img_1.png img_2.png  ^
  -combine ^
  img_3.png ^
  ( img_6.png -fill rgb(100%%,12%%,9%%) -opaque Black ) ^
  ( img_4.png -fill rgb(7%%,27%%,11%%) -opaque Black ) ^
  ( img_5.png -fill rgb(15%%,20%%,100%%) -opaque Black ) ^
  -compose Darken -layers merge ^
  spotcolors_out.png
Image
snibgo's IM pages: im.snibgo.com
mogadanez
Posts: 6
Joined: 2014-07-14T05:48:37-07:00
Authentication code: 6789

Re: Combining Channel Images with custom colors

Post by mogadanez »

Nice, thank you,
last question - how to turn out single layers?
with spot colors its pretty easy - just removing it's line.
but how to turn out single color from base CMYK ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining Channel Images with custom colors

Post by snibgo »

At each pixel, when colors overlap, only the darker color is used, so spot colors lighter than magenta won't overwrite magenta. If you want to ensure that later colors in the command overwrite earlier colors, use " -transparent White -compose Over -layers merge" instead of "-compose Darken -layers merge".

In my command, black acts like a spot color, overwriting any C M or Y values in the pixel.

If you don't want black, just remove img_3.png from the command.

If you don't want C or M or Y, you need to replace it with a white image of the same size. For example, this removes the magenta layer:

Code: Select all

%IM%convert ^
  img_0.png ^
  ( img_1.png -fill White -colorize 100 ) ^
  img_2.png  ^
  -combine ^
  img_3.png ^
  ( img_6.png -fill rgb(100%%,12%%,9%%) -opaque Black ) ^
  ( img_4.png -fill rgb(7%%,27%%,11%%) -opaque Black ) ^
  ( img_5.png -fill rgb(15%%,20%%,100%%) -opaque Black ) ^
  -transparent White ^
  -compose Over -layers merge ^
  spotcolors_out.png
snibgo's IM pages: im.snibgo.com
mogadanez
Posts: 6
Joined: 2014-07-14T05:48:37-07:00
Authentication code: 6789

Re: Combining Channel Images with custom colors

Post by mogadanez »

It works on previous sample but not for more complex images.
see:
https://www.dropbox.com/sh/fh2i1vu4604l ... SBkia?dl=0

i use command

Code: Select all

convert ^
  -size 512x512 xc:white ^
  -size 512x512 xc:white ^
  -size 512x512 xc:white ^
  -combine ^
( img1.png -fill rgb(0,175,235) -opaque Black ) ^
( img2.png -fill rgb(236,40,144) -opaque Black ) ^
-compose Darken -layers merge ^
out.png
and get
Image

but i'm expect( aprox, another crop, see on colors only ):
Image


any suggestions?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining Channel Images with custom colors

Post by snibgo »

Upthread I said:
snibgo wrote:The input files img_*.png contain only black and white, with no grays. Will this always be true?

I assume the inputs are black where you want the colour, white otherwise, with no gray.
But your inputs have gray, so we need "+level-colors". As you have no pure cyan, yellow or magenta, the first few lines are not needed.

Code: Select all

convert ^
  ( img1.png +level-colors rgb(0,175,235),White ) ^
  ( img2.png +level-colors rgb(236,40,144),White ) ^
  -compose Darken -layers merge ^
  im2_out.png
Image
snibgo's IM pages: im.snibgo.com
peter01242
Posts: 17
Joined: 2016-06-22T11:32:57-07:00
Authentication code: 1151

Re: Combining Channel Images with custom colors

Post by peter01242 »

I am trying to combine CMYK and a Pantone(TM) spot color into a composite PNG image as part of a project I am working on for my employers.

I am using the following command-line script although I intend to port it to Magick++ in a standalone utility once I have the behavior sorted:

Code: Select all

convert -set colorspace CMYK "spot(Cyan).png" "spot(Magenta).png" "spot(Yellow).png" "spot(Black).png" -negate -combine ^
-colorspace CMYK ^
-compose add ^
( "spot(PANTONE 282 U).png" -negate -opaque cmyk(255,173,0,138) -transparent cmyk(0,0,0,0) ) -composite ^
-layers merge ^
"composite.png"
This is pretty close to what I need, but the spot color is always appearing as black instead of the color I specify (a royal blue) for the -opaque command.
I have tried using the -level-colors and +level-colors options but have yet to find a combination that produces the correct color in the final image.

Here is a link to a DB folder containing the source PNG images. These were resampled from the original 800 dpi TIFF images at 300 dpi.

https://www.dropbox.com/sh/oi5x2kidlzvj ... EhM7a?dl=0

Any suggestions greatfully received.
Thanks,

Pete Y
Xitron LLC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining Channel Images with custom colors

Post by snibgo »

If I format your command like this:

Code: Select all

%IM%convert ^
  -set colorspace CMYK ^
  "spot(Cyan).png" "spot(Magenta).png" "spot(Yellow).png" "spot(Black).png" ^
  -negate -combine ^
  -colorspace CMYK ^
+write x.tiff ^
  -compose add ^
  ( "spot(PANTONE 282 U).png" -negate -opaque cmyk(255,173,0,138) -transparent cmyk(0,0,0,0) ^
+write x2.tiff ^
  ) ^
  -composite ^
  -layers merge ^
  "composite.png"
x.tiff is a CMYK file, and it looks fine. I don't understand what you want to do with "spot(PANTONE 282 U).png". This image is mostly white, with some black at bottom-left. It doesn't contain any colour "cmyk(255,173,0,138)". The result, x2.tiff, is sRGB (not CMYK), with some opaque white but mostly transparent black.

You then composite this sRGB image on the CMYK image, with the "add" method.

Composing images of different colourspaces is a bad idea. I suggest you make them both sRGB or both CMYK.

Note the output is type .PNG. This can't store CMYK, so IM will convert the image to sRGB.
snibgo's IM pages: im.snibgo.com
peter01242
Posts: 17
Joined: 2016-06-22T11:32:57-07:00
Authentication code: 1151

Re: Combining Channel Images with custom colors

Post by peter01242 »

Thanks for the quick response.
The 5 source PNGs are the individual colors for the final image (there are actually 6) but I decided to get 1 working then add the other after.
The source PDF is now in the DB folder as well for reference.
The original source files are 1-bit TIFF and I subsampled them to PNG using:

Code: Select all

mogrify -format png -resample 300 -depth 8 spot*.tif
None of the PNGs contain any 'color' - they are all 8-bit grayscale.
The effect I am trying to achieve is that the CMYK combine to form the base 32-bit image and then I composite on top of that the spot color replacing 255 across the CMYK planes with cmyk(255,173,0,138) and 0 with cmyk(0,0,0,0) or even better Transparent or None.

Code: Select all

convert -set colorspace CMYK "spot(Cyan).png" "spot(Magenta).png" "spot(Yellow).png" "spot(Black).png" -negate -depth 8 -combine ^
-colorspace CMYK ^
-compose add ^
( "spot(PANTONE 282 U).png" -negate +level-colors Transparent,cmyk(255,173,0,138) ) -composite ^
( "spot(PANTONE 429 U).png" -negate -level-colors Transparent,cmyk(8,0,0,82) ) -composite ^
-layers flatten ^
"composite.png"
This gives me a result closer to that required but the spot colors are not correct.
In the DB folder is a Color Comparison.PNG: http://www.dropbox.com/s/6o20g36uu6rouz ... n.PNG?dl=0

On the left is the source PDF as seen in Acrobat Reader - note the light grey and dark blue colors at the bottom.
On the right is the result of the above code - note the black and bright blue colors at the bottom.
The light grey equates to the Pantone 429 and the dark blue is the Pantone 282.
The CMYK values (255,173,0,138) and (8,0,0,82) when viewed in a Color Tool show the appropriate colors (light gray and dark blue), so I can only assume that there is some 'magic' formula I am missing in the code above that is causing IM to generate incorrect colors.

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

Re: Combining Channel Images with custom colors

Post by snibgo »

So, where "spot(PANTONE 282 U).png" is black you want the output to be over-written with blue, is that correct?

"-compose Add" adds pixels values, but you want to replace them, so "-compose Over". Your "-layers flatten" does nothing.

I think this does what I think you want:

Code: Select all

%IM%convert ^
  -set colorspace CMYK ^
  "spot(Cyan).png" "spot(Magenta).png" "spot(Yellow).png" "spot(Black).png" ^
  -negate -combine ^
  -colorspace CMYK ^
+write x.tiff ^
  ( "spot(PANTONE 282 U).png" ^
    -transparent White ^
    -set colorspace CMYK ^
    -fill cmyk(255,173,0,138) -opaque cmyk(0,0,0,0) ^
+write x2.tiff ^
  ) ^
  -compose Over -composite ^
  "c2.png"
When this does what you want, the two "+write" commands can be removed.
snibgo's IM pages: im.snibgo.com
peter01242
Posts: 17
Joined: 2016-06-22T11:32:57-07:00
Authentication code: 1151

Re: Combining Channel Images with custom colors

Post by peter01242 »

Thanks for all the help, I think I have a handle on it now.
Here is what I am using:

Code: Select all

%IM%convert ^
  -set colorspace CMYK ^
  "spot(Cyan).png" "spot(Magenta).png" "spot(Yellow).png" "spot(Black).png" ^
  -negate -combine ^
  -colorspace CMYK ^
  -compose Add ^
  ( "spot(PANTONE 282 U).png" -transparent White -set colorspace CMYK -fill cmyk(255,173,0,138) -opaque cmyk(0,0,0,0) ) -composite ^
  ( "spot(PANTONE 429 U).png" -transparent White -set colorspace CMYK -fill cmyk(8,0,0,82) -opaque cmyk(0,0,0,0)  ) -composite ^
  "composite.png"
  
There is a Color Comparison 2.PNG in the DB folder that shows how close I can get.
http://www.dropbox.com/s/0a2k7szgmx4ec ... .PNG?dl=0
This is for thumbnail generation so exact matching is not a required.

Thanks again,
Pete Y
Post Reply