0

python 2.7.18 , win10

I am trying to obtain an arbitery pixel's rgb (e.g top left corner 0,0). It doesnt need to be my mouse current position.

Not sure about how does it work? screenshot the current screen and save it to an image then PIL to edit it? Or assign a variable for somewhere temp storage then read the rgb directly? Sorry, I am new and dont understand how does it work in python. (I also read an image that is composed of just an number array in numpy/opencv).

There are some other questions Where does it store after taking a screenshot if no var assigned? Furthermore, is python like matlab? For example, "ans" is used to store the last executed command. Alternatively, "_" can only be used in IDE for last executable.

Many thanks

Tried: Method 1 using PIL/pillow

PIL.ImageGrab.grab().load()[0,0]

Method 2 using pyautogui

im.getpixel((0,0))
pyautogui.getpixel(0,0)
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • For your first question, you may find answer here : https://stackoverflow.com/questions/2846947/get-screenshot-on-windows-with-python . If no var is assigned, the output is generally deleted after execution of the function. Is python like matlab : no. They have some similar syntax but python is object-oriented, has a global interpreter lock, does not have an 'ans' method... As you said in IDE '_' replaces ans. – Adrien Mau Sep 09 '22 at 12:49
  • Collecting a full screen for 1 pixel is clearly overkill (~1_000_000 time more data than required for a HD screen). Probing 1 pixel is fine as long as you require only one (or very few) of them. Note that probing a pixel can be slow as the value can be stored on the GPU and so require some device interaction/sync (not to mention expensive system calls). – Jérôme Richard Sep 09 '22 at 13:00
  • Hi Richard still stucking on how to get a pixel's rgb first , but i definitely need to address this too. Any thought like just only capture a small portion screen? then obtain a pixel from it either in form of file or memory variable?? – coding newbie Sep 09 '22 at 13:26

0 Answers0