How to convert 8-bit GrayScale to RGB format (and have it still "look" gray!!!)?

Posted on 2004-11-02
Last Modified: 2008-01-09

  I have an image processing app that I am working on and ran into a snag trying to convert from 8-bit grayscale to RGB. The reason I want to do this is so that I can store my image data in the format that I want to use (.XPM).

  I noticed alot of people are asking this same question but are coming from the standpoint of wanting to add color to grayscale image. Think Turner broadcast colorization of an old western or something. I don't want this at all. I want my RGB equivalent image to look exactly like my grayscale image.

  My input is gray values between 0->FF (0-255).
  My output is RGB of the gray value (3 values between 0->FF (0-255))

  For example, I know that white is FF in grayscale. After the conversion it should be R=FF, G=FF, B=FF in RBG format. Likewise black is 00: it should be R=00, G=00, B=00. But how do I convert the other 254 values?

  Is this possible? Does anyone know the conversion formula?


Question by:sapbucket
    LVL 18

    Accepted Solution

    you could set all channels to the same value. So gray E0 would become rgb (E0,E0,E0).
    LVL 10

    Expert Comment

    GRAY=0.299R + 0.587G + 0.114B
    LVL 18

    Expert Comment

    and (x,x,x) would be a fine solution to that ;)
    LVL 12

    Expert Comment

    Hi rj2,
    Wrong conversion direction :-)
    LVL 10

    Expert Comment

    Hi all
    sapbucket asked for conversion formula, so I posted conversion formula.
    The formula at least demonstrates why gray scale conversion is a one way process. Information is lost about along the way, there are many combinations of R,G,B that satisfies the equation.
    LVL 3

    Author Comment


      perfect answer!

      I don't know why I didn't try that myself. I experimented with using the gray scale value and 0's - so like 255, 255, 0 or 0, 255, 0 - which just gives you pure color tones. Thanks for the help!

      Does cpan have a perl module that specializes in image processing of 8-bit values? Seems weird that I have to triple the file size (1 byte translates to 3 bytes) just to store the image on disk. At least that is using .pbm or .xpm.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now