Home > Mobile >  How do I screenshot a single monitor using OpenCV?
How do I screenshot a single monitor using OpenCV?

Time:11-17

I am trying to devleope a device that changes the RGB led strips according to the colour of my display. To this I am planning on screnshotiing the screen an normalising/taking the mean of the colours of individual pixels in the display. I have figured out how to screenshot a single monitor but want to make it work with a multi monitor setup. Here's my basic code. Any help would be greatly appreciated.

import numpy as np
import cv2
import pyautogui
   
  
# take screenshot using pyautogui
image = pyautogui.screenshot()
   
# since the pyautogui takes as a 
# PIL(pillow) and in RGB we need to 
# convert it to numpy array and BGR 
# so we can write it to the disk
image = cv2.cvtColor(np.array(image),
                     cv2.COLOR_RGB2BGR)

I tried this using the mss module but it isn't working. It's having an issue where the secondary display is just clipping in the final image.

import numpy as np
import cv2
import pyautogui
import mss 
  
with mss.mss() as sct:
    
    # Get information of monitor 2
    monitor_number = 1
    mon = sct.monitors[monitor_number]

    # The screen part to capture
    monitor = {
        "top": mon["top"],
        "left": mon["left"],
        "width": mon["width"],
        "height": mon["height"],
        "mon": monitor_number,
    }
    output = "sct-mon{mon}_{top}x{left}_{width}x{height}.png".format(**monitor)

    # Grab the data
    sct_img = sct.grab(monitor)
    img = np.array(sct.grab(monitor)) # BGR Image

CodePudding user response:

Using enter image description here


The above sample demonstrates the grabbing process.
You still have to compute the normalized mean of colour...
Note the the pixel format is BGRA and not BGR (the last channel is alpha (transparency) channel, that may be ignored).

  • Related