Monolith
Monolith

Reputation: 1147

Grab image from screen to numpy array and display it as black and white image

I am trying to read image data from a certain part of the screen, so I can post-process it in a form of a numpy array. This is what I tried so far:

import numpy as np
from PIL import ImageGrab, Image

img = ImageGrab.grab(bbox=(798, 166, 300, 400))  # (bbox=x,y,width,height)
img_np = np.array(img)

But when I try to print img_np, it returns:

<PIL.Image.Image image mode=RGB size=0x234 at 0x109F8F0>

It doesn't seem like it's an numpy array. I also want to display the black and white image from the numpy array to verify what I am doing is right (and also display processed numpy arrays in the future). Is there anything I am doing wrong?

Upvotes: 1

Views: 4481

Answers (2)

Jonas
Jonas

Reputation: 402

as @Ma Jun pointed out the (bbox=x,y,width,height) is not really like start from (x,y) and get a width to height window. Rather the opening window is x,y width-x, height-y I added this post because it took me a long time to figure this out :) keywords: Python ImageGrab bbox

Upvotes: 0

Ma Jun
Ma Jun

Reputation: 104

I think that your

(bbox=798, 166, 300, 400))

the x=798 is over your screen

so you must make your (x,y) in your screen.

look this picture: enter image description here the result is None ,because it cant capture your screen

and when I modify the x=100,it works enter image description here

the code:

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)

result:

<class 'PIL.Image.Image'>
<class 'numpy.ndarray'>
()      *******None

after 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

result:

<class 'PIL.Image.Image'>
<class 'numpy.ndarray'>
(234, 200, 3)

the right x coord and in screen, it can works


I am sorry for the first question,the right is here

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)

result:

1536 864
(464, 1236)

when you use the bbox=(x,y,width,height) ,the pixel counting method is width-x and height-y . so you must make your width>x and height>y and it works


as for convert to gray image,you can use opencv

import cv2
gray=cv2.cvtColor(img_np,cv2.COLOR_RGB2GRAY)[enter link description here][3]

link or the PIL convert("L") function

img=ImageGrab.grab(bbox=(300,400,width,height)).convert("L")  #798

 or

the formula:

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]))

Upvotes: 3

Related Questions