ImageGrab.grab(bbox)和Image.getpixel()一起使用 [英] ImageGrab.grab(bbox) and Image.getpixel() Used together
问题描述
使用PIL.ImageGrab的抓取功能时,有一个名为bbox的参数,该参数指定应使用屏幕抓图的边框.当我想获取特定像素的值时,坐标应该是什么? 例如:
While using the grab function from PIL.ImageGrab, there's a parameter called bbox, which specifies the bounding box of which the screengrab should be taken. And when I want to get the value of a specific pixel, what should the coordinates be? For example:
im = ImageGrab.grab(bbox=(500, 500, 600, 700)
print(im.getpixel(510, 510))
print(im.getpixel(10, 10))
两个语句都给我一个错误.我在这里做什么错了?
Both the statements give me an error. What am I doing wrong here?
推荐答案
您犯了两个错误:
-
由于指定了边界框,因此生成的图像小于屏幕分辨率.边界框是一个
(left_x, top_y, right_x, bottom_y)
元组,因此值为(500, 500, 600, 700)
时,图像的宽度为100像素,高度为200像素.
Since you've specified a bounding box, the resulting image is smaller than your screen resolution. The bounding box is a
(left_x, top_y, right_x, bottom_y)
tuple, so with the values(500, 500, 600, 700)
, the image has a width of 100 pixels and a height of 200 pixels.
这说明了为什么im.getpixel(510, 510)
不起作用-坐标510在图像之外.
That explains why im.getpixel(510, 510)
doesn't work - the coordinate 510 is outside of the image.
getpixel
方法将坐标作为(x, y)
元组,而不是两个单独的参数.正确的语法是im.getpixel((10, 10))
.
The getpixel
method takes the coordinates as a (x, y)
tuple, not as two separate arguments. The correct syntax is im.getpixel((10, 10))
.
这篇关于ImageGrab.grab(bbox)和Image.getpixel()一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!