composite changes image from RGB to Greyscale

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
ketil

composite changes image from RGB to Greyscale

Post by ketil »

Hi,

I'm using composite to overlay a watermark on some images. It's a bunch of images that are later to be reassembled to a video. Anyway, here's what happens:

$ identify output-1.tga
output-1.tga TGA 1024x576 1024x576+0+0 8-bit DirectClass 20.3KiB 0.000u 0:00.010
$ identify scaled-watermark.tga
scaled-watermark.tga TGA 1024x576 1024x576+0+0 8-bit DirectClass 2.25MiB 0.210u 0:00.210
$ composite -dissolve 60 scaled-watermark.tga output-1.tga wmark-output-1.tga
$ identify wmark-output-1.tga
wmark-output-1.tga TGA 1024x576 1024x576+0+0 8-bit PseudoClass 256c 576KiB 0.060u 0:00.070
$ composite -colorspace RGB -dissolve 60 scaled-watermark.tga output-1.tga wmark-output-1.tga
$ identify wmark-output-1.tga
wmark-output-1.tga TGA 1024x576 1024x576+0+0 8-bit PseudoClass 256c 576KiB 0.060u 0:00.060

The GIMP identifies both my source files (output-1.tga and scaled-watermark.tga) as RGB color, and both versions of wmark-output-1.tga as Greyscale. Both input files are probably made up of only grey colors in some cases, but that doesn't mean I want to change the colorspace. And -colorspace RGB (or sRGB) didn't seem to do any good to make it stop.

How can I get composite to stop changing the color space? This is breaking my further use of the images, and it's a pain to have to process each image again if that's my option...

My version of ImageMagick, running on Ubuntu 10.04:

$ composite --version
Version: ImageMagick 6.5.7-8 2009-11-26 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC
Features: OpenMP

Thanks for any help.

Ketil
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: composite changes image from RGB to Greyscale

Post by fmw42 »

composite watermark I believe is meant to use one color image and one grayscale image. see http://www.imagemagick.org/Usage/compose/#watermark and http://www.imagemagick.org/Usage/annota ... mark_image

I think you just want to composite overlay rather than composite watermark. Or better start using convert -compose -composite so that you can do other processing (like fading one image) with parenthesis in the command line. composite is almost deprecated by now.

see

http://www.imagemagick.org/Usage/compose/
http://www.imagemagick.org/Usage/layers/#convert

If you provide links to your input images and what you expect for the output, perhaps someone on the list can show you how to do what you need to do.

try this example and see if it is what you want to do:


convert \( logo: -resize 50% \) \( rose: -alpha set -channel alpha -evaluate set 50% \) \
-gravity center -geometry +40+40 -compose over -composite tmp.png
Last edited by fmw42 on 2010-08-20T10:14:20-07:00, edited 1 time in total.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: composite changes image from RGB to Greyscale

Post by Drarakel »

And: Add "-type TrueColor" before your output filename to always get a full 24bit RGB file in your case. Without such an option, ImageMagick partly tries to store the output file in an 'optimized' format.
ketil

Re: composite changes image from RGB to Greyscale

Post by ketil »

Thanks!

Drarakel, I really thought I had the solution there, with your "-type TrueColor" before the output image. Worked great in my initial tests. However, that option also changes the way the input watermark is read. Using that makes the output image a lot darker than the input image, because it seems to cause the input watermark, which is greyscale with transparency, to be interpreted as black where it is transparent, and too bright where it was grey. That took me a while to notice.

fmw42, I'll look a bit more into your suggestion, and see if I can work that stuff out.

Cheers, Ketil
ketil

Re: composite changes image from RGB to Greyscale

Post by ketil »

fmw42, I can't confirm that composite is "almost deprecated". From one of the links you sent:
There are of course features in "composite", not available in any form of "convert" image overlaying. See the composite command summary.
So far I'm not able to get convert to do what I want. Using -evaluate set 60% appears to darken the watermark, instead of making it more transparent. Using -alpha set -channel alpha -evaluate set 60% has the same problem as I've had with composite .... -type TrueColor ...., the transparency in the watermark darkens my output image.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: composite changes image from RGB to Greyscale

Post by Drarakel »

As fmw42 wrote - if you can post links to your input files, then some users here might be able to give specific hints regarding the commandlines.
ketil

Re: composite changes image from RGB to Greyscale

Post by ketil »

Here's a sample, would really appreciate if someone could help me out on this one. :)

composite usually works just fine! Here's a typical successful sample:

composite -dissolve 60 sample-watermark.png sample.png output-1.png

Adding the -type TrueColor gives a different result, darkening the resulting image:

composite -dissolve 60 sample-watermark.png sample.png -type TrueColor output-1-tc.png

As you can see, just adding -type TrueColor changes the final image completely.

So, why add -type TrueColor? My images with watermarks are assembled to a movie using ffmpeg. ffmpeg assumes that the colorspace/colordepth is the same for all input images, so when it suddenly changes in an image sequence, it breaks the output horribly. When an input image is completely black, it is probably just the first frame of a color sequence, or a single frame in a sequence, so I need to add the watermark and encode it as black with full 24 bit RGB colors. Preferrably in one step, so that I don't need twice the processing power for the same job.

Here's a sample. I actually use .tga for all I do, because working with lots of temporary images is faster when you don't need to compress/decompress all the time, and I can actually see from the file size how the colors are encoded.

composite -dissolve 60 sample-watermark.png black.png output-black.tga
composite -dissolve 60 sample-watermark.png black.png -type TrueColor output-black-tc.tga

The size of the .tga files incidentally show that the -tc image uses 3 bytes per pixel, while the other one uses 1 byte per pixel (the images are 1024x576):

$ ls -l output-black*tga
-rw-r--r-- 1 ubuntu ubuntu 1769490 2010-08-25 22:19 output-black-tc.tga
-rw-r--r-- 1 ubuntu ubuntu 589842 2010-08-25 22:19 output-black.tga

Too bad they look different... For the record, output-black.tga and output-1.png look right, while output-black-tc.tga and output-1-tc.png look wrong.

What am I missing? :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: composite changes image from RGB to Greyscale

Post by fmw42 »

As I said composite may go away for the next major release of IM (v7). So start using the convert syntax (because it is more flexible and allows parenthesis processing and other things that you cannot do with composite). For example:


convert sample.png sample-watermark.png -compose dissolve \
-set option:compose:args 60 -composite sample_tmp.png

see http://www.imagemagick.org/script/compose.php

Looks fine to me.

What is the issue with your output?

Do you want it to be Directclass or Pseudoclass. It depends upon what image is the source and what is the destination (or foreground background) and how it is written in the command line. IM images will mostly be RGB whether grayscale or not, except for formats that support grayscale. So the colorspace is not what you want to look at. It is the Type (in the -verbose info) or the Class.

Does it matter if Directclass or Pseudoclass as long as it looks right? Or is it a size issue?



identify sample.png
sample.png[0] DirectClassRGB channels=rgb depth=8 min=0 max=1 mean=0.328365 std=0.240429 1406463B bytes

identify sample-watermark.png
sample-watermark.png[0] PseudoClassGrayMatte channels=graya depth=8 min=0 max=1 mean=0.0238138 std=0.152469 6010B bytes

identify sample_tmp.png
sample_tmp.png[0] DirectClassRGB channels=rgb depth=8 min=0 max=1 mean=0.334113 std=0.2408 1400290B bytes

Your two input images are both different. So you cannot have your output being both. You have to choose and preferably it should be the same as the base (background) image and not the watermark image. This is why convert works better as its first image is the background and so the output should have the same characteristics as it rather than the characteristics of the watermark (which is gray with alpha).

So again, please clarify what is wrong or what you desire for your output. Sorry I am missing something here.
ketil

Re: composite changes image from RGB to Greyscale

Post by ketil »

Yep, your command produces exactly what I need - but only for color input images. If you use the black.png input image, and create the output image as black_tmp.tga, your output image will be 589842 bytes. Now, I don't mind that the output is one byte per pixel instead of 3 - but ffmpeg does. So I need a command that creates an output image which is 24 bit RGB colordepth, regardless of what colors are actually used in the image. With your command, the output representation differs depending on the input image.

I guess it's the channel depth that's different - one get's 8 bits for each of R, G and B, while the other gets just 8 bits for gray. This file is output from your command using a color image as input:

Code: Select all

$ identify -verbose sample_tmp.tga
Image: sample_tmp.tga
  Format: TGA (Truevision Targa image)
  Class: DirectClass
  Geometry: 1024x576+0+0
  Resolution: 72x72
  Print size: 14.2222x8
  Units: Undefined
  Type: TrueColor
  Endianess: Undefined
  Colorspace: RGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    red:
      min: 0 (0)
      max: 255 (1)
      mean: 85.1987 (0.334113)
      standard deviation: 61.4037 (0.240799)
      kurtosis: 0.194184
      skewness: 0.931908
    green:
      min: 0 (0)
      max: 255 (1)
      mean: 84.9539 (0.333153)
      standard deviation: 58.433 (0.229149)
      kurtosis: -0.0195667
      skewness: 0.875009
    blue:
      min: 0 (0)
      max: 255 (1)
      mean: 62.7578 (0.246109)
      standard deviation: 62.6226 (0.245579)
      kurtosis: 1.00696
      skewness: 1.36372
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 58.2276 (0.228343)
      standard deviation: 63.1647 (0.247705)
      kurtosis: 0.67333
      skewness: 1.18918
  Rendering intent: Undefined
  Interlace: None
  Background color: white
  Border color: rgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Compose: Over
  Page geometry: 1024x576+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: None
  Orientation: Undefined
  Properties:
    date:create: 2010-08-26T00:15:29+02:00
    date:modify: 2010-08-26T00:15:29+02:00
    signature: 68392852a223ebc89cc3fad81d4dd4cca365bf2272c7d0cda3cc9e157b7d283b
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 1.688MiB
  Number pixels: 576KiB
  Pixels per second: 3.75MiB
  User time: 0.140u
  Elapsed time: 0:01.150
  Version: ImageMagick 6.5.7-8 2009-11-26 Q16 http://www.imagemagick.org
and this is the output image from your command using a black image as input:

Code: Select all

$ identify -verbose black_tmp.tga 
Image: black_tmp.tga
  Format: TGA (Truevision Targa image)
  Class: PseudoClass
  Geometry: 1024x576+0+0
  Resolution: 72x72
  Print size: 14.2222x8
  Units: Undefined
  Type: Grayscale
  Base type: Grayscale
  Endianess: Undefined
  Colorspace: RGB
  Depth: 8-bit
  Channel depth:
    gray: 8-bit
  Channel statistics:
    gray:
      min: 0 (0)
      max: 92 (0.360784)
      mean: 2.07432 (0.0081346)
      standard deviation: 13.5476 (0.0531278)
      kurtosis: 39.4922
      skewness: 6.43071
  Histogram:
    575778: (  0,  0,  0) #000000 black
        68: (  1,  1,  1) #010101 rgb(1,1,1)
        32: (  2,  2,  2) #020202 rgb(2,2,2)
        25: (  3,  3,  3) #030303 grey1
        35: (  4,  4,  4) #040404 rgb(4,4,4)
        29: (  5,  5,  5) #050505 grey2
        14: (  6,  6,  6) #060606 rgb(6,6,6)
        26: (  7,  7,  7) #070707 rgb(7,7,7)
        32: (  8,  8,  8) #080808 grey3
         5: (  9,  9,  9) #090909 rgb(9,9,9)
        22: ( 10, 10, 10) #0A0A0A grey4
        11: ( 11, 11, 11) #0B0B0B rgb(11,11,11)
        13: ( 12, 12, 12) #0C0C0C rgb(12,12,12)
        26: ( 13, 13, 13) #0D0D0D grey5
        19: ( 14, 14, 14) #0E0E0E rgb(14,14,14)
        12: ( 15, 15, 15) #0F0F0F grey6
        11: ( 16, 16, 16) #101010 rgb(16,16,16)
        28: ( 17, 17, 17) #111111 rgb(17,17,17)
         5: ( 18, 18, 18) #121212 grey7
        19: ( 19, 19, 19) #131313 rgb(19,19,19)
        13: ( 20, 20, 20) #141414 grey8
         6: ( 21, 21, 21) #151515 rgb(21,21,21)
        10: ( 22, 22, 22) #161616 rgb(22,22,22)
         9: ( 23, 23, 23) #171717 grey9
         9: ( 24, 24, 24) #181818 rgb(24,24,24)
        12: ( 25, 25, 25) #191919 rgb(25,25,25)
        24: ( 26, 26, 26) #1A1A1A grey10
         1: ( 27, 27, 27) #1B1B1B rgb(27,27,27)
        19: ( 28, 28, 28) #1C1C1C grey11
        11: ( 29, 29, 29) #1D1D1D rgb(29,29,29)
         7: ( 30, 30, 30) #1E1E1E rgb(30,30,30)
        17: ( 31, 31, 31) #1F1F1F grey12
        11: ( 32, 32, 32) #202020 rgb(32,32,32)
         9: ( 33, 33, 33) #212121 grey13
         6: ( 34, 34, 34) #222222 rgb(34,34,34)
        19: ( 35, 35, 35) #232323 rgb(35,35,35)
         5: ( 36, 36, 36) #242424 grey14
        22: ( 37, 37, 37) #252525 rgb(37,37,37)
         7: ( 38, 38, 38) #262626 grey15
        10: ( 39, 39, 39) #272727 rgb(39,39,39)
        10: ( 40, 40, 40) #282828 rgb(40,40,40)
        14: ( 41, 41, 41) #292929 grey16
         5: ( 42, 42, 42) #2A2A2A rgb(42,42,42)
        10: ( 43, 43, 43) #2B2B2B grey17
        20: ( 44, 44, 44) #2C2C2C rgb(44,44,44)
         1: ( 45, 45, 45) #2D2D2D rgb(45,45,45)
        16: ( 46, 46, 46) #2E2E2E grey18
        16: ( 47, 47, 47) #2F2F2F rgb(47,47,47)
         6: ( 48, 48, 48) #303030 grey19
        15: ( 49, 49, 49) #313131 rgb(49,49,49)
        21: ( 50, 50, 50) #323232 rgb(50,50,50)
         9: ( 51, 51, 51) #333333 grey20
        17: ( 52, 52, 52) #343434 rgb(52,52,52)
        19: ( 53, 53, 53) #353535 rgb(53,53,53)
         3: ( 54, 54, 54) #363636 grey21
        14: ( 55, 55, 55) #373737 rgb(55,55,55)
        11: ( 56, 56, 56) #383838 grey22
        15: ( 57, 57, 57) #393939 rgb(57,57,57)
        19: ( 58, 58, 58) #3A3A3A rgb(58,58,58)
        14: ( 59, 59, 59) #3B3B3B grey23
         6: ( 60, 60, 60) #3C3C3C rgb(60,60,60)
        13: ( 61, 61, 61) #3D3D3D grey24
        12: ( 62, 62, 62) #3E3E3E rgb(62,62,62)
         7: ( 63, 63, 63) #3F3F3F rgb(63,63,63)
        17: ( 64, 64, 64) #404040 grey25
        14: ( 65, 65, 65) #414141 rgb(65,65,65)
        13: ( 66, 66, 66) #424242 grey26
         9: ( 67, 67, 67) #434343 rgb(67,67,67)
        13: ( 68, 68, 68) #444444 rgb(68,68,68)
         9: ( 69, 69, 69) #454545 grey27
        17: ( 70, 70, 70) #464646 rgb(70,70,70)
        18: ( 71, 71, 71) #474747 grey28
         3: ( 72, 72, 72) #484848 rgb(72,72,72)
        32: ( 73, 73, 73) #494949 rgb(73,73,73)
        14: ( 74, 74, 74) #4A4A4A grey29
         7: ( 75, 75, 75) #4B4B4B rgb(75,75,75)
        13: ( 76, 76, 76) #4C4C4C rgb(76,76,76)
        17: ( 77, 77, 77) #4D4D4D grey30
        16: ( 78, 78, 78) #4E4E4E rgb(78,78,78)
        19: ( 79, 79, 79) #4F4F4F grey31
        24: ( 80, 80, 80) #505050 rgb(80,80,80)
         6: ( 81, 81, 81) #515151 rgb(81,81,81)
        13: ( 82, 82, 82) #525252 grey32
        27: ( 83, 83, 83) #535353 rgb(83,83,83)
        23: ( 84, 84, 84) #545454 grey33
        33: ( 85, 85, 85) #555555 rgb(85,85,85)
        23: ( 86, 86, 86) #565656 rgb(86,86,86)
        15: ( 87, 87, 87) #575757 grey34
        43: ( 88, 88, 88) #585858 rgb(88,88,88)
        49: ( 89, 89, 89) #595959 grey35
        14: ( 90, 90, 90) #5A5A5A rgb(90,90,90)
        98: ( 91, 91, 91) #5B5B5B rgb(91,91,91)
     12495: ( 92, 92, 92) #5C5C5C grey36
  Colormap: 256
         0: (  0,  0,  0) #000000 black
         1: (  1,  1,  1) #010101 rgb(1,1,1)
         2: (  2,  2,  2) #020202 rgb(2,2,2)
         3: (  3,  3,  3) #030303 grey1
         4: (  4,  4,  4) #040404 rgb(4,4,4)
         5: (  5,  5,  5) #050505 grey2
         6: (  6,  6,  6) #060606 rgb(6,6,6)
         7: (  7,  7,  7) #070707 rgb(7,7,7)
         8: (  8,  8,  8) #080808 grey3
         9: (  9,  9,  9) #090909 rgb(9,9,9)
        10: ( 10, 10, 10) #0A0A0A grey4
....
       250: (250,250,250) #FAFAFA grey98
       251: (251,251,251) #FBFBFB rgb(251,251,251)
       252: (252,252,252) #FCFCFC grey99
       253: (253,253,253) #FDFDFD rgb(253,253,253)
       254: (254,254,254) #FEFEFE rgb(254,254,254)
       255: (255,255,255) #FFFFFF white
  Rendering intent: Undefined
  Interlace: None
  Background color: white
  Border color: rgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Compose: Over
  Page geometry: 1024x576+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: None
  Orientation: Undefined
  Properties:
    date:create: 2010-08-26T00:15:55+02:00
    date:modify: 2010-08-26T00:15:55+02:00
    signature: b5b356cc146e9abf895712a538724926f3c8ad5d9078bb215e134d5502f59472
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 576KiB
  Number pixels: 576KiB
  Pixels per second: 8.036MiB
  User time: 0.070u
  Elapsed time: 0:01.070
  Version: ImageMagick 6.5.7-8 2009-11-26 Q16 http://www.imagemagick.org
So I need the black and white image to be stored with 24 bits per pixel for ffmpeg not to barf, but I haven't managed without changing how it looks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: composite changes image from RGB to Greyscale

Post by fmw42 »

convert sample.png sample-watermark.png -compose dissolve \
-set option:compose:args 60 -composite -depth 8 -type truecolor sample_tmp3.png

(adding -depth 8 as I am on Q16 and -type truecolor does not change the colors)



convert \( -size 1024x576 xc:black \) sample-watermark.png \
-compose dissolve -set option:compose:args 60 -composite \
-depth 8 -type truecolor sample_black_tmp3.png
ids sample_black_tmp3.png
sample_black_tmp3.png[0] DirectClassGray channels=rgb depth=8 min=0 max=0.360784 mean=0.0081346 std=0.0531278 8500B bytes

Seems to work. Is that what you need?

IM 6.6.3.9 Q16 (HDRI) Mac OSX Tiger
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: composite changes image from RGB to Greyscale

Post by Drarakel »

Fred's commandline indeed should work for you.

As to why "-type TrueColor" modified the image in your former commands:
With "convert", the order of the operations is better manageable, and "-type TrueColor" as last 'operation' basically only changes the format of the output file. But with your former "composite" command, "-type TrueColor" was applied to the whole image sequence, and only after that the composition with dissolve was done. The result was that your watermark image lost the alpha channel - thus the final image was also different. So, "-type TrueColorMatte" would have been better - the final image would be ok then (but also with an additional, fully opaque alpha channel). Or, one could use that in order to get a good 24bit output file:
composite -dissolve 60 sample-watermark.png \( black.png -type TrueColor \) output-black-tc.tga
(The parentheses are important here.)
ketil

Re: composite changes image from RGB to Greyscale

Post by ketil »

Thanks for your time, and the detailed explanations!

fmw42's (Fred's?) command line works for me, and I've been using it for a little time now. But it takes about 3 times as long as the old command I used before (about 0.3 seconds CPU time vs 0.1s CPU time), which did all the right stuff but a bit too much optimization. :) Since I'm running thousands of these every day, that matters. I'll try to time your command as well, Drarakel, and see if that is faster (and works as I need).

Cheers!
ketil

Re: composite changes image from RGB to Greyscale

Post by ketil »

OK, here are some timings from my tests:

Code: Select all

composite -dissolve 60 sample-watermark.png \( black.png -type TrueColor \) output-black-tc.tga # approx 0.131 - 0.160s
convert black.png sample-watermark.png -compose dissolve -set option:compose:args 60 -composite -depth 8 -type truecolor sample_black_tmp3.png # approx 0.331 - 0.365s
and they appear to produce equivalent output. So I'll try to switch to the faster one to see how that goes.
Post Reply