def _record_loop(q: SimpleQueue, filename, monitor, frame_rate): with mss() as sct: fourcc = cv2.VideoWriter_fourcc(*'mp4v') # adjust monitor to crop out the parts not visible if monitor['left'] < 0: monitor['width'] += monitor['left'] monitor['left'] = 0 if monitor['top'] < 0: monitor['height'] += monitor['top'] monitor['top'] = 0 monitor['height'] = min(monitor['height'], sct.monitors[0]['height'] - monitor['top']) monitor['width'] = min(monitor['width'], sct.monitors[0]['width'] - monitor['left']) out = cv2.VideoWriter(filename, fourcc, frame_rate, (monitor['width'], monitor['height'])) period = 1. / frame_rate while q.empty(): start_time = time.time() img = np.array(sct.grab(monitor)) out.write(img[:, :, :3]) # wait for frame rate time elapsed = time.time() - start_time if elapsed < period: time.sleep(period - elapsed) out.release()
def capture(x, y, w, h): with mss() as sct: region = { 'top': y, 'left': x, 'width': w, 'height': h} sct_img = sct.grab(region) img = Image.fromarray(np.array(sct_img)) return img
def view(self): with mss() as screen_capture: while cv2.waitKey(10) != 27: # Grab the pixels in the box (in full colour) image = np.array(screen_capture.grab(self.image_box)) # Discard unneeded colour information, makes calculations faster image_grey = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY) # Calculate edges image_laplacian = cv2.Laplacian(image_grey, cv2.CV_64F) # Show the captured area laplacian. cv2.imshow('Captured area', image_laplacian)
def run(self): self.start() # Wait one second for the zoom effect to end sleep(1) with mss() as screen_capture: while True: # Grab the pixels in the box (in full colour) image = np.array(screen_capture.grab(self.image_box)) # Discard unneeded colour information, makes calculations faster image_grey = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY) # Calculate difference in image value = self.difference(image_grey) if value > 0: self.jump()
def screen_record(): sct = mss() last_time = time.time() while (True): img = sct.grab(mon) print('loop took {} seconds'.format(time.time() - last_time)) last_time = time.time() img = np.array(img) processed_image = process_image(img) mean = np.mean(processed_image) print('mean = ', mean) if not mean == float(0): pg.press('space') if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break
def screen_recorder(duration, end_time, top, left, width, height): print("Starting recording") monitor = {"top": top, "left": left, "width": width, "height": height} codec = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(output, codec, duration, (width, height)) with mss() as sct: frames = 0 while time.perf_counter() < end_time: sct_img = sct.grab(monitor) img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX") image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) out.write(image) frames += 1 out.release() cv2.destroyAllWindows() print("Finished recording") return frames
def new_image_string(): # ScreenShot mon = {'top': 600, 'left': 130, 'width': 100, 'height': 130} sct = mss() while True: img = sct.grab(mon) img = np.array(img) img = process_image(img) # visualise cv2.imshow('hello', img) if cv2.waitKey(25) & 0xFF == ord("q"): cv2.destroyAllWindows() break #img -> base64 pil_img = Image.fromarray(img) buff = BytesIO() pil_img.save(buff, format="PNG") new_image_string = base64.b64encode(buff.getvalue()).decode("utf-8") return (new_image_string)
def loop(): with mss() as sct: #for _ in range(10): while True: f = sct.shot() # Read create screenshot img = skimage.io.imread(f) small = skimage.transform.resize(img, (108, 192), anti_aliasing=True) # Blur sigma = 3 blurred = skimage.filters.gaussian(small, sigma=sigma, truncate=3.5, multichannel=True) segments = get_borders(small, num_leds, 15) colors = [rgb2int(c) for c in segments[0]] c.segments(*colors) time.sleep(0.035)
img = getScreenAsImage() else: img = getRectAsImage(bbox) return img grab = nt_grab except: pass if grab is None: try: from mss.linux import MSS as mss from PIL import Image log.info('Using mss module') sct = mss() monitor = sct.enum_display_monitors()[0] def mss_grab(bbox): sct.get_pixels(monitor) img = Image.frombytes('RGB', (sct.width, sct.height), sct.image).crop(bbox) return img grab = mss_grab except: try: from pil import ImageGrab log.info("Using PIL ImageGrab module") except: try:
def showimage(): mss().shot() panel = Label(root, text='Screenshot saved to home directory') panel.pack()
if bbox is None: img = getScreenAsImage() else: img = getRectAsImage(bbox) return img grab = nt_grab except Exception: pass if grab is None: try: from mss.linux import MSS as mss from PIL import Image sct = mss() monitor = sct.monitors[0] def mss_grab(bbox): sct_img = sct.grab(monitor) img = Image.frombytes('RGBA', sct_img.size, bytes(sct_img.raw), 'raw', 'BGRA').crop(bbox) img = img.convert('RGB') return img grab = mss_grab log.debug('Using mss module') except Exception: try: from PIL import ImageGrab grab = ImageGrab.grab
def __init__(self, monitor=1, colorspace=None, logging=False, **options): #intialize threaded queue mode self.threaded_queue_mode = True try: # try import necessary system specific mss library import platform if platform.system() == 'Linux': from mss.linux import MSS as mss elif platform.system() == 'Windows': from mss.windows import MSS as mss elif platform.system() == 'Darwin': from mss.darwin import MSS as mss else: from mss import mss #import mss error handler from mss.exception import ScreenShotError except ImportError as error: # otherwise raise import error raise ImportError( 'python-mss library not found, install it with `pip install mss` command.' ) # create mss object self.mss_object = mss() # create monitor instance for the user-defined monitor monitor_instance = None if (monitor > 0): monitor_instance = self.mss_object.monitors[monitor] else: raise ValueError("`monitor` value cannot be negative, Read Docs!") # Initiate User-Defined Threaded Queue Mode if options: if "THREADED_QUEUE_MODE" in options: if isinstance(options["THREADED_QUEUE_MODE"], bool): self.threaded_queue_mode = options[ "THREADED_QUEUE_MODE"] #assigsn special parameter to global variable del options["THREADED_QUEUE_MODE"] #clean #reformat option dict self.queue = None #intialize deque if self.threaded_queue_mode: #import deque from collections import deque #define deque and assign it to global var self.queue = deque(maxlen=96) #max len 96 to check overflow #log it if logging: print('Enabling Threaded Queue Mode!') else: #otherwise disable it self.threaded_queue_mode = False #intiate screen dimension handler screen_dims = {} #initializing colorspace variable self.color_space = None try: #reformat proper mss dict and assign to screen dimension handler screen_dims = { k.strip(): v for k, v in options.items() if k.strip() in ["top", "left", "width", "height"] } # separately handle colorspace value to int conversion if not (colorspace is None): self.color_space = capPropId(colorspace.strip()) except Exception as e: # Catch if any error occurred if logging: print(e) # intialize mss capture instance self.mss_capture_instance = None try: # check whether user-defined dimensions are provided if screen_dims and len(screen_dims) == 4: self.mss_capture_instance = screen_dims #create instance from dimensions else: self.mss_capture_instance = monitor_instance #otherwise create instance from monitor # extract global frame from instance self.frame = np.asanyarray( self.mss_object.grab(self.mss_capture_instance)) if self.threaded_queue_mode: #intitialize and append to queue self.queue.append(self.frame) except ScreenShotError: #otherwise catch and log errors raise ValueError( "ScreenShotError caught: Wrong dimensions passed to python-mss, Kindly Refer Docs!" ) if logging: print(self.mss_object.get_error_details()) # enable logging if specified self.logging = logging # thread initialization self.thread = None # initialize termination flag self.terminate = False