Write a program that takes in a standard truecolor image and a single 24-bit RGB color (three numbers from 0 to 255). Modify the input image (or output a new image with the same dimensions) such that its average color is exactly the single color that was input. You may modify the pixels in the input image in any way you like to achieve this, but the goal is to make the color changes as visually unnoticeable as possible.
The average color of an RGB image is really a set of three arithmetic means, one for each color channel. The average red value is the sum of the red values across all the pixels in the image divided by the total number of pixels (the image area), rounded down to the nearest integer. The green and blue averages are calculated in the same way.
from PIL import Image print 'Enter image file' im = Image.open(raw_input()).convert('RGB') pixels = im.load() avg = [0, 0, 0] for x in range(im.size): for y in range(im.size): for i in range(3): avg[i] += pixels[x, y][i] print 'The average color is', tuple(c // (im.size * im.size) for c in avg)
(There are similar average color programs here, but they don't necessarily do the exact same calculation.)
The main requirement for your program is that for any input image, its corresponding output's average color must exactly match the color that was input - as judged by the Python snippet or some equivalent code. The output image must also have the exact same dimensions as the input image.
So you could technically submit a program that simply colors the entire input the specified average color (because the average would always be that color), but this is a popularity contest - the submission with the highest number of votes will win, and such a trivial submission will not get you many upvotes. Novel ideas like taking advantage of quirks in human vision, or shrinking the image down and drawing a colored border around it will (hopefully) get you votes.
Note that certain combinations of average colors and images necessitate extremely noticeable color changes. For example, if the average color to match were black (0, 0, 0), any input image would need to be made completely black because if any pixels had non-zero values, they would make the average non-zero as well (barring round-off errors). Keep such limitations in mind when voting.
Some images and their default average colors to play around with. Click for full sizes.
A. average (127, 127, 127)
B. average (62, 71, 73)
C. average (115, 112, 111)
D. average (154, 151, 154)
E. average (105, 103, 102)
Mount Shasta. Provided by me.
F. average (75, 91, 110)
- The exact input and output formats and image file types your program uses does not matter much. Just make sure it is clear how to use your program.
- It is probably a good idea (but not technically a requirement) that if an image already has the goal average color, it should be output as is.
- Please post test images with the average color input as either (150, 100, 100) or (75, 91, 110), so voters can see the same inputs across different solutions. (Posting more examples than this is fine, even encouraged.)