21
6
Average color of an image
Scientists have been able to determine the average color of the universe but in how many bytes can we find the average color on an image?
Your task
Your input will be a single image which you will need to find the average of the colors in the image and output a hex color-string (#??????
). The image can be any of the following formats
- JPEG/JFIF
- JPEG 2000
- TIFF
- GIF
- BMP
- PNG
- PNM
- PPM
The input can also be taken as a URL/URI to the image.
Build-in functions which calculate averages or sample the image at once such as ImageMeasurements
are not allowed.
Examples
Results will differ slightly depending on how you calculate the average and which color models you use. I've added RGB and LCH (HSV) values for the images below.
output: #53715F
RGB, may also be #3B7D3D
LCH (HSV)
output: #8B7D41
RGB, #96753C
LCH (HSV)
What image formats do we have to handle? Specifically, can we choose to handle only PPM? – Dennis – 2015-07-22T04:56:13.223
Can I have a smaller test case please? My script is very slow, and while I will run it on the large case, I don't to waste that time if it is wrong. Or even just the script you calculated it with. – Maltysen – 2015-07-22T05:10:28.360
@Maltysen I've added a 240x140 example. Hopfully that's small enough – Downgoat – 2015-07-22T05:14:01.420
Should we always round down? In the first example, the
95.6...
, which you have rounded to95
in the specified output. – Dennis – 2015-07-22T05:16:40.010@Dennis Yeah, you can just truncate off (round down) the decimal – Downgoat – 2015-07-22T05:17:49.703
Must the colourspace used for the average be uniform (e.g. L*a*b*) or can it be the colourspace of the input image (i.e. YUV for JPEG, etc.)? What about gamma? – Peter Taylor – 2015-07-22T05:53:17.490
4PS There's no point posting a question in the sandbox unless you're going to leave it there for at least 24 hours, so that people in other time zones can see it, and realistically you need to give it 72 hours because not everyone checks the sandbox obsessively. – Peter Taylor – 2015-07-22T05:55:51.240
@PeterTaylor I've updated the question, your results will obviously depend on which color model you use. – Downgoat – 2015-07-22T06:11:35.060
You may want to add in the fact that you're assuming the images are unsigned 8-bit integer per channel... or 24-bit RGB images. There are some images... like in TIF... especially in medical image stacks where the bit precision is larger... 16-bit or 32-bit perhaps. – rayryeng - Reinstate Monica – 2015-07-22T17:39:58.297