Cannot use im.getcolors

痞子三分冷 提交于 2019-12-13 20:04:23

问题


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?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/27579899/cannot-use-im-getcolors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!