Adjusting color of individual RGB channels

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
tommyfrank
Posts: 3
Joined: 2018-12-21T21:21:58-07:00
Authentication code: 1152

Adjusting color of individual RGB channels

Post by tommyfrank »

Hello,

I’m a weaver and textile maker with no prior coding or programming experience. In researching and reading through posts on this and other message boards, I was able to find a lot of useful information, but nothing that I could clearly map on what it is I’m trying to do. I’m hoping someone can steer me in the right direction.

I’m working with a set of 18x18 (324px), 8 bit RGB color PNGs which will ultimately be converted into weave structures. I am interested in implementing a script which will allow me to change the value of all pixels of a particular color channel. I’m seeing a number of posts describing how to change the color values of specific pixels, but little about making global changes to the color channels themselves.

I want to be able to manipulate each color channel separately. For example, I'd like to be able to edit all RED pixels in an image such that all R pixel values increase by, say, 2, while pixels containing no R remain unchanged. Additionally, I want to be able to specify a limit, e.g. if the maximum R value in the image is 178, all R pixel values equaling 178 will remain unchanged, while all pixels with R values less than 178 will increase in value by 2. 

I hope to be able to specify the channel (e.g. RED), amount of change (e.g. 2, 1, etc.), type of change (e.g. increase/decrease), and limit (e.g. n = 178, n = 0, n = 255, etc.) as those variables will need to be adjusted to satisfy different color palettes and weave structures.
 
I've tried achieving this in Photoshop and other graphics editors, but outside of selecting like pixels and adjusting their values manually, I haven't been able to find an effective solution for this.
 
I’m wondering 1) how feasible this is, 2) what programming language (C++, Python, Java, etc) might provide the most straightforward, expedient solution and 3) if anybody has any tips about how go about writing a script like this, as this is all quite new to me.

Thanks in advance for your help,
TF
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adjusting color of individual RGB channels

Post by fmw42 »

First you need to clarify whether you want to change red pixels or the red channel. You first talk about editing all red pixel by increasing their value, but what do you mean by red? All red hues, only the red channel values, pixels with red values not equal to 0 in rgb specifications.

It is easy to change all pixels in a channel in Imagemagick. You can also change hue values. You can also edit pixels that have a range of red values.

It is not clear to me what you want.

Lets take you comment:
I hope to be able to specify the channel (e.g. RED), amount of change (e.g. 2, 1, etc.), type of change (e.g. increase/decrease), and limit (e.g. n = 178, n = 0, n = 255, etc.) as those variables will need to be adjusted to satisfy different color palettes and weave structures
I am not clear what you are asking. It is easy to add 2 units of value to the red channel. You can do that for all pixels below 178. But I am not sure what you want to do with n=0, n=255 white still changing n=178 presumably channel values. Please clarify if you can.

If you want to specify how each value should change, you can make a look up table image and apply that to your image to make changes to each value in the red channel.

Imagemagick is quite flexible.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adjusting color of individual RGB channels

Post by snibgo »

What version of IM, on what platform? I'll assume v6 Q8, in Windows BAT scripts.

I'm not sure what the question is, but I think the answer is "-fx".

Just for demonstration, I'll use an image with one pixel, and I'll write it in "txt:" format so we can see the numbers.

Code: Select all

convert xc:srgb(10,20,30) -channel Red -fx "u+2/255" +channel txt:
# ImageMagick pixel enumeration: 1,1,255,srgb
0,0: (12,20,30)  #0C141E  srgb(12,20,30)
The input has values 10,20,30 out of 255. For the red channel, we added 2/255, so the result for that channel is 12 out of 255.

We can make the "-fx" expression a conditional: when the red channel is less than 178/255, add 2/255, otherwise leave it alone.

Code: Select all

convert xc:srgb(10,20,30) -channel Red -fx "u<178/255?u+2/255:u" +channel txt:
# ImageMagick pixel enumeration: 1,1,255,srgb
0,0: (12,20,30)  #0C141E  srgb(12,20,30)

convert xc:srgb(180,20,30) -channel Red -fx "u<178/255?u+2/255:u" +channel txt:
# ImageMagick pixel enumeration: 1,1,255,srgb
0,0: (180,20,30)  #B4141E  srgb(180,20,30)
snibgo's IM pages: im.snibgo.com
tommyfrank
Posts: 3
Joined: 2018-12-21T21:21:58-07:00
Authentication code: 1152

Re: Adjusting color of individual RGB channels

Post by tommyfrank »

I apologize if there was a confusion of terms. I'd hoped to keep the language clear, yet unspecific enough to encourage different types of suggestions. My post was asking how I might go about editing all red pixels ≥ 1 but < 178, such that every pixel with a red value falling within this range is increased +2, e.g. 0,0: (4,12,51) and 0,1: (7,20,51) would be converted to 0,0: (6,12,51) and 0,1: (9,20,51).

@snibgo - OSX 10.11.6, ImageMagick 6.9.9-40 Q16 x86_64 2018-12-27; the "-fx" command you included seems to achieve the outcome specified above. Thank you very much.

None of the images I'm working with have, or will need to have, an RGB value = 255.

@fmw42 - You bring up something which didn't occur to me at the outset. Pixels with R=0, e.g. 1,0 (0,19,51) aren't converted with a command specifying the R channel. How might I amend or add to the command above such that R=0 pixels are converted as well, e.g. 1,0 (0,19,51) converted to 1,0 (2,19,51)?

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

Re: Adjusting color of individual RGB channels

Post by fmw42 »

@fmw42 - You bring up something which didn't occur to me at the outset. Pixels with R=0, e.g. 1,0 (0,19,51) aren't converted with a command specifying the R channel. How might I amend or add to the command above such that R=0 pixels are converted as well, e.g. 1,0 (0,19,51) converted to 1,0 (2,19,51)?
I am not sure of your terminology? Colors are not expressed as 1,0 (2,19,51). Please clarify.

If you want to increase all pixels containing a red value of 1 to 177 by 2, those values would go to 3 to 179. That could be done using -fx operating on the red channel, if you do not care about the values in the green and blue channels.

I do not understand the reference to changing R=0. Do you mean 0 to 177 going to 2 to 179?

You mention (0,19,51). Does that mean you only want to change values (0,19,51) to (177,19,51) to increase the red by 2 or do you care what the green and blue values are?
tommyfrank
Posts: 3
Joined: 2018-12-21T21:21:58-07:00
Authentication code: 1152

Re: Adjusting color of individual RGB channels

Post by tommyfrank »

I was identifying the coordinates of several pixels in some hypothetical image, along with their corresponding RGB values, e.g. P1,0: (R0,G19,B51). This seemed intuitive to me, but maybe there are standard ways of notating these things?

Regardless, my previous post is irrelevant; I misunderstood what the first expression in snibgo's post is actually doing. I think it (and variations of it) will work for what it is I'm trying to do.

Thanks for taking the time to reply.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adjusting color of individual RGB channels

Post by fmw42 »

I understand now that your values are coming from txt: output. But note that the color values you are showing as 1,0 (2,19,51), i.e. the values 2,19,51 are in quantum range of your Imagemagick compile. The default is Q16 or a range of 0 to 65535. If you are using a Q8 compile, then those values are in the range 0 to 255. Snibgo mentioned his assumption of Q8. But your IM is ImageMagick 6.9.9-40 Q16. So it is Q16 and your values are in the range 0 to 65535. You can see the normal 0 to 255 range of values by looking at the end of the line for the actual srgb(r,g,b) values.
Post Reply