Recolor Image and maintain shading

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
nick18
Posts: 10
Joined: 2018-01-12T20:58:36-07:00
Authentication code: 1152

Recolor Image and maintain shading

Post by nick18 »

Hi, i'm using IM version 6.8.9-9 on Ubuntu Linux and am attempting to recolorize this image of a fabric tuft:

Image

By recolorize i mean that i want to change the blue and green areas to a different color but maintain the difference that currently exists (e.g. there are lots of shades of blue, so i want to replace those with the relative shade of my red color which is "rgb(239,70,36)" ).

I do have the option to split the image into separate blue and green versions and process them separately.

I've looked through the IM documentation and this forum extensively and tried everything i can find but just can't seem to get it working right.

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

Re: Recolor Image and maintain shading

Post by fmw42 »

The way to do it is to shift Hues. See my bash unix shell script, replacecolor, at my link below.

Code: Select all

replacecolor -i "rgb(6,16,138)" -o "rgb(239,70,36)" -f 30 -g 100 pile_resized.png pile_resized_blue2red.png
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Recolor Image and maintain shading

Post by fmw42 »

I added an example above with your image. I do not know what you consider the blue input color, so I just measure the blue shade at some pixel in your image. Change the blue color as desired or the red color.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Recolor Image and maintain shading

Post by snibgo »

I would work in HCL (Hue, Chroma, Lightness) colorspace. You want to change hues, without changing chroma or lightness? Yes?

Red is at hue=0 degrees. Purple is at hue=75%. To make all the colour red:

Code: Select all

convert pile_resized.png -colorspace HCL -channel R -evaluate set +channel -colorspace sRGB r.png
Instead, we can make all the colour purple:

Code: Select all

convert pile_resized.png -colorspace HCL -channel R -evaluate set 75% +channel -colorspace sRGB p.png
If we want to change green into red, and blue into purple, we can combine the previous two results with a mask. Notice that you have anti-aliasing where green and blue merge. By inspecting the hue channel, I find that green is about 33% and blue is about 68%. The middle of this is 50.5. 10% on either side is 40.5% to 60.5%. We will create a ramp in this range.

Code: Select all

convert pile_resized.png -colorspace HCL -channel R -separate +channel -level 40.5,60.5% m.png
m.png is a mask, black where we had green (or no colour at all), white where we had blue.

So we combine r.png and p.png with this mask. I also flatten against white and convert to JPG, just for bandwidth speed:

Code: Select all

convert r.png p.png m.png -compose Over -composite -background White -layers Flatten purpred.jpg
All the convert commands can be combined into a single command.

Image
snibgo's IM pages: im.snibgo.com
nick18
Posts: 10
Joined: 2018-01-12T20:58:36-07:00
Authentication code: 1152

Re: Recolor Image and maintain shading

Post by nick18 »

Thank you both so much - this is brilliant!

Just one more aspect if you don't mind, is there a way to 'lighten' the replaced color so it appears closer to the replaced color (rgb(239,70,36))?

Forgive me if this explanation is lacking, but essentially what I am doing is recoloring this image, sizing it down to approx. 25px-60px wide, then repeating it dozens of times to give the impression of a rug. When viewed as a complete image the 'texture' of the individual loops is apparent which makes it look like a rug, but currently the perceived color is darker than the rgb(239,70,36).

I appreciate this is tricky as the blues in the source image are dark, so if I wanted to make this universal (meaning the blue could be replaced with any color, and then when sized down or zoomed out the aggregate result appeared close to the new color) is there a better choice of color for the blue (e.g. changing it to shades or gray), or is there a adjustment that could be made to 'lighten up' the color change to get closer?

Thank you again for your time - I appreciate it so much!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Recolor Image and maintain shading

Post by snibgo »

The method I show keeps the same lightness. However, the finished result might seem lighter or darker, depending on various factors.

My command include a sequence like:

Code: Select all

-colorspace HCL -channel R -evaluate set 75% +channel -colorspace sRGB
This converts to HCL, sets the Hue channel to 75%, and converts back to RGB. It could also adjust lightness, eg to reduce it slightly:

Code: Select all

-colorspace HCL -channel R -evaluate set 75% -channel B -evaluate Multiply 0.9 +channel -colorspace sRGB
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Recolor Image and maintain shading

Post by fmw42 »

In my script, there are arguments for adjusting the lightness and saturation.
nick18
Posts: 10
Joined: 2018-01-12T20:58:36-07:00
Authentication code: 1152

Re: Recolor Image and maintain shading

Post by nick18 »

Thank you both again for your time and help - I ended up changing the image i was using to have more neutral colors, and then added additional code to look at the relative values of the RGB color we were replacing to. Via this i then used a variation of:

Code: Select all

-colorspace HCL -channel R -evaluate set 75% -channel B -evaluate Multiply 0.9 +channel -colorspace sRGB
And the end result was successful!
Post Reply