2

I'm trying to do a piece of code that takes a picture of a specific place on the screen. I can take a picture of the screen, but not of a specific place.

My goal is to take a picture where the top left point is at this position: (536, 76)

and the bottom right point is at this position: (998, 791)

Here's what I have so far, which takes a screenshot

import PIL

snapshot = PIL.ImageGrab.grab()

save_path = "C:\\Users\\pierr\\Pictures\\Screenshots\\oui_" + str(i) + ".jpg"  

snapshot.save(save_path)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Pierre Marsaa
  • 346
  • 2
  • 10
  • 1
    Try adding a `bbox` parameter to your `grab()` function as shown here... https://stackoverflow.com/a/55516307/2836621 – Mark Setchell Jun 14 '19 at 09:36
  • not a bad idea, but, i don't know why, it doesn't work good, This is what I wrote : `snapshot = ImageGrab.grab(bbox = (535,75,999,792))` It takes me a picture but not in the right place, however, for bbox you have to enter the coordinates x,y from the point at the top left and coordinates x,y from the point at the bottom right no? – Pierre Marsaa Jun 14 '19 at 10:47
  • Try grabbing the entire screen and checking what dimensions you get and how that relates to the screen size - maybe you have X and Y interchanged? Or maybe there is some screen-doubling going on? – Mark Setchell Jun 14 '19 at 16:14
  • I finally made it, the coordinates were wrong. The "problem" being that I had retrieved the first coordinates with the same code that gave me the new coordinates ^^ Anyway, now it works, thank you very much – Pierre Marsaa Jun 14 '19 at 16:30

1 Answers1

2

After Getting the image convert it to numpy array(i.e opencv format of image) by using check here for extra information

image = numpy.array(snapshot) 

From this numpy array access the part of image with

image[start_y:end_y, start_x:end_x] in your case image[76:791, 536:998] and then u can save image using cv2.imwrite()

Veer
  • 211
  • 4
  • 14
  • The code works, but there is a gap between the coordinates I enter and the coordinates where the capture is taken – Pierre Marsaa Jun 14 '19 at 12:33
  • After an afternoon of research, I finally managed to get something not too bad, unfortunately, I can't read the image that comes out of the code you advised me.... The image is well recorded in my files, in png, but I can't read it and I don't know why, but hey, I found another technique, which works so everything is fine, thanks – Pierre Marsaa Jun 14 '19 at 16:32