Change single color to another in image

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?".
Locked
Ash7890
Posts: 18
Joined: 2020-03-02T23:53:35-07:00
Authentication code: 1152

Change single color to another in image

Post by Ash7890 »

I have an image (alpha.png):
https://drive.google.com/file/d/1yTBRhz ... sp=sharing

As you can see, there are small dots in the image. I believe they are R135 G135 B135. I need to covert them to blue (0,0,255). Could somebody tell me how to do it please?

Below is the command I tried which did not work:
convert alhpa.png -fuzz 0% -fill 'rgb(135,135,135)' -opaque 'rgb(0,0,255)' result.png

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

Re: Change single color to another in image

Post by fmw42 »

You have your colors swapped.

But you need to specify rgba with alpha values, if your image is transparent. Or disable the alpha channel then change colors then enable the alpha channel

Try this (your value is 134 not 135).

Code: Select all

convert alhpa.png -alpha off -fill "rgb(0,0,255)" -opaque "rgb(134,134,134)" -alpha on result.png

Ash7890
Posts: 18
Joined: 2020-03-02T23:53:35-07:00
Authentication code: 1152

Re: Change single color to another in image

Post by Ash7890 »

Thank you for the reply.
It generates a gray image. I want to keep it transparent.

I tried with alpha like below however it seems the image did not change:

Code: Select all

convert alhpa.png -fill "rgba(0,0,255,255)" -opaque "rgba(134,134,134,255)" result.png

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

Re: Change single color to another in image

Post by snibgo »

What version of IM?

Your input is grayscale, so I suggest inserting "-colorspace sRGB" after reading the input.
snibgo's IM pages: im.snibgo.com

Ash7890
Posts: 18
Joined: 2020-03-02T23:53:35-07:00
Authentication code: 1152

Re: Change single color to another in image

Post by Ash7890 »

IM version is 7.0.9-27.
I added "-colorspace sRGB" still no difference.

I think I should explain the details of process. Firstly, I have a raw data (my.raw) here: https://drive.google.com/file/d/1QpHxuQ ... sp=sharing

The result I want is a transparent background with blue dots(135,135,135 parts) of PNG of this raw.
Here is what I did:

1.Conver to PNG:

Code: Select all

convert -size 512x512 -depth 16 gray:my.raw -evaluate AddModulus 50% -auto-level +level 30720x34391 my.png
2.I used PHP to get background(121,121,121) transparent:

Code: Select all

$image = imagecreatefrompng("my.png");
$bgcolor = imagecolorallocatealpha($image, 121, 121, 121, 127);
imagefill($image, 0, 0, $bgcolor);
imagepng($image,"alpha.png");
Edited: *I found IM command to make it transparent so I'm gonna use this:

Code: Select all

convert my.png -fuzz 1% -transparent "rgb(121,121,121)" alpha.png
3.Then make dots blue:

Code: Select all

convert alpha.png -alpha off -fill "rgb(0,0,255)" -opaque "rgb(134,134,134)" -alpha on result.png
Seems it went well till #2 so far.

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

Re: Change single color to another in image

Post by snibgo »

What is your platform: bash, Windows BAT, or whatever? If Windows BAT, you need to double the percent % signs to %%.

As you use IM v7, I suggest you use "magick", not "convert".

Please show the text output from "magick -version". If you use Q16 or larger, the final operation probably needs a fuzz. "magick alpha.png -verbose info:" tells you about that image. For me, this gives:

Code: Select all

  Type: GrayscaleAlpha

  Colors: 3
  Histogram:
    261884: (30720,30720,30720,    0) #7800780078000000 graya(46.8757%,0)
        17: (34377,34377,34377,65535) #864986498649FFFF graya(52.4559%,1)
       243: (34391,34391,34391,65535) #865786578657FFFF graya(52.4773%,1)
Note that none of these pixel values are multiples of 257, so cannot be expressed by "rgb(X,X,X)"

This works fine for me (Windows BAT syntax):

Code: Select all

magick ^
  alpha.png ^
  -colorspace sRGB ^
  -alpha off -fuzz 1%% -fill "rgb(0,0,255)" -opaque "rgb(134,134,134)" -alpha on ^
  result.png
snibgo's IM pages: im.snibgo.com

Ash7890
Posts: 18
Joined: 2020-03-02T23:53:35-07:00
Authentication code: 1152

Re: Change single color to another in image

Post by Ash7890 »

Thank you snibgo. It is working! The image lost transparency after your code, so I put the transparent command at last.

Here is my final code(I'm using Ubuntu):

Code: Select all

// Convert raw to png
magick -size 512x512 -depth 16 gray:my.raw -evaluate AddModulus 50% -auto-level +level 30720x34391 my.png

// Make marker blue
magick my.png -colorspace sRGB -alpha off -fuzz 1% -fill 'rgb(0,0,255)' -opaque 'rgb(134,134,134)' -alpha on my.png

// Transparent
magick my.png -fuzz 1% -transparent 'rgb(121,121,121)' my.png
Edit: Actually, the image is not transparent at the second command, so I removed -alpha states:

Code: Select all

magick my.png -colorspace sRGB -fuzz 1% -fill 'rgb(0,0,255)' -opaque 'rgb(134,134,134)' my.png

Locked