Change color of images without changing the texture

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Son13
Posts: 12
Joined: 2016-04-18T13:04:23-07:00
Authentication code: 1151

Change color of images without changing the texture

Post by Son13 »

May be this can be a simple thing for you experts hence reaching out.

I have a color chart with different texture formats as referred http://beckerlelumber.com/zar.htm
Is it possible to change the color for each of these sample to one color (say, RED) without changing the texture. Again, the same shade of RED should be applied to each of this chart except preserving the texture.

It will be good to have a BATCH command where one sample from the chart is passed as an input image and changed it to RED. Thanks for your time!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change color of images without changing the texture

Post by fmw42 »

Please always provide your IM version and Platform, since syntax may differ. Are you trying to simple add some red or convert the whole pattern to red on top of the texture. If the latter, then

Code: Select all

convert image -colorspace gray +level-colors black,red result
or

Code: Select all

convert image -colorspace gray +level-colors red,white result
Are you trying to convert this whole image or just each texture pattern in the image?

Please be more specific about what you want to do or provide an input and output example.
Son13
Posts: 12
Joined: 2016-04-18T13:04:23-07:00
Authentication code: 1151

Re: Change color of images without changing the texture

Post by Son13 »

Hi fmw42:
Thanks for your quick response. I have tried these commands earlier and rechecked again... the resultant images color don't get the same RED color even though the texture is maintained.
One texture image is passed at a time.

Note: I have PNG images and not sure if that makes a difference. The darker colors images result into darker shade of RED and lighter color images result into lighted shade of RED and this is what I am trying to overcome. It will be great to have same SHADE of RED but it also preserves the texture.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change color of images without changing the texture

Post by fmw42 »

I think you may need to separate each texture pattern into its own image, then perhaps do something like (untested):

Code: Select all

convert texture -colorspace gray -equalize +level-colors black,red result
or

Unix syntax:

Code: Select all

convert \( texture -colorspace gray -equalize \) \( -clone 0 -fill red -colorize 100 \) -compose multiply -composite result
or

Code: Select all

convert \( texture -colorspace gray -equalize \) \( -clone 0 -fill red -colorize 100 \) \( -clone 0 \) -compose multiply -composite result
For -equalize to work effectively to "equalize" each texture, it needs to process each texture separately.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change color of images without changing the texture

Post by fmw42 »

The other thing you could do besides -equalize, would be to convert each image to the same brightness and contrast using the mean and std statistics. See snibgo's web site for details at http://im.snibgo.com/gainbias.htm
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change color of images without changing the texture

Post by snibgo »

There are many methods to re-colour an image while keeping the texture. In terms of HSL, perhaps you want a constant hue but varying lightness and saturation. If so, just convert to HSL, set the hue, and convert back.

Or you could convert it to grayscale, then "-tint" or "-colorize" it.

Or you could make a Gaussian pyramid, recolour the top layer with your chosen colour, and collapse it.

Without understanding your goals, advice is difficult.
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: Change color of images without changing the texture

Post by fmw42 »

I think he wants to "equalize" the light/dark striping of each texture to the same brightness/contrast before coloring it.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change color of images without changing the texture

Post by snibgo »

Quite possibly. Or perhaps he wants the samples to have the same average intensity while retaining different contrasts. "-auto-gamma" will do that.
snibgo's IM pages: im.snibgo.com
Son13
Posts: 12
Joined: 2016-04-18T13:04:23-07:00
Authentication code: 1151

Re: Change color of images without changing the texture

Post by Son13 »

Hi FNW42 & Snibgo:
Thanks for the options that you guys have provided.

Just to restate the goal...
1. Its a web application and the user is allowed to select couple of images (with varied range of color & texture http://beckerlelumber.com/zar.htm)
2. User select the new color, say RED
3. The updated two images are compare - The problem I am currently facing is the darker colors images(No 4 http://beckerlelumber.com/zar.htm) result into darker shade of RED and lighter color images(No 8 http://beckerlelumber.com/zar.htm) result into lighted shade of RED.
I would like to have the same SHADE of RED for both darker and lighter input images..
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change color of images without changing the texture

Post by snibgo »

So, "-equalize" or "-auto-gamma".

But you'll have to do this for each image separately. You can't do it to one image that contains multiple samples. And each image must contain only the sample, without any borders.
snibgo's IM pages: im.snibgo.com
Son13
Posts: 12
Joined: 2016-04-18T13:04:23-07:00
Authentication code: 1151

Re: Change color of images without changing the texture

Post by Son13 »

As directed, took one image separately and tried the below commands with -equalize & -auto-gamma. The resultant images lose their texture even though the color is the same.
convert 4.png +level-colors #FF0000 -equalize 41.png
convert 4.png +level-colors #FF0000 -auto-gamma 41.png

With this command, the texture is maintained perfectly but the color is not the same.
convert 4.png +level-colors #FF0000, white -equalize 41.png
convert 4.png +level-colors #FF0000, white -auto-gamma 41.png

Please suggest for any changes. Thanks again!
Last edited by Son13 on 2016-04-18T21:25:39-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change color of images without changing the texture

Post by fmw42 »

Please provide the 4.png image and others that you want to make the same red. We need to be able to reproduce your results and see what you have done. You can post images to some free hosting service such as dropbox.com and put the URLs here.
Son13
Posts: 12
Joined: 2016-04-18T13:04:23-07:00
Authentication code: 1151

Re: Change color of images without changing the texture

Post by Son13 »

Here, are the sample images:
Set 1:
https://www.dropbox.com/s/vgqvgfh4oi6eci9/4.png?dl=0
https://www.dropbox.com/s/3gbc3xcjtuav5lh/8.png?dl=0

Set 2: Different sample with white background, using (convert 3.png +level-colors #FF0000, white -equalize 31.png)
https://www.dropbox.com/s/mzd66hscv4njg8d/3.png?dl=0
https://www.dropbox.com/s/0finrl3539i7zwd/2.png?dl=0
Last edited by Son13 on 2016-04-19T06:58:57-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change color of images without changing the texture

Post by fmw42 »

4.png and 41.png are quite different. The latter is completely red. So I am confused. If you want two different textures to be the same color, then provide two different texture images, so we can test methods.

On your wig images, you will need to make the background transparent or the white will change. You probably want to use two different shades of red in via +level-colors or via a clut with two or more shades of red. See http://www.imagemagick.org/Usage/color_mods/#clut and http://www.imagemagick.org/Usage/color_mods/#duotone
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change color of images without changing the texture

Post by snibgo »

Code: Select all

convert 4.png -colorspace gray -auto-level +level-colors red,white wood1.png
Image

Code: Select all

convert 4.png -colorspace gray -auto-level +level-colors black,red wood2.png
Image

I don't know what effect you are aiming at.
snibgo's IM pages: im.snibgo.com
Post Reply