我正在使用一个简单的代码通过 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/