python - im.getcolors() 返回 None

标签 python image-processing

我正在使用一个简单的代码通过 PIL 的 getcolors() 函数将图像与桌面屏幕截图进行比较。当我打开一张图片时,它起作用了:

im = Image.open('sprites\Bowser\BowserOriginal.png')
current_sprite = im.getcolors() 
print current_sprite

但是,同时使用 pyautogui.screenshot() 和 ImageGrab.grab() 作为屏幕截图,我的代码没有返回任何结果。我尝试使用 RGB 转换,如下所示:Cannot use im.getcolors .

此外,即使我将屏幕截图保存为 .png,它仍然不返回任何内容。

i = pyautogui.screenshot('screenshot.png')
f = Image.open('screenshot.png')
im = f.convert('RGB')
search_image = im.getcolors()
print search_image

第一次发帖,不胜感激。

最佳答案

很老的问题,但对于那些现在看到这个的人来说:

Image.getcolors() 将“maxcolors – 最大颜色数”作为参数。 (来自文档 here )。

图像可以拥有的最大颜色数等于它包含的像素数。 例如,一张 50*60px 的图像最多有 3,000 种颜色。

要将它翻译成代码,试试这个:

# Open the image.
img = Image.open("test.jpg")
# Set the maxcolors number to the image's pixels number.
colors = img.getcolors(img.size[0]*img.size[1])

关于python - im.getcolors() 返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37146711/

相关文章:

python - 使用allowDiskUse时语法无效:True through PyMongo

python - 从命令行参数转换时间

Python:使用负函数比较元胞数组

image-processing - 霍夫变换方程

python - 揭秘 sharedctypes 的性能

python / split : How to find and select an option on a site?

python - 最小最大图像直方图拉伸(stretch)

windows - 帧到视频 - Windows 8 中的图像处理(仅限 WinRT)

c# - 使用 C# 5.0 进行并行图像处理

image - 检测图像通常是红色、绿色、蓝色还是黑色