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

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions.

A community for students.

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

Computer Science
See more answers at brainly.com
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

In any language of your choice!
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.
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

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

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

Not the answer you are looking for?

Search for more explanations.

Ask your own question