Replace specific color of 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?".
mefuzar
Posts: 3
Joined: 2017-10-09T00:53:38-07:00
Authentication code: 1151

Re: Replace specific color of image

Post by mefuzar » 2017-10-09T01:08:10-07:00

Hi,

I am trying to achieve something very similar to the problem described in this post, i.e: change colour of an image with another colour but the problem is that I would like to change it with white.

This is not grayscale as I would like to set specific RGB which will transition to white.

for example, I have an image of 4 pixels:
(0,0,0)=BLACK
(32,0,0)=VERY VERY LIGHT RED
(64,0,0)=VERY LIGHT RED
(128,0,0)=LIGHT RED

so the LIGHT RED will be changed to white (255,255,255) , VERY LIGHT RED to GREY and VERY VERY LIGHT RED to LIGHT GREY.

It is a little bit hard for me to explain, for example, if I have a source picture of a red gradient, starts with BLACK and finishes at (128,0,0)
i will be able to set (128,0,0) as: (255,255,255) and get a BLACK to WHITE gradient output which will match the source.

I have tried: Fred's ImageMagick color2gray but did not know how to use it and was not sure it is the right script to solve this problem.

Thank you very much in advance,
Shuki

User avatar
fmw42
Posts: 22105
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Replace specific color of image

Post by fmw42 » 2017-10-09T09:54:45-07:00

Code: Select all

convert colorimage -colorspace gray grayimage
My script does the same, but allows different types of representation of "gray".

mefuzar
Posts: 3
Joined: 2017-10-09T00:53:38-07:00
Authentication code: 1151

Re: Replace specific color of image

Post by mefuzar » 2017-10-09T10:53:02-07:00

Hi Fred,

Thank you for your answer and your scripts are awesome BTW!!! :D

I have tried the command you have posted, and I can't seem to get the effect I want.

Here is an example, the following image has 4 squares:
RGB = 16,0,0 (left upper)
RGB = 32,0,0 (right upper)
RGB = 64,0,0 (left down)
RGB = 128,0,0 (right down)
Image

After converting using the "convert red.png -colorspace gray gray.png" command, i get the following:
Image

You can clearly see the right down square is not white.

The problem is that I want the grayscale image to be referenced to the maximum red value, i.e: specify somehow to Imagemagick that the red (RGB: 128,0,0) should be: white (RGB: 255,255,255).

I am expecting a result that the right down square will be completely white and the three will be darker compared to it.

Do you know of a way to achieve this task using Imagemagick?

Thank you very much in advance,
Shuki

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

Re: Replace specific color of image

Post by snibgo » 2017-10-09T13:11:13-07:00

mefuzar wrote:It is a little bit hard for me to explain, for example, if I have a source picture of a red gradient, starts with BLACK and finishes at (128,0,0)
i will be able to set (128,0,0) as: (255,255,255) and get a BLACK to WHITE gradient output which will match the source.
Put this another way: make a grayscale image from just the red channel, and auto-level it.

Code: Select all

convert redsq.png -channel Red -separate +channel -auto-level out.png
snibgo's IM pages: im.snibgo.com

mefuzar
Posts: 3
Joined: 2017-10-09T00:53:38-07:00
Authentication code: 1151

Re: Replace specific color of image

Post by mefuzar » 2017-10-09T13:53:21-07:00

This is exactly what I was looking for... thank you so much both of you!!!

Post Reply