我认为你的
(bbox=798, 166, 300, 400))
x=798 出现在您的屏幕上
所以你必须在你的屏幕上显示你的 (x,y)。
看这张图:
enter image description here
结果是 None ,因为它无法捕获您的屏幕
当我修改 x=100 时,它可以工作
enter image description here
代码:
import numpy as np
from PIL import ImageGrab,Image
img=ImageGrab.grab(bbox=(798,166,300,400)) #798
# img=Image.open("Modric.jpg")
print(type(img))
img_np=np.array(img)
print(type(img_np))
print(img_np.shape)
结果:
<class 'PIL.Image.Image'>
<class 'numpy.ndarray'>
() *******None
x=100之后:
import numpy as np
from PIL import ImageGrab,Image
img=ImageGrab.grab(bbox=(100,166,300,400)) #798
# img=Image.open("Modric.jpg")
print(type(img))
img_np=np.array(img)
print(type(img_np))
print(img_np.shape) code here
结果:
<class 'PIL.Image.Image'>
<class 'numpy.ndarray'>
(234, 200, 3)
正确的x坐标和屏幕,它可以工作
对不起第一个问题,正确的在这里
import tkinter
win=tkinter.Tk()
width=win.winfo_screenwidth() #get your screen's width
height=win.winfo_screenheight() #get your screen's height
print(width,height)
img=ImageGrab.grab(bbox=(300,400,width,height)).convert("L") #798
img_np=np.array(img)
print(img_np.shape)
结果:
1536 864
(464, 1236)
当您使用 bbox=(x,y,width,height) 时,像素计数方法是 width-x 和 height-y 。所以你必须让你的宽度>x 和高度>y 并且它可以工作
转灰度图可以用opencv
import cv2
gray=cv2.cvtColor(img_np,cv2.COLOR_RGB2GRAY)[enter link description here][3]
链接
或者
PIL convert("L") 函数
img=ImageGrab.grab(bbox=(300,400,width,height)).convert("L") #798
or
公式:
def rgb2gray(rgb):
"""
gray=0.299*R+0.587*G+0.144*B
"""
return np.uint8(np.dot(rgb[...,:3], [0.299, 0.587, 0.114]))