Mikhaylova
Mikhaylova

Reputation: 137

Cannot use im.getcolors

I was trying this code:

im = Image.open("myimage") 
colors = im.getcolors()
print colors

and it returns "None". So I tried this:

im = Image.open("myimage") 
size = im.size 
colors = im.getcolors(size[0]*size[1]) 

and when I "print colors" with this, Python basically crashes. I'm not using a huge image. How can I make it work?

My goal is to know from an image how many pixels are closer to black and how many px are closer to white. Maybe im.getcolors it's not the right solution?

Upvotes: 2

Views: 4037

Answers (2)

im_rgb = im.convert('RGB')
colors = im_rgb.getcolors(maxcolors=1000000) #max colors to show
print(colors)

Upvotes: 1

ekhumoro
ekhumoro

Reputation: 120598

The image has to be in RGB mode in order to use getcolors. So try:

    im_rgb = im.convert('RGB')
    colors = im_rgb.getcolors()
    print colors

Upvotes: 4

Related Questions