def wait(img_name, timeout = 5, threshold = 30): """ Blocks until image is shown at location or timeout timeout raises pyautogui.exception.FailSafeException move mouse to position (0, 0) to apply FailSafeException at any time img = image bytes object threshold = acceptable difference in image data """ start = time() bbox, img = images[img_name] # Compare screenshot and the image zipped = zip(grab(bbox).tobytes(), img) difference = sum(abs(scrn - img) for scrn, img in zipped) while difference > threshold: sleep(.1) if position() == (0, 0): raise FailSafeException if time() - start > timeout: raise FailSafeException zipped = zip(grab(bbox).tobytes(), img) difference = sum(abs(scrn - img) for scrn, img in zipped) return True
def find_rex(mouse, keyboard): mouse.click(10, 100) time.sleep(0.1) keyboard.tap_key(keyboard.up_key) time.sleep(0.5) img = grab() img = img.filter(ImageFilter.MinFilter(3)) rex_on_ground = np.array(img)[:, :, 0] / 128 time.sleep(0.1) keyboard.tap_key(keyboard.up_key) time.sleep(0.05) img = grab() img = img.filter(ImageFilter.MinFilter(3)) rex_jump = np.array(img)[:, :, 0] / 128 diff = zip(*np.where(rex_on_ground != rex_jump)) km = KMeans(n_clusters=2).fit(diff) centers = [list(s) for s in km.cluster_centers_[:, [1, 0]]] centers.sort() x, y = [int(s) for s in centers[0]] time.sleep(1) radius = 50 rex_pixels = np.array( zip(*np.where( rex_on_ground[y - radius:y + radius, x - radius:x + radius] != 1))) rex_center = rex_pixels.mean(axis=0) dy, dx = [int(round(s - radius)) for s in rex_center] cy = y + dy cx = x + dx w = min(800, rex_on_ground.shape[1] - cx) return cx, cy, w
def grab(self, autocrop=True): img = grab() if sys.platform == "darwin" or sys.platform == "win32": img = grab(xdisplay=self.new_display_var) else: img = grab() if autocrop: img = self.autocrop(img) return img
def TestCompare(self):#test hashing compasions i1 = grab() i2 = i1.copy() print("Change screen") time.sleep(15) i3 = grab() hash1 = imagehash.average_hash(i1) hash2 = imagehash.average_hash(i2) hash3 = imagehash.average_hash(i3) print("image 1 vs image 2") print(hash1 == hash2) print("image 1 vs image 3") print(hash1 == hash3)
def checkend():#checks for the ad that pops up when game finishes to see if its over im = grab() pixels = im.load() if pixels[learnmore] == learnmorecolour: win32api.SetCursorPos(newgame) leftDown() leftUp() while True: time.sleep(1) im = grab() pixels = im.load() if pixels[movelist1] == (255, 255, 255) and \ pixels[movelist2] == (255, 255, 255) and \ pixels[movelist3] == (255, 255, 255): #This checks if a new game started(movelist turns white) break startgame()
def Screenshot(): # grab fullscreen info("Screenshot() taking screenshot") im = grab() # save image file info("Screenshot() saving screenshot") im.save('screen.png')
def barcode(self, startx, starty, dirx, diry, norx, nory, clean=False, color=(0, 0, 0)): barnum = 10 barlen = 14 barpos = -2 if clean == False: im = grab() r, g, b = im.getpixel((startx, starty)) barnum = 9 barlen = 12 barpos = 0 for i in range(barnum): if clean == True: r, g, b = color self.setcolor(r, g, b) else: col = 255 * ((i % 4) // 2) self.setcolor(col, col, col) self.drawline(startx + norx * i + dirx * barpos, starty + nory * i + diry * barpos, startx + norx * i + dirx * barlen, starty + nory * i + diry * barlen) time.sleep(0.1) return r, g, b
def grab(self, autocrop=True): # TODO: use Xvfb fbdir option for screenshot img = grab(xdisplay=self.new_display_var) if autocrop: img = self.autocrop(img) return img
def run(self, export: bool = False): if export: input("[!] Position scene at top left corner of your screen. " "Once you're done, hit 'Enter'.") time.sleep(1) n = 1 for step in range(self.sm.STEPS): if not (step % self.frame_step): if export: time.sleep(0.5) grab(self.scene_box).save( f"{self.sm.png_path}/img{n:06}.png") time.sleep(1 / 24.0) self.render_frame(step) n += 1
def send_image(): sock = socket(AF_INET, SOCK_DGRAM) sock.setsockopt(SOL_SOCKET, SO_BROADCAST, True) IP = '255.255.255.255' cursor = Image.open('./cursor.png') while sending.get(): x, y = pag.position() im = grab() im.paste(cursor, box=(x, y), mask=cursor) w, h = im.size im_bytes = compress(im.tobytes()) sock.sendto(b'start', (IP, 22222)) for i in range(len(im_bytes) // BUFFER_SIZE + 1): start = i * BUFFER_SIZE end = start + BUFFER_SIZE sock.sendto(im_bytes[start:end], (IP, 22222)) sock.sendto(b'_over' + str((w, h)).encode(), (IP, 22222)) sleep(0.0001) sock.sendto(b'close', (IP, 22222)) sock.close()
def side(): #checks to see if your profile pic is on the top(and therefore you are black) im = grab() pixels = im.load() if pixels[profpic] == (profpiccolour): #This is the colour of your profile pic(mine is red) return(0)#black else: return(1)#white
def setscr(self): print('I need more information about your screen size.') print( 'Please use asdw keys to put the mouse near the center of the color wheel, then press enter.' ) x, y = self.setmouse(502 + self.scr_width // 2, 212 + self.scr_height // 2) self.mouse.click(x, y - 15) time.sleep(0.1) im = grab() maxr, maxj, cx, lv = 0, 0, 0, 0 for j in range(-8, 9): data = [sum(im.getpixel((x - 50 + k, y + j))) for k in range(100)] left, right = -1, 0 for k in range(100): if data[k] < 60: if left < 0: left = k elif k - left > 10: right = k break radius = right - left if radius > maxr: maxr = radius maxj = j cx = (left + right + 1) // 2 elif radius == maxr: lv += 1 x += cx - 50 y += maxj + lv // 2 self.mouse.click(x + 15, y) time.sleep(0.1) im = grab() data = [sum(im.getpixel((x, y + j - 40))) for j in range(81)] top, bottom = -1, -1 for k in range(40): if data[40 - k] < 300: if top < 0: top = k if data[40 + k] < 200: if bottom < 0: bottom = k y = y + (bottom - top) // 2 self.mouse.click(x, y) self.wheel_x = x self.wheel_y = y self.radius = (top + bottom) // 2
def screen_grab(): # snapshot of screen im = grab() image_name = os.getcwd() + r'\boi.jpg' # saves in current work directory with name based on time of pic im.save(image_name) with open(image_name, 'rb') as image: print len(image.read())
def get_image(bbox=(600, 100, 900, 500)): """ Returns a screenshot of the given area, By default returns a screenshot of the fingerprint """ # Starting Point (600, 100, 900, 500) img = array(grab(bbox=bbox)) return cvtColor(img, COLOR_BGR2GRAY)
def oppmove(): im = grab(board) pixels = im.load() isx = im.size[0] isy = im.size[1] io = oppmovein(pixels, isy, isx) fo = oppmovefin(pixels, io, isy, isx) return str(io[0]) + str(io[1]) + \ str(fo[0]) + str(fo[1])
def setscr(self): print('I need more information about your screen size.') print('Please move your mouse near the center of the color wheel, then press enter.') x,y = self.setmouse(502 + self.scr_width//2,212 + self.scr_height//2) self.mouse.click(x,y-15) time.sleep(0.1) im=grab() maxr, maxj, cx, lv = 0, 0, 0, 0 for j in range(-8,9): data=[sum(im.getpixel((x-50+k,y+j))) for k in range(100)] left, right = -1,0 for k in range(100): if data[k]<60: if left<0: left=k elif k-left>10: right=k break radius = right-left if radius>maxr: maxr=radius maxj = j cx=(left+right+1)//2 elif radius==maxr: lv+=1 x += cx-50 y += maxj+lv//2 self.mouse.click(x+15,y) time.sleep(0.1) im=grab() data=[sum(im.getpixel((x,y+j-40))) for j in range(81)] top,bottom=-1,-1 for k in range(40): if data[40-k]<300: if top<0: top=k if data[40+k]<200: if bottom<0: bottom=k y = y+(bottom-top)//2 self.mouse.click(x,y) self.wheel_x = x self.wheel_y = y self.radius = (top+bottom)//2
def getScreen(self): screen = grab() image = cvtColor(np.array(screen), COLOR_BGR2RGB) # Resize to 50 percent of original dimensions width = int(image.shape[1] * 50 / 100) height = int(image.shape[0] * 50 / 100) imageScale = resize(image, (width, height)) return imageScale
def drift(self, dir, t, x0, y0, vec1, vec2): self.shift(dir,t) time.sleep(3) self.mouse.move(1,100) im = grab() data = [sum(im.getpixel((x0+k*vec1, y0+k*vec2))[:3]) for k in range(409)] for k in range(401): if data[k] < 100: if max(data[k+1:k+3])>600 and min(data[k+2:k+5])<150 and max(data[k+3:k+6])>600 and min(data[k+4:k+7])<150: return k return -1
def turn(): #check timer to see if it's red, and therefore who's turn time.sleep(0.03)#don't get ahead of yourself r = 0 count = 0 image = grab(timer).load() for s in range(0, 12): for t in range(0, 12): pixlr = (image[s, t])[0] r += pixlr count += 1 if (r/count) >253: return(0)#black else: return(1)#white
def get_press(key): bef = time.time() img = np.asarray(grab()) gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) width_, height_ = int(gray.shape[1] * 0.25), int(gray.shape[0] * 0.25) resized = cv2.resize(gray, (width_, height_)) key = str(key).strip("'") al_1 = random.choice(alphabets) al_2 = random.choice(alphabets) num_1 = random.randint(0, 10000) num_2 = random.randint(0, 10000) if key in char_set: cv2.imwrite(f'{path}{key}_{al_1}_{al_2}_{num_1}_{num_2}.jpg', resized) else: pass aft = time.time() print(f'{key} Pressed, processing speed = {aft-bef}')
def scan_account_id(): """ Scan screenshot area (1290, 197, 1380, 206) for an account id """ screen = grab((1290, 197, 1380, 206)).tobytes().hex() # Vertical unique slice of number image to match numbers = [ '000000000000474747878787999999929292787878000000000000', '000000b0b0b0939393000000000000000000000000000000000000', '0000005555553f3f3f000000000000000000000000000000828282', '0000002f2f2f000000000000000000000000000000000000262626', '000000000000000000000000000000919191939393000000000000', '0000000000000000000000000000000000000000004d4d4d676767', '0000000000000000000000007d7d7da0a0a0a3a3a3757575000000', '717171808080000000000000000000000000000000000000474747', '0000000000005b5b5b0000000000003f3f3f909090737373000000', '0000003535359a9a9aa4a4a46d6d6d000000000000000000000000' ] # Function to split a list into evenly sized chunks chunk = lambda lst, n: [lst[i:i + n] for i in range(0, len(lst), n)] # rows of columns = screen_slices[row][column] rows_of_columns = [chunk(row, 6) for row in chunk(screen, 540)] # Transpose to vertical slices = screen_slices[column][row] screen_slices = ["".join(b) for b in zip(*rows_of_columns)] account_id = [] while set(screen_slices) & set(numbers): # Get first match num = (set(screen_slices) & set(numbers)).pop() # Get horizontal position of match in screen_slice slice_index = screen_slices.index(num) account_id.append((slice_index, str(numbers.index(num)))) # Change screen_slices to avoid duplicates screen_slices[slice_index] = 0 # Return account id sorted by slice_index account_id = sorted(account_id, key = lambda k: k[0]) return "".join(data[1] for data in account_id)
def drift(self, dir, t, x0, y0, vec1, vec2): self.shift(dir, t) time.sleep(3) self.mouse.move(1, 100) im = grab() data = [ sum(im.getpixel((x0 + k * vec1, y0 + k * vec2))[:3]) for k in range(409) ] for k in range(401): if data[k] < 100: if max(data[k + 1:k + 3]) > 600 and min( data[k + 2:k + 5]) < 150 and max( data[k + 3:k + 6]) > 600 and min( data[k + 4:k + 7]) < 150: return k return -1
def barcode(self, startx, starty, dirx, diry, norx, nory, clean=False, color=(0,0,0)): barnum = 10 barlen = 15 if clean == False: im=grab() r,g,b=im.getpixel((startx,starty)) barnum = 9 barlen = 12 for i in range(barnum): if clean==True: r,g,b=color self.setcolor(r,g,b) else: col = 255*((i%4)//2) self.setcolor(col,col,col) self.drawline(startx+norx*i, starty+nory*i, startx+norx*i+dirx*barlen, starty+nory*i+diry*barlen) time.sleep(0.1) return r,g,b
def recordVid(self, tLeng, output): ''' Records screen and resizes to an array to self.vid over a specified time in seconds ''' start = time.clock() L=[] vid = [] for i in range(tLeng): time.sleep(1) l = grab() l= spmisc.imresize(l, .075, 'nearest') L.append([plt.imshow(l)]) s = l.shape[0]*l.shape[1]*l.shape[2] lp=np.reshape(l, (1,s)) vid.append(lp) dt = time.clock() - start output.put([vid,L,dt]) print "viddone" output.task_done()
def autoGAL(): if Auto.get() == 1: global Click_x, Click_y, Release_x, Release_y, LastImageValue, GALResultText, TranslationSetting # NowImage = grab((Click_x, Click_y, Release_x, Release_y)) NowImage = grab( (Click_x, Click_y, Release_x + 1, Release_y + 1)) #一个像素差 NowImage.save('important/NowImage.jpg') with open('important/NowImage.jpg', 'rb+') as f: NowImageValue = f.read() if NowImageValue != LastImageValue: # 区域不一样,继续OCR OCRText = OCR_Core(NowImage) if not OCRText: return False GALResultText.delete(1.0, "end") GALResultText.insert( "end", eval(TransAll[GALTranslationSetting.get()] + """('''""" + OCRText + """''')""")) GALResultText.after(TimeInterval, autoGAL) else: GALResultText.after_cancel(1)
def login(self, account_no: str = '', password: str = '', comm_pwd: str = '', client_path: str = ''): self.run(client_path) print('{} 正在登录交易服务器...'.format(util.curr_time())) from PIL.ImageGrab import grab while True: time.sleep(.5) if user32.GetForegroundWindow() == self.h_login: # 模拟键盘输入 util.keyboard.send(util.keyboard.KEY_UP) info = (account_no, password, comm_pwd or util.image_to_string( grab(util.get_rect(self._IMG)))) for text in info: util.write(text) time.sleep(0.5) util.keyboard.send('\r') break user32.SetForegroundWindow(self.h_login) # self.capture() if self.visible(times=20): self.account_no = account_no self.password = password self.comm_pwd = comm_pwd self.mkt = (0, 1) if util.get_text(self.get_handle('mkt')).startswith( '上海') else (1, 0) print('{} 已登入交易服务器。'.format(util.curr_time())) self.init() return {'puppet': "{} 木偶准备就绪!".format(util.curr_time())} # 兼容广发证券 util.keyboard.send('\r') if util.wait_for_view(self.root, 9): self.init() return {'puppet': '{} 登录失败或服务器失联'.format(util.curr_time())}
def find_obstacle(cx, cy, w): img = np.array(grab())[cy - 50:cy + 50, cx:cx + w, 0] / 128 if img.sum() * 2 > w * 100: img = 1 - img hist = img.sum(axis=0) obstacles = [] owner = 'rex' count = 0 for i in xrange(w): s = hist[i] if s > 10: count = 0 if owner == 'desert': owner = 'obstacle' left = i elif owner != 'desert': count += 1 if count >= 15: count = 0 if owner == 'obstacle': obstacles.append([left, i - count, 100]) owner = 'desert' for obs in obstacles: left, right = obs[:2] h_hist = img[:, left:right].sum(axis=1) count = 0 for i in xrange(100 - 1, -1, -1): if h_hist[i] > 5: if count > 5: height = 100 - i + count obs[2] = height break count += 1 else: count = 0 return obstacles
def pix(self): return PixelData(grab(self.bbox)) @property
# another way to do the screen cap loop: t_start = time() t_stop = t_start # start index at last numbered image in data folder n_img = int(max(glob(dpath + '*')[:-8:-4])[-8:-4]) if glob(dpath + '*') else 0 n_init = n_img while t_stop - t_start < TIME: # update progress message n_img += 1 stdout.write("\r") stdout.write("Capturing Image: {:<5}".format(n_img)) stdout.flush() # capture screenshot if FORMAT == 'jpg': grab().convert("RGB").save(dpath + 'img_{:0=4d}.jpg'.format(n_img)) else: grab().save(dpath + 'img_{:0=4d}.png'.format(n_img)) # enforce a maximum save rate if time() - t_stop < INTERVAL: sleep(INTERVAL) t_stop = time() print("Screen Capture Complete. {} files saved to: {}".format( n_img - n_init, expanduser(getcwd()) + '/' + dpath)) # ORIGINAL SAVE LOOP ########################################################### # # loop {time/interval} time steps # trange = int(TIME / INTERVAL) # for tstep in range(trange): # # update progress message # "\r" returns cursur to beginning of line.
def setscr(self): print('I need more information about your screen size.') print('Please use asdw keys to put the mouse near the center of the color wheel, then press enter.') x,y = self.setmouse(502 + self.scr_width//2,212 + self.scr_height//2) self.mouse.click(x,y-15) time.sleep(0.1) im=grab() maxr, maxj, cx, lv = 0, 0, 0, 0 for j in range(-8,9): data=[sum(im.getpixel((x-50+k,y+j))) for k in range(100)] left, right = -1,0 for k in range(100): if data[k]<60: if left<0: left=k elif k-left>10: right=k break radius = right-left if radius>maxr: maxr=radius maxj = j cx=(left+right+1)//2 elif radius==maxr: lv+=1 x += cx-50 y += maxj+lv//2 self.mouse.click(x+15,y) time.sleep(0.1) im=grab() data=[sum(im.getpixel((x,y+j-40))) for j in range(81)] top,bottom=-1,-1 for k in range(40): if data[40-k]<300: if top<0: top=k if data[40+k]<200: if bottom<0: bottom=k y = y+(bottom-top)//2 self.mouse.click(x,y) self.wheel_x = x self.wheel_y = y self.radius = (top+bottom)//2 print('Please set pen size to 1 and choose a color that is not on the ball.') print('Please use asdw keys to put the mouse near the center of the big ball, then press enter, I will help you to find the center.') x=self.center_x y=self.center_y m = self.mouse while True: c=getch().decode() if c=='a': x-=1 elif c=='d': x+=1 elif c=='w': y-=1 elif c=='s': y+=1 elif c=='\r' or c=='\n': r=320 self.setcolor(0,0,0) self.polar(lambda x: 300, cx=x, cy=y) self.polar(lambda x: 320, cx=x, cy=y) self.drawline(x-r,y,x-r+20,y) self.drawline(x+r-20,y,x+r,y) self.drawline(x,y-r+20,x,y-r) self.drawline(x,y+r-20,x,y+r) self.drawline(x-20,y,x+20,y) self.drawline(x,y-20,x,y+20) self.mouse.move(x,y) print('Is the pen in the center of the ball now(y/n)?') c=getch().decode() if c=='y': break elif c=='n': print('Please use asdw keys to put the mouse near the center of the big ball, then press enter, I will help you to find the center.') continue self.center_x = x self.center_y = y
def setmouse(self): print('I need more information about your screen size') print('Please use asdw keys to put the mouse near the center of the big ball, then press enter.') x=self.center_x y=self.center_y while True: c=getch().decode() if c=='a': x-=1 elif c=='d': x+=1 elif c=='w': y-=1 elif c=='s': y+=1 elif c=='\r' or c=='\n': break self.mouse.move(x,y) self.center_x = x self.center_y = y x = 502 + self.scr_width//2 y = 212 + self.scr_height//2 print('Please use asdw keys to put the mouse near the center of the color wheel, then press enter.') while True: c=getch().decode() if c=='a': x-=1 elif c=='d': x+=1 elif c=='w': y-=1 elif c=='s': y+=1 elif c=='\r' or c=='\n': break self.mouse.move(x,y) self.mouse.click(x,y-15) time.sleep(0.1) im=grab() maxr = 0 maxj = 0 cx = 0 lv=0 for j in range(-8,9): data=[sum(im.getpixel((x-50+k,y+j))) for k in range(100)] left, right = -1,0 for k in range(100): if data[k]<60: if left<0: left=k elif k-left>10: right=k break radius = right-left if radius>maxr: maxr=radius maxj = j cx=(left+right+1)//2 elif radius==maxr: lv+=1 x += cx-50 y += maxj+lv//2 self.mouse.click(x+15,y) time.sleep(0.1) im=grab() data=[sum(im.getpixel((x,y+j-40))) for j in range(81)] top,bottom=-1,-1 for k in range(40): if data[40-k]<300: if top<0: top=k if data[40+k]<200: if bottom<0: bottom=k y = y+(bottom-top)//2 self.mouse.click(x,y) radius = (top+bottom)//2 return x,y,radius
def screenGrab(): box = () im = grab() im.save(os.getcwd() + '\\full_snap__' + str(int(time.time())) + '.png', 'PNG')
crop_boundary = None EnumWindows(enum_callback, []) if not crop_boundary: print("Couldn't find HLMV, is it open with a model loaded?") exit() else: print("Auto-computed crop boundary:", crop_boundary) white_images = [] model.set_background(False) # Loops in this order to get the images in the right order. for y_rot in xrange(0, 360, 360//number_of_images): for x_rot in xrange(-15*vertical_rotations, 15*vertical_rotations+1, 15): model.rotate(x_rot, y_rot) sleep(0.02) # Wait for redraw white_images.append(grab().crop(crop_boundary)) black_images = [] model.set_background(True) for y_rot in xrange(0, 360, 360//number_of_images): for x_rot in xrange(-15*vertical_rotations, 15*vertical_rotations+1, 15): model.rotate(x_rot, y_rot) sleep(0.02) # Wait for redraw black_images.append(grab().crop(crop_boundary)) model.rotate(0, 0) # Reset back to starting rotation for user print('Blending...' + ' '*(len(white_images) - 12) + '|') for (white_image, black_image) in zip(white_images, black_images): print('#', end='') ip.blend(white_image, black_image) print('')
def __init__(self): from PIL.ImageGrab import grab self.image = grab()
stopy=img2y-img1.shape[0]+1 stopx=img2x-img1.shape[1]+1 for x1 in range(0,stopx): for y1 in range(0,stopy): x2=x1+img1.shape[1] y2=y1+img1.shape[0] pic=img2[y1:y2,x1:x2] test=pic==img1 if test.all(): print (x1, y1) return x1, y1 print('board not found') sys.exit boardcheck = (0,0,170,250) #top left corner, up to the bottom right of first square bx, by = boardcords(Image.open('small.png'),grab(boardcheck)) #bx2, by2 board = (bx,by,bx+543,by+543) #exact board dimensions(no black lines) #coords newgame = (bx+208, by+470) #the new 1 min game button in the popup(rightside) learnmore = (bx+186, by+380) #The learn more button movelist1 = (bx+818, by+15) #to see when game is found(the move list, each row) movelist2 = (bx+818, by+35) movelist3 = (bx+818, by+55) profpic = (bx+5, by-20) #location of profile pic #colours learnmorecolour = (255, 102, 0) #the learn more button colour profpiccolour = (255, 35, 28) #colour of profile pic(should be solid) movelight = (247, 236, 116) #light square after moving piece movedark = (218, 195, 74) #dark square after moving piece #coords for pics
def setmouse(self): print('I need more information about your screen size') print( 'Please use asdw keys to put the mouse near the center of the big ball, then press enter.' ) x = self.center_x y = self.center_y while True: c = getch().decode() if c == 'a': x -= 1 elif c == 'd': x += 1 elif c == 'w': y -= 1 elif c == 's': y += 1 elif c == '\r' or c == '\n': break self.mouse.move(x, y) self.center_x = x self.center_y = y x = 502 + self.scr_width // 2 y = 212 + self.scr_height // 2 print( 'Please use asdw keys to put the mouse near the center of the color wheel, then press enter.' ) while True: c = getch().decode() if c == 'a': x -= 1 elif c == 'd': x += 1 elif c == 'w': y -= 1 elif c == 's': y += 1 elif c == '\r' or c == '\n': break self.mouse.move(x, y) self.mouse.click(x, y - 15) time.sleep(0.1) im = grab() maxr = 0 maxj = 0 cx = 0 lv = 0 for j in range(-8, 9): data = [sum(im.getpixel((x - 50 + k, y + j))) for k in range(100)] left, right = -1, 0 for k in range(100): if data[k] < 60: if left < 0: left = k elif k - left > 10: right = k break radius = right - left if radius > maxr: maxr = radius maxj = j cx = (left + right + 1) // 2 elif radius == maxr: lv += 1 x += cx - 50 y += maxj + lv // 2 self.mouse.click(x + 15, y) time.sleep(0.1) im = grab() data = [sum(im.getpixel((x, y + j - 40))) for j in range(81)] top, bottom = -1, -1 for k in range(40): if data[40 - k] < 300: if top < 0: top = k if data[40 + k] < 200: if bottom < 0: bottom = k y = y + (bottom - top) // 2 self.mouse.click(x, y) radius = (top + bottom) // 2 return x, y, radius
def capture(name): image = grab() image.save("photos/" + name + ".png") Beep(200, 100) sleep(0.1)
def sendThread(self): while True: img = grab((0, 0, WIDTH, HEIGHT)) img = img.resize((1024, 768)) data = pickle.dumps(img) self.send(data)
def autoplot(self,mode=1): a,b = self.center_x, self.center_y target = [(250,15,225)] if mode>0: self.mouse.move(1,100) time.sleep(2) im=grab() bad_count=0 pix=[[0 for i in range(14)]for j in range(14)] tot=0 for j in range(210): for i in range(210): x=a-210+i*2 y=b-210+j*2 r0,g0,b0 = im.getpixel((x,y)) for colors in target: r1,g1,b1=colors if max(abs(r0-r1),abs(g0-g1),abs(b0-b1))<40: pix[j//15][i//15]=1 tot+=1 break if tot<4000: return 0 find_square=0 for sizex in range(13,4,-1): for sizey in range(13,4,-1): for y in range(14-sizey): for x in range(14-sizex): gridcount=sum([sum(pix[y+k][x:x+sizex]) for k in range(sizey)]) if gridcount==sizex*sizey: find_square=1 cx = a-210+15*(2*x+sizex+1) cy = b-210+15*(2*y+sizey+1) radiusx = 15*sizex - 30 radiusy = 15*sizey - 30 break if find_square==1: break if find_square==1: break if find_square==1: break if find_square==0: return 0 n=random.randint(4,8) theta=random.randint(0,360) if mode<0: cx,cy,radius=a,b,-mode if mode==0: self.setcolor(random.randint(0,255),random.randint(0,255),random.randint(0,255)) self.typeletter('l',cx-radiusx,cy+radiusy,2*radiusx/3,2*radiusy) self.typeletter('o',cx-radiusx+1.5*radiusx/3,cy+radiusy,2.5*radiusx/3,2*radiusy) self.typeletter('l',cx-radiusx+4*radiusx/3,cy+radiusy,2*radiusx/3,2*radiusy) #for i in range(2*n+1): # angle=math.pi*theta/180 # x0=cx+int(radius*math.cos(angle)) # y0=cy+int(radius*math.sin(angle)) # theta+=360*n/(2*n+1) # angle=math.pi*theta/180 # x1=cx+int(radius*math.cos(angle)) # y1=cy+int(radius*math.sin(angle)) # self.drawline(x0,y0,x1,y1) return 1
def scan_screen(): """ Get QR code from screen """ _decode(grab())
def pil(self): return grab(self.bbox) @property
def autoplot(self, mode=1, rtl=1, rtu=1, s='', speed=7): a, b = self.center_x, self.center_y target = [(250, 15, 225), (250, 0, 0)] if mode > 0: self.mouse.move(1, 100) time.sleep(2) im = grab() bad_count = 0 pix = [[0 for i in range(14)] for j in range(14)] aux = [[[0, 0] for i in range(14)] for j in range(14)] tot = 0 for j in range(210): for i in range(210): x = a - 210 + i * 2 y = b - 210 + j * 2 r0, g0, b0 = im.getpixel((x, y)) for colors in target: r1, g1, b1 = colors if max(abs(r0 - r1), abs(g0 - g1), abs(b0 - b1)) < 40: pix[j // 15][i // 15] = 1 tot += 1 break if tot < 4000: return 0 maxsize = 0 bestx = 0 besty = 0 aux[0][0] = [pix[0][0], pix[0][0]] for j in range(1, 14): if pix[0][j] == 1: aux[0][j][1] = 1 aux[0][j][0] = aux[0][j - 1][0] + 1 if pix[j][0] == 1: aux[j][0][0] = 1 aux[j][0][1] = aux[j - 1][0][1] + 1 if mode <= 1: c1, c2 = 0, 1 else: c1, c2 = 1, 0 for j in range(1, 14): for i in range(1, 14): if pix[j][i] == 1: aux[j][i][0] = min(aux[j - 1][i][0], aux[j][i - 1][0], aux[j - 1][i - 1][0]) + 1 aux[j][i][1] = min(aux[j - 1][i][1], aux[j][i - 1][1], aux[j - 1][i - 1][1]) + 1 sz = c1 * aux[j][i][0] * aux[j][i][1] + c2 * min( aux[j][i]) if sz > maxsize: maxsize = sz bestx = i besty = j width, height = aux[besty][bestx] if width <= 4 or height <= 4: return 0 lx = bestx - width + 1 ly = besty - height + 1 cx = a - 210 + 15 * (2 * lx + width) cy = b - 210 + 15 * (2 * ly + height) radiusx = 15 * width - 30 radiusy = 15 * height - 30 n = random.randint(4, 8) theta = random.randint(0, 360) if mode < 0: cx, cy, radius = a, b, -mode if mode == 0 or mode == 2: self.setcolor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) if mode == 0 or mode == 1: radius = min(radiusx, radiusy) for i in range(2 * n + 1): angle = math.pi * theta / 180 x0 = cx + int(radius * math.cos(angle)) y0 = cy + int(radius * math.sin(angle)) theta += 360 * n / (2 * n + 1) angle = math.pi * theta / 180 x1 = cx + int(radius * math.cos(angle)) y1 = cy + int(radius * math.sin(angle)) self.drawline(x0, y0, x1, y1) elif mode == 2 or mode == 3: ln = len(s) rt = radiusx / radiusy if 3 * rt < ln: self.Hershey(s, cx - radiusx, cy + radiusy, 2 * radiusx, radiusy / 1.2, speed) self.Hershey(s, cx - radiusx, cy, 2 * radiusx, radiusy / 1.2, speed) elif rt > ln * 3: self.Hershey(s, cx - radiusx, cy + radiusy, radiusx / 1.2, 2 * radiusy, speed) self.Hershey(s, cx, cy + radiusy, radiusx / 1.2, 2 * radiusy, speed) else: self.Hershey(s, cx - radiusx, cy + radiusy, 2 * radiusx, 2 * radiusy, speed) return 1
"screenshot", "take picture", "take screenshot", "picture", "print screen" ] while True: recogniser = speech.Recognizer() with speech.Microphone() as src: print("Please input:") audio = recogniser.listen(src) try: said = recogniser.recognize_google(audio) if (said in words): print("Screenshotting...") content = grab() filename = asksaveasfilename(filetypes=typesallowed, defaultextension=typesallowed, initialdir="Downloads", title="Save screenshot") if (filename): try: content.save(filename) print("Saved as: {0}".format(filename)) except ValueError: print("Unable to save with that file extension.") else: print("Chosen not to save file.")
def grabArea(self,bbox):# turns a defined area into (x0-x1,y0-y1) into an image return grab(bbox)
def Screenshot(): # grab fullscreen im = grab() # save image file im.save('screen.png')
def automateDis( key, numberOfImages=24, n=0, rotationOffset=None, initialRotation=None, initialTranslation=None, verticalOffset=None, verticalRotations=1, screenshotPause=False, teamColours=False, pathToHlmv='', itemName='', REDVMTFiles=None, BLUVMTFiles=None): """ Method to automize process of taking images for 3D model views. Parameters: key -> (REQUIRED) The registry key for the model numberOfImages -> Number of images to take per one full rotation. n -> Offset the rotation from this image number. rotationOffset -> Offset the center of rotation horizontally verticalOffset -> Offset the center of rotation vertically verticalRotations -> Set to 0 to disable vertical rotations. screenshotPause -> Pause on each screenshot. NUMLOCK will continue. pathToHlmv -> Path to hlmv.exe. Usually in common\\Team Fortress 2\\bin itemName -> The name of the item, as will be saved to disk and uploaded REDVMTFiles -> A list of RED vmt file locations. BLUVMTFiles -> A list of BLU vmt file locations. """ outputFolder = outputImagesDir + sep + itemName try: makedirs(outputFolder) except WindowsError: answer = raw_input('Folder "%s" already exists, overwrite files? (y\\n) ' % itemName) if answer.lower() in ['no', 'n']: import sys sys.exit(1) # Time for user to cancel script start sleep(3) # Close HLMV, in case it's already open. Suppress all responses. model = HLMVModelRegistryKey(key, rotation=initialRotation, translation=initialTranslation) Popen(['taskkill', '/f', '/t', '/im', 'hlmv.exe'], stderr=PIPE, stdout=PIPE) sleep(2.0) print 'initialTranslation =', initialTranslation print 'initialRotation =', initialRotation # Adjust model rotation as needed if rotationOffset: model.rot_offset = rotationOffset if verticalOffset: model.vert_offset = verticalOffset # Create the image processors, used for blending, cropping, and stitching if teamColours: ipRed = imageProcessor(suffix='RED') ipBlu = imageProcessor(suffix='BLU') else: ip = imageProcessor() for y in range(n, numberOfImages): yrotation = (360/numberOfImages)*y print 'n =', n for xrotation in range(15, -30, -15): if (verticalRotations == 0 and xrotation == 0) or verticalRotations == 1: # Set rotation sleep(0.5) model.rotate(xrotation, yrotation) # Open HLMV Popen([pathToHlmv + sep + 'hlmv.exe', '-game', pathToHlmv[:-4]+'\\tf\\']) sleep(2) # Focus and maximise HLMV def enum_callback(hwnd, _): if GetWindowText(hwnd)[:22] == 'Half-Life Model Viewer': SetForegroundWindow(hwnd) ShowWindow(hwnd, win32con.SW_MAXIMIZE) EnumWindows(enum_callback, []) # Open most recent model x, y = fileButtonCoordindates SetCursorPos((x, y)) mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0) mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0) SendKeys(r'{UP 2}{RIGHT}{ENTER}') sleep(1) # If user wants to pose model before taking screenshot, make script wait if screenshotPause: numKeyState = GetKeyState(win32con.VK_NUMLOCK) while GetKeyState(win32con.VK_NUMLOCK) == numKeyState: pass global threads if teamColours: # Take two (red) images, on one black and one on white, # and blends them together to find transparency imgWhiteBG = grab().crop(imgCropBoundaries) SendKeys(r'^b') imgBlackBG = grab().crop(imgCropBoundaries) SendKeys(r'^b') thread = Thread(target=ipRed.blend, kwargs={ 'blackImg': imgBlackBG, 'whiteImg': imgWhiteBG, 'name': '%s\\%d_%d_RED.png' % (outputFolder, n, xrotation / -15) }) threads.append(thread) thread.start() # Swap the red and blue .vmts to change the weapon's colour redFiles = [open(f, 'rb').read() for f in REDVMTFiles] for bluFileName, redFileName in zip(BLUVMTFiles, REDVMTFiles): with open(redFileName, 'wb') as redFile, open(bluFileName, 'rb') as bluFile: redFile.write(bluFile.read()) SendKeys(r'{F5}') sleep(1.0) # Take two (blue) images and blend them together imgWhiteBG = grab().crop(imgCropBoundaries) SendKeys(r'^b') imgBlackBG = grab().crop(imgCropBoundaries) SendKeys(r'^b') thread = Thread(target=ipBlu.blend, kwargs={ 'blackImg': imgBlackBG, 'whiteImg': imgWhiteBG, 'name': '%s\\%d_%d_BLU.png' % (outputFolder, n, xrotation / -15) }) threads.append(thread) thread.start() # Swap the item back to red for redFileName, redFileContents in zip(REDVMTFiles, redFiles): with open(redFileName, 'wb') as redFile: redFile.write(redFileContents) else: # Take two images, on one black and one on white, blend them together to find transparency imgWhiteBG = grab().crop(imgCropBoundaries) SendKeys(r'^b') imgBlackBG = grab().crop(imgCropBoundaries) SendKeys(r'^b') thread = Thread(target=ip.blend, kwargs={ 'blackImg': imgBlackBG, 'whiteImg': imgWhiteBG, 'name': '%s\\%d_%d.png' % (outputFolder, n, xrotation / -15) }) threads.append(thread) thread.start() # Close HLMV, supress success message Popen(['taskkill', '/f', '/t', '/im', 'hlmv.exe'], stdout=PIPE) # Check for kill switch if GetKeyState(win32con.VK_CAPITAL) in [1, -127]: print 'Successfully terminated' import sys sys.exit(0) n += 1 for thread in threads: thread.join() # Wait for threads to finish, if any print 'Stitching images together...' if teamColours: ipRed.stitch(outputFolder + sep + itemName + ' RED 3D.jpg', n, verticalRotations) ipBlu.stitch(outputFolder + sep + itemName + ' BLU 3D.jpg', n, verticalRotations) else: ip.stitch(outputFolder + sep + itemName + ' 3D.jpg', n, verticalRotations) # Upload images to wiki if teamColours: uploadFile(outputFolder, itemName + ' RED 3D.jpg') uploadFile(outputFolder, itemName + ' BLU 3D.jpg') else: uploadFile(outputFolder, itemName + ' 3D.jpg') # All done yay print '\nAll done'
def drift(self, dir, sz=2): cx,cy=self.center_x,self.center_y if dir=='right': dx,dy=1,0 elif dir=='left': dx,dy=-1,0 elif dir=='down': dx,dy=0,1 else: dx,dy=0,-1 self.mouse.move(1,100) time.sleep(0.3) im=grab() if dir=='right' or dir=='left': background=[im.getpixel((cx+(k-250)*dx,cy+120)) for k in range(500)] else: background=[im.getpixel((cx+120,cy+(k-250)*dy)) for k in range(500)] rc,gc,bc=im.getpixel((cx+205*dx,cy+205*dy)) n=2 while True: w=256//n colrec=[0]*n*n*n for r,g,b in background: ind=(r//w)*n*n + (g//w)*n + b//w colrec[ind]=1 for k in range(n*n*n): if colrec[k]==0: break if colrec[k]==0: break n*=2 r1=(k//(n*n))*w+w//2 g1=((k//n)%n)*w+w//2 b1=(k%n)*w+w//2 r0,g0,b0=background[455] self.setcolor(r1,g1,b1) wd=max(3-sz,0) for i in range(-wd,wd+1): if dir=='left' or dir=='right': self.drawline(cx+205*dx+i,cy+120-wd,cx+205*dx+i,cy+120+wd) else: self.drawline(cx+120-wd,cy+205*dy+i,cx+120+wd,cy+205*dy+i) for i in range(9): self.setcolor(255*((i%4)//2),255*((i%4)//2),255*((i%4)//2)) if dir=='left' or dir=='right': self.drawline(cx+(225+i)*dx,cy-5,cx+(225+i)*dx,cy+5) else: self.drawline(cx-5,cy+(225+i)*dy,cx+5,cy+(225+i)*dy) time.sleep(0.5) self.shift(dir,1) time.sleep(1) while True: time.sleep(2) self.mouse.move(1,100) time.sleep(0.5) im = grab() if dir=='right' or dir=='left': bg2=[im.getpixel((cx+(k-250)*dx,cy+120)) for k in range(500)] else: bg2=[im.getpixel((cx+120,cy+(k-250)*dy)) for k in range(500)] pl,pr=-1,0 for k in range(500): r,g,b=bg2[k] if max(abs(r-r1),abs(g-g1),abs(b-b1)) <= w//3: if pl==-1: pl=k pr=k else: if pl!=-1: break if dir=='right' or dir=='down': k=(pl+pr)//2+1 else: k=(pl+pr)//2-1+(pl+pr)%2 if pl==-1 or pr>465: print('Lost target') print(bg2) print(r1,g1,b1,w) a,b=self.setmouse(cx,cy) self.mouse.click(a,b) k=160 if k>350: self.shift(dir,0.7) elif k>250: self.shift(dir,0.6) elif k>150: self.shift(dir,0.5) else: break self.setcolor(r0,g0,b0) for i in range(-wd,wd+1): if dir=='left' or dir=='right': self.drawline(cx+(k-250)*dx+i,cy+120-wd,cx+(k-250)*dx+i,cy+120+wd) else: self.drawline(cx+120-wd,cy+(k-250)*dy+i,cx+120+wd,cy+(k-250)*dy+i) self.setcolor(rc,gc,bc) for i in range(11): if dir=='left' or dir=='right': self.drawline(cx+(k-231+i)*dx,cy-6,cx+(k-231+i)*dx,cy+6) else: self.drawline(cx-6,cy+(k-231+i)*dy,cx+6,cy+(k-231+i)*dy) return k-55
def autoplot(self,target,mode=1,rtl=1,rtu=1,s='',speed=7): a,b = self.center_x, self.center_y if mode>0: self.mouse.move(1,100) time.sleep(2) im=grab() bad_count=0 pix=[[0 for i in range(14)]for j in range(14)] aux=[[[0,0] for i in range(14)]for j in range(14)] tot=0 for j in range(210): for i in range(210): x=a-210+i*2 y=b-210+j*2 r0,g0,b0 = im.getpixel((x,y)) for colors in target: r1,g1,b1=colors if max(abs(r0-r1),abs(g0-g1),abs(b0-b1))<40: pix[j//15][i//15]=1 tot+=1 break if tot<4000: return 0 maxsize=0 bestx=0 besty=0 aux[0][0]=[pix[0][0],pix[0][0]] for j in range(1,14): if pix[0][j]==1: aux[0][j][1]=1 aux[0][j][0]=aux[0][j-1][0]+1 if pix[j][0]==1: aux[j][0][0]=1 aux[j][0][1]=aux[j-1][0][1]+1 if mode<=1: c1,c2=0,1 else: c1,c2=1,0 for j in range(1,14): for i in range(1,14): if pix[j][i]==1: aux[j][i][0]=min(aux[j-1][i][0],aux[j][i-1][0],aux[j-1][i-1][0])+1 aux[j][i][1]=min(aux[j-1][i][1],aux[j][i-1][1],aux[j-1][i-1][1])+1 sz=c1*aux[j][i][0]*aux[j][i][1]+c2*min(aux[j][i]) if sz>maxsize: maxsize=sz bestx=i besty=j width,height=aux[besty][bestx] if width<=4 or height<=4: return 0 lx=bestx-width+1 ly=besty-height+1 cx = a-210+15*(2*lx+width) cy = b-210+15*(2*ly+height) radiusx = 15*width - 30 radiusy = 15*height - 30 n=random.randint(4,8) theta=random.randint(0,360) if mode<0: cx,cy,radius=a,b,-mode if mode==0 or mode==2: self.setcolor(random.randint(0,255),random.randint(0,255),random.randint(0,255)) if mode==0 or mode==1: radius=min(radiusx,radiusy) for i in range(2*n+1): angle=math.pi*theta/180 x0=cx+int(radius*math.cos(angle)) y0=cy+int(radius*math.sin(angle)) theta+=360*n/(2*n+1) angle=math.pi*theta/180 x1=cx+int(radius*math.cos(angle)) y1=cy+int(radius*math.sin(angle)) self.drawline(x0,y0,x1,y1) elif mode==2 or mode==3: ln=len(s) rt=radiusx/radiusy if 3*rt<ln: self.Hershey(s,cx-radiusx,cy+radiusy,2*radiusx,radiusy/1.2,speed) self.Hershey(s,cx-radiusx,cy,2*radiusx,radiusy/1.2,speed) elif rt>ln*3: self.Hershey(s,cx-radiusx,cy+radiusy,radiusx/1.2,2*radiusy,speed) self.Hershey(s,cx,cy+radiusy,radiusx/1.2,2*radiusy,speed) else: self.Hershey(s,cx-radiusx,cy+radiusy,2*radiusx,2*radiusy,speed) return 1
def take_screenshot() -> str: print("This is an real!!") screenshot = grab() file_path = os.path.join("temp", "screenshot.jpg") screenshot.save(file_path) return file_path
def CutScreen(self): #截屏,返回图像路径 pic_name = strftime('%Y%m%d%H%M%S',localtime(time())) pic = grab() dir = r'D:/%s.jpg' % pic_name pic.save(dir) return dir