sualfred/script.embuary.helper

Average color function is inaccurate

Closed this issue · 1 comments

roidy commented

After some basic testing the average color function seems to be inaccurate. It could be simplified and made more accurate using the following code:-

img = Image.open(filepath)
img2 = img.resize((1,1), Image.ANTIALIAS)
col = img2.getpixel((0,0))
imagecolor = 'FF%s%s%s' % (format(col[0], '02x'), format(col[1], '02x'), format(col[2], '02x'))

Take for example the following image, I have provided color sample of the output from both your original code and the new PIL based code.
test1

Clearly the average color of an equal sized area of red and blue should be purple like the new code outputs, not red like the current code. While this is an extreme example and on a real world image the difference is much less noticeable, it's still there.

Not only is the new code more accurate but on an average 200x200 pixel image it runs about 30% faster in my testing.

I'm happy to see a PR