A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

How do I write a program that takes some sort of image file as input and outputs a grayscale copy of the file?

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    In any language of your choice!

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Applescript and/or Automator can do this quite simply on a Mac either as a stand alone application or a Service added to the Finder or main Application contextual menu. This can be a simple drag and drop exercise in Automator, not sure how it can be done in Windows but I assume there is something similar. I would, and have gone for the drag and drop option and have a similar action for processing large numbers of photographs.

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    It depends on the compression used e.g. jpeg, png, gif but the general theory is the same. Go through the file individually change the color of the pixel blocks. For example, in C#: http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    This should do the trick (C): void RGBToGray(unsigned char* pRGBData, unsigned int numPixels, unsigned char* pGrayData) { while(numPixels--) { *pGrayData++ = *pRGBData++ * 0.33f + *pRGBData++ * 0.59f + *pRGBDatA++ * 0.11f; } }

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Oh, you wanted it to take an image file. Didn't see that :P Well, basically add loading the image file to an RGB buffer to the above. How to do that largely depends on the file format, and the frameworks you're using. In Qt, for example, you could simply do (changing the RGBToGray function, because Qt's RGB format is 32 bits so we need to skip the first byte out of every pixel) void RGBToGray(unsigned char* pRGBData, unsigned int numPixels, unsigned char* pGrayData) { while(numPixels--) { pRGBData++; // skip A *pGrayData++ = *pRGBData++ * 0.33f + *pRGBData++ * 0.59f + *pRGBDatA++ * 0.11f; } } Now, for loading the image: char filename[] = "myImage.jpg"; int main() { QImage *pImg = 0; QImageReader reader(filename); reader.read(pImg); pImg->convertToFormat(QImage::Format_RGB32); RGBToGray(pImg->bits(), pImg->width()*pImg->height()); } (Have I mentioned that Qt is awesome, by the way?)

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    And again, missed outputting the grayscale copy of the file. I should really learn to properly read the questions before answering. To do that, you'd make the destination buffer width*height*4 (4 channels like the input buffer), adjusting RGBToGray accordingly, and then save the file with QImage newImg(pGrayData, width, height, QImage::Format_RGB32); newImg.save(destFileName, "JPG", 85);

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.