文章背景:最近在学习pyautogui模块中的处理屏幕,想要通过运行python代码,获取鼠标光标处像素的RGB颜色值。python代码如下:
# from the book: Automate the Boring Stuff with Python
import pyautogui
pyautogui.pause = 1
pyautogui.failsafe = True
print('Press Ctrl-C to quit.')
try:
while True:
# Get and print the mouse coordinates.
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
pixelColor = pyautogui.screenshot().getpixel((x,y))
positionStr += ' RGB: (' + str(pixelColor[0]).rjust(3)
positionStr += ', ' + str(pixelColor[1]).rjust(3)
positionStr += ', ' + str(pixelColor[2]).rjust(3) + ')'
print(positionStr,end = '')
print('\b'*len(positionStr),end='',flush = True)
except KeyboardInterrupt:
print('\nDone.') 代码运行时,程序报错。pyscreeze.PyScreezeException: The Pillow package is required to use this function.
检查电脑上的pyllow模块,目前是6.2.0版本,还不是最新版本,考虑升级pyllow模块。我的电脑环境是:win10+py3.7+anaconda3。在Anaconda3的Prompt上运行如下代码:
pip install -i https://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com pillow

也就是说,豆瓣源上的pillow模块是6.2.0版本,而pypi.org网站上最新的pillow模块是7.2.0版本。后来在网上找到了清华大学源。在Anaconda3的Prompt上运行如下代码:
pip install -U pillow -i https://pypi.tuna.tsinghua.edu.cn/simple/

Pillow-7.2.0安装成功。
重新运行前面的python代码,不再报错。
视频演示:http://mpvideo.qpic.cn/0bf2l4bgwaaciuag6dj3mrpvex6dnnpqe2ya.f10002.mp4?
参考资料: