The problem is that I can't understand how the "official" JPG->PGM convertitors work in terms of what value to assign to the final pixel (i guess, 0->255) starting from the classic RGB format.

There is likely a gamma adjustment in the conversion those "official" tools are using.
That is, it is not just a linear transform.

See this Wikipedia section for the details: Converting color to grayscale

I believe you want to use the formula for Csrgb.
Try it out and see if it matches the results you're expecting.

Basically, you'll do this:

  1. Take R, G, B color (each in [0,1] range)
    • If they're in the range 0..255 instead, simply divide by 255.0
  2. Compute Clinear = 0.2126 R + 0.7152 G + 0.0722 B
    • This is likely the linear transform you were applying before
  3. Compute Csrgb according to it's formula, based on Clinear
    • This is the nonlinear gamma correction piece you were missing
    • Check out this WolframAlpha plot
    • Csrgb = 12.92 Clinear when Clinear <= 0.0031308
    • Csrgb = 1.055 Clinear1/2.4 - 0.055 when Clinear > 0.0031308
Answer from Timothy Shields on Stack Overflow
Top answer
1 of 6
38

The problem is that I can't understand how the "official" JPG->PGM convertitors work in terms of what value to assign to the final pixel (i guess, 0->255) starting from the classic RGB format.

There is likely a gamma adjustment in the conversion those "official" tools are using.
That is, it is not just a linear transform.

See this Wikipedia section for the details: Converting color to grayscale

I believe you want to use the formula for Csrgb.
Try it out and see if it matches the results you're expecting.

Basically, you'll do this:

  1. Take R, G, B color (each in [0,1] range)
    • If they're in the range 0..255 instead, simply divide by 255.0
  2. Compute Clinear = 0.2126 R + 0.7152 G + 0.0722 B
    • This is likely the linear transform you were applying before
  3. Compute Csrgb according to it's formula, based on Clinear
    • This is the nonlinear gamma correction piece you were missing
    • Check out this WolframAlpha plot
    • Csrgb = 12.92 Clinear when Clinear <= 0.0031308
    • Csrgb = 1.055 Clinear1/2.4 - 0.055 when Clinear > 0.0031308
2 of 6
4

To harold's point about the "Y plane": standard color JPEGs are encoded using the YCbCr colorspace, where Y is the luminance component (i.e. the brightness) and Cb and Cr are the blue-difference and red-difference chroma components. So one way of turning a color JPEG into a grayscale one is to simply drop the Cb and Cr components.

There is a utility called jpegtran than can do this losslessly, using the -grayscale option. (The lossless part would really only matter if you wanted to end up with a JPEG and not PGM, to avoid generation loss.) In any case, this would probably be the fastest way to do this transformation, because it doesn't even decode the image into pixels, much less do math on each one.

🌐
Mustafa Murat ARAT
mmuratarat.github.io › 2020-05-13 › rgb_to_grayscale_formulas
RGB to Grayscale Conversion | Mustafa Murat ARAT
May 13, 2020 - #OPENCV reads as Blue Green Red ... algorithm that OpenCV’s cvtColor() use (see the documentation) The formula used is: \[Y = 0.299\times R + 0.587 \times G + 0.114 \times B\]...
Discussions

colors - Convert grayscale value to RGB representation? - Stack Overflow
How can I convert a grayscale value (0-255) to an RGB value/representation? It is for using in an SVG image, which doesn't seem to come with a grayscale support, only RGB... Note: this is not RGB -> More on stackoverflow.com
🌐 stackoverflow.com
Help Needed with RGB to Grayscale Conversion in TouchDesigner Using Reorder and Math TOPs
In the common tab on most of the tops you can select between different bit rates and channels. Choose one of the monos. It will become Grey scale. More on reddit.com
🌐 r/TouchDesigner
1
1
October 22, 2023
Confusion about Image distortion converting from RGB to 16-bit?
Notes on Quality Questions & Productive Participation Include Images Images give everyone a chance to understand the problem. Several types of images will help: Example Images (what you want to analyze) Reference Images (taken from published papers) Annotated Mock-ups (showing what features you are trying to measure) Screenshots (to help identify issues with tools or features) Good places to upload include: Imgur.com, GitHub.com, & Flickr.com Provide Details Avoid discipline-specific terminology ("jargon"). Image analysis is interdisciplinary, so the more general the terminology, the more people who might be able to help. Be thorough in outlining the question(s) that you are trying to answer. Clearly explain what you are trying to learn, not just the method used, to avoid the XY problem . Respond when helpful users ask follow-up questions, even if the answer is "I'm not sure". Share the Answer Never delete your post, even if it has not received a response. Don't switch over to PMs or email. (Unless you want to hire someone.) If you figure out the answer for yourself, please post it! People from the future may be stuck trying to answer the same question. (See: xkcd 979 ) Express Appreciation for Assistance Consider saying "thank you" in comment replies to those who helped. Upvote those who contribute to the discussion. Karma is a small way to say "thanks" and "this was helpful". Remember that "free help" costs those who help: Aside from Automoderator, those responding to you are real people, giving up some of their time to help you. "Time is the most precious gift in our possession, for it is the most irrevocable." ~ DB If someday your work gets published, show it off here! That's one use of the "Research" post flair. Be civil & respectful Be kind. Remember the human . Be Excellent To Each Other . Don't make your robotic overlord angry. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/ImageJ
10
1
September 24, 2024
Can't change image color space to grayscale
Which file format did you save the image in? What happens if you select the image you've placed in InDesign, right-click and select Edit Original? When it opens in Photoshop, is it grayscale or RGB? How do you determine that the image is still RGB? If it's in a PDF, is it an interactive PDF (which is always RGB)? What happens if you turn off the black ink in Separations Preview panel? Does the image disappear? More on reddit.com
🌐 r/indesign
14
1
August 12, 2024
🌐
MathWorks
mathworks.com › matlab › graphics › images
rgb2gray - Convert RGB image or colormap to grayscale - MATLAB
I = rgb2gray(RGB) converts the truecolor image RGB to the grayscale image I. The rgb2gray function converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance.

The problem is that I can't understand how the "official" JPG->PGM convertitors work in terms of what value to assign to the final pixel (i guess, 0->255) starting from the classic RGB format.

There is likely a gamma adjustment in the conversion those "official" tools are using.
That is, it is not just a linear transform.

See this Wikipedia section for the details: Converting color to grayscale

I believe you want to use the formula for Csrgb.
Try it out and see if it matches the results you're expecting.

Basically, you'll do this:

  1. Take R, G, B color (each in [0,1] range)
    • If they're in the range 0..255 instead, simply divide by 255.0
  2. Compute Clinear = 0.2126 R + 0.7152 G + 0.0722 B
    • This is likely the linear transform you were applying before
  3. Compute Csrgb according to it's formula, based on Clinear
    • This is the nonlinear gamma correction piece you were missing
    • Check out this WolframAlpha plot
    • Csrgb = 12.92 Clinear when Clinear <= 0.0031308
    • Csrgb = 1.055 Clinear1/2.4 - 0.055 when Clinear > 0.0031308
Answer from Timothy Shields on Stack Overflow
🌐
Dynamsoft
dynamsoft.com › blog › insights › image-processing › image-processing-101-color-space-conversion
Color Space Conversion & Binarization for Image Processing | Dynamsoft Blog
May 24, 2019 - HSV (hue, saturation, value) and HSL (hue, saturation, lightness or luminance) are transformations of a Cartesian RGB color space. To convert HSV to grayscale, we first need to convert HSV to RGB and then convert the RGB triple to a grayscale value.
🌐
Wikipedia
en.wikipedia.org › wiki › Grayscale
Grayscale - Wikipedia
April 4, 2026 - {\displaystyle Y'=0.299R'+0.587G'+0.114B'} where we use the prime to distinguish these nonlinear values from the sRGB nonlinear values (discussed above) which use a somewhat different gamma compression formula, and from the linear RGB components.
🌐
Good Calculators
goodcalculators.com › rgb-to-grayscale-conversion-calculator
RGB to Grayscale Conversion Calculator | Good Calculators
The grayscale weighted average, Y, is represented by the following equation: ... Where R, G and B are integers representing red (R), green (G) and blue (B) with values in the range 0–255.
Find elsewhere
🌐
Baeldung
baeldung.com › home › algorithms › how to convert an rgb image to a grayscale
How to Convert an RGB Image to a Grayscale | Baeldung on Computer Science
March 18, 2024 - We can easily see that this method presents a very serious weakness since one RGB component is not used. This is definitely a problem because the amount of lightness that our eye perceives depends on all three basic colors. Another method is to take the average value of the three components (red, green, and blue) as the grayscale value:
🌐
TutorialsPoint
tutorialspoint.com › dip › grayscale_to_rgb_conversion.htm
Grayscale to RGB Conversion
Average method is the most simple one. You just have to take the average of three colors. Since its an RGB image, so it means that you have add r with g with b and then divide it by 3 to get your desired grayscale image.
🌐
John D. Cook
johndcook.com › blog › 2009 › 08 › 24 › algorithms-convert-color-grayscale
Three algorithms for converting color to grayscale
April 29, 2019 - The formula for luminosity is 0.21 R + 0.72 G + 0.07 B. The example sunflower images below come from the GIMP documentation. The lightness method tends to reduce contrast. The luminosity method works best overall and is the default method used if you ask GIMP to change an image from RGB to ...
🌐
Had2know
had2know.org › technology › rgb-to-gray-scale-converter.html
RGB to Grayscale Conversion Calculator
To convert a non-neutral color ... r, g, and b are integers between 0 and 255. The grayscale weighted average, x, is given by the formula x = 0.299r + 0.587g + 0.114b....
🌐
Brandonrohrer
brandonrohrer.com › convert_rgb_to_grayscale.html
How to Convert an RGB Image to Grayscale
import lodgepole.image_tools as lit gray_img = lit.rgb2gray(color_img) If after reading this far you insist on straight up averaging the three channels together, I will judge you. Now go make beautiful grayscale images!
🌐
The blog at the bottom of the sea
blog.demofox.org › 2014 › 02 › 03 › converting-rgb-to-grayscale
Converting RGB to Grayscale « The blog at the bottom of the sea
January 22, 2018 - There’s a wikipedia page on this topic here, but the equation to use is below: grayScale = red * 0.3f + green * 0.59f + blue * 0.11f; Here are some sample images to show you the difference.
🌐
RapidTables
rapidtables.com › convert › image › rgb-to-grayscale.html
Color image to black and white converter
RGB to grayscale image conversion online: Gray RGB color code has equal red,green and blue values: R = G = B · For each image pixel with red, green and blue values of (R,G,B): R' = G' = B' = (R+G+B) / 3 = 0.333R + 0.333G + 0.333B · This formula can be changed with different weights for each ...
🌐
Quora
quora.com › How-do-I-convert-RGB-to-grayscale
How to convert RGB to grayscale - Quora
Answer (1 of 3): It depends on the specifics of the RGB space in which you’re working. You have to look at the defined white point, and how the specific red, green, and blue primaries combine to create that white. It also makes a difference as to whether or not the RGB values in question ...
🌐
scikit-image
scikit-image.org › docs › stable › auto_examples › color_exposure › plot_rgb_to_gray.html
RGB to grayscale — skimage 0.26.0 documentation
import matplotlib.pyplot as plt from skimage import data from skimage.color import rgb2gray original = data.astronaut() grayscale = rgb2gray(original) fig, axes = plt.subplots(1, 2, figsize=(8, 4)) ax = axes.ravel() ax[0].imshow(original) ax[0].set_title("Original") ax[1].imshow(grayscale, ...
🌐
MathWorks
mathworks.com › matlab › graphics › images
im2gray - Convert RGB image to grayscale - MATLAB
The im2gray function converts RGB values to grayscale values by forming a weighted sum of the R, G, and B components: 0.298936021293775 * R + 0.587043074451121 * G + 0.114020904255103 * B · The coefficients used to calculate grayscale values in the im2gray function are identical to those used ...
🌐
KDnuggets
kdnuggets.com › 2019 › 12 › convert-rgb-image-grayscale.html
How to Convert an RGB Image to Grayscale - KDnuggets
December 18, 2019 - import lodgepole.image_tools as lit gray_img = lit.rgb2gray(color_img) If after reading this far you insist on straight up averaging the three channels together, I will judge you. Now go make beautiful grayscale images!