Search code examples
pythonopencvscreenshot

Get average-dominant color from a rectangle on screen (live/python)


What I am trying to do is set a rectangle on the PC screen (Windows 10). Every 5-10 seconds, get the average color of that rectangle.

Example: rectangle 50x50 on bottom left of the screen > get average color. If redish, do this. If blueish, do that. Repeat.

My idea is that I can get a screenshot (Pyautogui probably), save the image, then crop the desired rectangle (OpenCV), save the image again and analyze it (OpenCV/Numpy).

However, I think this would be very CPU intensive. Where could I start, if I wanted something lightweight and fast? What libraries would you suggest?


Solution

  • You can do this task more efficiently but even if you do the way you describe, these are not computationally intensive operations. All should take less than 0.1 seconds. As @fmw42 described, you don't need to save, do the analysis on the fly.

    The libraries you mention are good.

    1. Get screenshot. Store it in memory (not disk)
    2. mean: get the mean of each channel color, and get the dominant color.
    3. save image on disk