无法使用im.getcolors [英] Cannot use im.getcolors

查看:179
本文介绍了无法使用im.getcolors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下代码:

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

,它返回"None".所以我尝试了这个:

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

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

,当我用它打印颜色"时,Python基本上崩溃了.我没有使用大图像.我该如何运作?

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

我的目标是从图像中知道多少像素更接近黑色,多少像素更接近白色.也许im.getcolors这不是正确的解决方案?

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?

推荐答案

图像必须处于RGB模式才能使用getcolors.因此,尝试:

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

这篇关于无法使用im.getcolors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆