Skip to content

Instantly share code, notes, and snippets.

@dyspop
Created March 27, 2017 02:50
Show Gist options
  • Save dyspop/77b220a38e4309326bdf802ac9ac1b64 to your computer and use it in GitHub Desktop.
Save dyspop/77b220a38e4309326bdf802ac9ac1b64 to your computer and use it in GitHub Desktop.
assign averages of image channel histograms to a dictionary
from PIL import Image
filename = 'pastries.jpg'
image = Image.open(filename)
# get average color
# inspiration from https://gist.github.com/olooney/1246268
def average_image_color(imagedata):
channel_histograms = {}
color_averages = {}
for mode in imagedata.mode:
channel_histograms[mode] = channel_histogram(imagedata,mode)
color_averages[mode] = sum( i*w for i, w in enumerate(channel_histograms[mode]) ) / sum(channel_histograms[mode])
return color_averages
print average_image_color(image)
@dyspop
Copy link
Author

dyspop commented Mar 27, 2017

{'B': 60, 'R': 152, 'G': 105}

@maky-hnou
Copy link

maky-hnou commented Mar 29, 2019

I got this error: channel_histogram is not defined.
What is channel_histogram?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment