Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

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

OpenStudy (anonymous):

In any language of your choice!

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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; } }

OpenStudy (anonymous):

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?)

OpenStudy (anonymous):

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);

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!