def roll_manager(self, area): x, y, w, h = area self.mode = 1 def on_click(x, y, button, pressed): print(x, y, button) if area[0] < x < area[0] + area[2] and area[1] < y < area[1] + area[3] and not pressed: self.mode = 1 lis.stop() def on_scroll(x, y, button, pressed): self.mode = 0 lis.stop() self.tips = TipsShower("单击自动滚动;\n或手动下滚;") if x < QApplication.desktop().width() - x - w: self.tips.move(x + w, y) else: self.tips.move(x - self.tips.width(), y) self.showrect.setGeometry(x, y, w, h) self.showrect.show() QApplication.processEvents() lis = MouseListenner(on_click=on_click, on_scroll=on_scroll) lis.start() print("等待用户开始") lis.join() lis.stop() if self.mode: print("auto_roll") self.auto_roll(area) else: print("scroll_to_roll") self.scroll_to_roll(area)
def scroll_to_roll(self, area): # 手动滚动模式 x, y, w, h = area self.rollermask.tips.setText("向下滚动,单击结束") QApplication.processEvents() screen = QApplication.primaryScreen() winid = QApplication.desktop().winId() self.id = self.rid = 0 self.a_step = 0 def onclick(x, y, button, pressed): if pressed: pix = screen.grabWindow(winid, x, y, w, h) newimg = Image.fromqpixmap(pix) img = cv2.cvtColor(asarray(newimg), cv2.COLOR_RGB2BGR) self.img_list.append(img) else: print("click to stop", len(self.img_list)) self.in_rolling = False listener.stop() def on_scroll(px, py, x_axis, y_axis): # print(px, py, x_axis, y_axis) # if not self.inthearea((px,py),area): # return self.a_step += 1 if self.a_step < 2: return else: self.a_step = 0 if y_axis < 0: if self.rid >= self.id: # 当滚动id与真实id一样时说明 pix = screen.grabWindow(winid, x, y, w, h) # 滚动段距离进行截屏 newimg = Image.fromqpixmap(pix) img = cv2.cvtColor(asarray(newimg), cv2.COLOR_RGB2BGR) self.img_list.append(img) # cv2.imwrite("j_temp/{}.png".format(self.id), img) self.id += 1 # 记录当前滚动的id self.rid = self.id else: # 不一样时说明用户往回滚了,跳过 print("跳过") self.rid += 1 else: # 方向往回滚时id-1,以记录往回的步数 self.rid -= 1 print("方向错误") listener = MouseListenner(on_click=onclick, on_scroll=on_scroll) # 鼠标监听器,传入函数句柄 self.match_thread = Commen_Thread( self.match_and_merge) # 也是把拼接函数放入后台线程中 self.in_rolling = True i = 0 listener.start() # 鼠标监听器启动 self.match_thread.start() # 拼接线程启动 while self.in_rolling: # 等待结束滚动 time.sleep(0.2) listener.stop() # self.showrect.hide() self.match_thread.wait() # 等待拼接线程结束
def scroll_to_roll(self, area): x, y, w, h = area self.rollermask.tips.setText("向下滚动,单击结束") QApplication.processEvents() screen = QApplication.primaryScreen() winid = QApplication.desktop().winId() self.id = self.rid = 0 self.a_step = 0 def onclick(x, y, button, pressed): if pressed: pix = screen.grabWindow(winid, x, y, w, h) newimg = Image.fromqpixmap(pix) img = cv2.cvtColor(asarray(newimg), cv2.COLOR_RGB2BGR) self.img_list.append(img) else: print("click to stop", len(self.img_list)) self.in_rolling = False listener.stop() def on_scroll(px, py, x_axis, y_axis): print(px, py, x_axis, y_axis) # if not self.inthearea((px,py),area): # return self.a_step += 1 if self.a_step < 2: return else: self.a_step = 0 if y_axis < 0: if self.rid >= self.id: pix = screen.grabWindow(winid, x, y, w, h) newimg = Image.fromqpixmap(pix) img = cv2.cvtColor(asarray(newimg), cv2.COLOR_RGB2BGR) self.img_list.append(img) # cv2.imwrite("j_temp/{}.png".format(self.id), img) self.id += 1 self.rid = self.id else: print("跳过") self.rid += 1 else: self.rid -= 1 print("方向错误") listener = MouseListenner(on_click=onclick, on_scroll=on_scroll) self.match_thread = Commen_Thread(self.match_and_merge) self.in_rolling = True i = 0 listener.start() self.match_thread.start() while self.in_rolling: time.sleep(0.2) listener.stop() # self.showrect.hide() self.match_thread.wait()
def auto_roll(self, area): # 自动滚动模式 x, y, w, h = area self.rollermask.tips.setText("单击停止") QApplication.processEvents() speed = round(1 / self.roll_speed, 2) screen = QApplication.primaryScreen() winid = QApplication.desktop().winId() def onclick(x, y, button, pressed): # 点击退出的函数句柄 if pressed: print("click to stop") self.in_rolling = False listener.stop() controler = MouseController() # 鼠标控制器 listener = MouseListenner(on_click=onclick) # 鼠标监听器 self.match_thread = Commen_Thread( self.match_and_merge) # 把match_and_merge放入一个线程中 self.in_rolling = True i = 0 controler.position = (area[0] + int(area[2] / 2), area[1] + int(area[3] / 2)) # 控制鼠标点击到滚动区域中心 oldimg = Image.new("RGB", (128, 128), "#FF0f00") listener.start() while self.in_rolling: st = time.time() pix = screen.grabWindow(winid, x, y, w, h) # 截屏 newimg = Image.fromqpixmap(pix) # 转化为pil的格式 img = cv2.cvtColor(asarray(newimg), cv2.COLOR_RGB2BGR) self.img_list.append(img) # 图片数据存入self.img_list中被后台的拼接线程使用 # cv2.imshow("FSd", img) # cv2.waitKey(0) if i >= 1: if i == 1: self.match_thread.start() # 当截第二张图片时拼接线程才启动 if self.is_same(oldimg, newimg): # 每帧检查是否停止 self.in_rolling = False i += 1 break oldimg = newimg controler.scroll(dx=0, dy=-3) # 控制鼠标滚动 time.sleep(speed) # 通过sleep控制自动滚动速度 # cv2.imwrite('j_temp/{0}.png'.format(i), img) i += 1 print("结束滚动,共截屏{}张".format(i)) listener.stop() # self.showrect.hide() self.match_thread.wait()
def auto_roll(self, area): x, y, w, h = area self.tips.setText("单击停止") QApplication.processEvents() speed = round(1 / self.roll_speed, 2) screen = QApplication.primaryScreen() winid = QApplication.desktop().winId() def onclick(x, y, button, pressed): if pressed: print("click to stop") self.in_rolling = False listener.stop() controler = MouseController() listener = MouseListenner(on_click=onclick) self.match_thread = Commen_Thread(self.match_and_merge) self.in_rolling = True i = 0 controler.position = (area[0] + int(area[2] / 2), area[1] + int(area[3] / 2)) oldimg = Image.new("RGB", (128, 128), "#FF0f00") listener.start() while self.in_rolling: st = time.time() pix = screen.grabWindow(winid, x, y, w, h) newimg = Image.fromqpixmap(pix) img = cv2.cvtColor(np.asarray(newimg), cv2.COLOR_RGB2BGR) self.img_list.append(img) # cv2.imshow("FSd", img) # cv2.waitKey(0) if i >= 1: if i == 1: self.match_thread.start() if self.is_same(oldimg, newimg): # 每帧检查是否停止 self.in_rolling = False i += 1 break oldimg = newimg controler.scroll(dx=0, dy=-3) time.sleep(speed) # cv2.imwrite('j_temp/{0}.png'.format(i), img) i += 1 print("结束滚动,共截屏{}张".format(i)) listener.stop() self.showrect.hide() self.match_thread.wait() self.showm_signal.emit("长截图完成")
def roll_manager(self, area): # 滚动截屏控制器,控制滚动截屏的模式(自动还是手动滚) x, y, w, h = area self.mode = 1 def on_click(x, y, button, pressed): # 用户点击了屏幕说明用户想自动滚 print(x, y, button) if button == mouse.Button.left: if area[0] < x < area[0] + area[2] and area[ 1] < y < area[1] + area[3] and not pressed: self.mode = 1 lis.stop() elif button == mouse.Button.right: self.mode = 2 lis.stop() def on_scroll(x, y, button, pressed): # 用户滚动了鼠标说明用户想要手动滚 self.mode = 0 lis.stop() self.rollermask = roller_mask(area) # 滚动截屏遮罩层初始化 # self.showrect.setGeometry(x, y, w, h) # self.showrect.show() pix = QApplication.primaryScreen().grabWindow( QApplication.desktop().winId(), x, y, w, h) # 先截一张图片 newimg = Image.fromqpixmap(pix) img = cv2.cvtColor(asarray(newimg), cv2.COLOR_RGB2BGR) self.img_list.append(img) QApplication.processEvents() lis = MouseListenner(on_click=on_click, on_scroll=on_scroll) # 鼠标监听器初始化并启动 lis.start() print("等待用户开始") lis.join() lis.stop() if self.mode == 1: # 判断用户选择的模式 print("auto_roll") self.auto_roll(area) elif self.mode == 2: print("exit roller") return 1 else: print("scroll_to_roll") self.scroll_to_roll(area) self.showm_signal.emit("长截图完成") self.rollermask.hide() return 0
def roll_manager(self, area): x, y, w, h = area self.mode = 1 def on_click(x, y, button, pressed): print(x, y, button) if area[0] < x < area[0] + area[2] and area[ 1] < y < area[1] + area[3] and not pressed: self.mode = 1 lis.stop() def on_scroll(x, y, button, pressed): self.mode = 0 lis.stop() self.rollermask = roller_mask(area) # self.showrect.setGeometry(x, y, w, h) # self.showrect.show() pix = QApplication.primaryScreen().grabWindow( QApplication.desktop().winId(), x, y, w, h) newimg = Image.fromqpixmap(pix) img = cv2.cvtColor(asarray(newimg), cv2.COLOR_RGB2BGR) self.img_list.append(img) QApplication.processEvents() lis = MouseListenner(on_click=on_click, on_scroll=on_scroll) lis.start() print("等待用户开始") lis.join() lis.stop() if self.mode: print("auto_roll") self.auto_roll(area) else: print("scroll_to_roll") self.scroll_to_roll(area) self.showm_signal.emit("长截图完成") self.rollermask.hide()