Colour replacement in grey images

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
robertf
Posts: 2
Joined: 2012-09-09T15:53:08-07:00
Authentication code: 67789

Colour replacement in grey images

Post by robertf »

I am trying to replace a particular grey value in a grey-level image with an rgb colour using the command

Code: Select all

convert xz59.tif -opaque "rgb(48,48,48)" -fill blue -colorspace RGB -type TrueColor xz59_blue.tif
The result is an RGB image in which the pixels with the specified grey level (48) have been replaced by white rather than by blue. The input image is RGB but contains only greys. (I don't know if the -colorspace and -type options should be required, they're the result of much experimentation that produced greyscale images when I was using PNG's. And I know that I could use "gray(48)" rather than "rgb(48,48,48)".)

I'm using version 6.6.0-4 under Linux and both 6.6.7-6-Q16-windows-x64-dll and 6.7.9-4-Q16-windows-x64-dll under Windows 7.

Can anyone tell me what silly mistake I'm making? Thank you.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colour replacement in grey images

Post by fmw42 »

convert xz59.tif -opaque "rgb(48,48,48)" -colorspace RGB -type TrueColor xz59_blue.tif
put -fill before -opaque

use gray(48), though rgb(48,48,48) will work unless your image has transparency. Then use graya or rgba.

convert xz59.tif -fill blue -opaque "rgb(48,48,48)" -type truecolor xz59_blue.tif

you should not need to add -colorspace as the output will be sRGB, when you specify -fill blue.

You may want to post a link to your input image for us to test, if the above does not help. Are you sure your input image has exactly graylevel=48 in 8-bit equivalent? If not exactly that value, then you may need to add -fuzz XX% before -fill.
robertf
Posts: 2
Joined: 2012-09-09T15:53:08-07:00
Authentication code: 67789

Re: Colour replacement in grey images

Post by robertf »

Thank you! Putting -fill before -opaque worked like a charm. I should have thought to try that. I guess the idea is that the -fill with a non-grey colour warns IM to immediately start thinking of the image as not just grey? Should I have been able to deduce this from the documentation?

Adding '-colorspace rgb' not only is unnecessary but actually causes a problem, making all of the grey levels darker for some reason.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colour replacement in grey images

Post by fmw42 »

Should I have been able to deduce this from the documentation?

Adding '-colorspace rgb' not only is unnecessary but actually causes a problem, making all of the grey levels darker for some reason.
See
http://www.imagemagick.org/Usage/basics/#syntax
http://www.imagemagick.org/Usage/reference.html

Basically settings should come before the operator that uses them. -fill is just a setting and -opaque is the operator or command.

In current versions of IM -colorspace RGB and sRGB have been swapped to apply the true meaning and conform to current standards. After IM 6.7.5.5, -colorspace RGB became linear RGB and sRGB became non-linear RGB. So you have probably converted a sRGB result to linear RGB by adding -colorspace RGB. After IM 6.7.8.3, grayscale images were treated as linear and not non-linear. Any of these could explain your issue. More proper color management is now needed to get results to look like they used to look.
Post Reply