def matchResImgInWindow(handle,imgName,threshold=0.8,mult=True): #获取目标图片 tmp=imgName.split(".") fSplit=tmp[0].split("_") fLen=len(fSplit) targetImgPerLeftX=int(fSplit[fLen-4]) targetImgPerLeftY=int(fSplit[fLen-3]) targetImgPerRightX=int(fSplit[fLen-2]) targetImgPerRightY=int(fSplit[fLen-1]) imgPath=path.getResDirPath()+imgName if not os.path.exists(path.getProjectPath()): os.makedirs(path.getProjectPath()) targetImg=Image.open(imgPath) targetImgWidth=targetImg.size[0] targetImgHeigth=targetImg.size[1] perSize=(targetImgPerRightX-targetImgPerLeftX)*0.01 resWinWidth=int(targetImgWidth/perSize) perSize=(targetImgPerRightY-targetImgPerLeftY)*0.01 resWinHeight=int(targetImgHeigth/perSize) #模板图片 temImg=cv2.cvtColor(numpy.asarray(targetImg),cv2.COLOR_RGB2BGR) targetImg.close() wLeft, wTop, wRight, wBottom = appGetWindowRect(handle) winImg = ImageGrab.grab(bbox=(wLeft, wTop, wRight, wBottom)) #对截图缩放,适配资源图片 toMatchWinImgSrc=cv2.cvtColor(numpy.asarray(winImg),cv2.COLOR_RGB2BGR) toMatchWinImg=cv2.resize(toMatchWinImgSrc, (resWinWidth,resWinHeight),interpolation=cv2.INTER_AREA) winImg.close() res = cv2.matchTemplate(toMatchWinImg,temImg,cv2.TM_CCOEFF_NORMED) xyList=[] if mult==True : loc = numpy.where(res>=threshold) for pt in zip(*loc[::-1]): xyList.append((wLeft+pt[0]+(targetImgWidth>>1),wTop+pt[1]+(targetImgHeigth>>1))) else: #单个很不准确 min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) top_left = min_loc #左上角的位置 xyList.append((wLeft+top_left[0]+(targetImgWidth>>1),wTop+top_left[1]+(targetImgHeigth>>1))) # print(xyList) return xyList
def grabCaptureRect(hwnd,tLeft, tTop, tRight, tBottom,needShow=False): win32gui.SetForegroundWindow(hwnd) img = ImageGrab.grab(bbox=(tLeft, tTop, tRight, tBottom)) screenPath=path.getProjectPath()+"screen\\rect\\"+datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")+".png" if not os.path.exists(path.getProjectPath()+"screen\\rect"): os.makedirs(path.getProjectPath()+"screen\\rect") img.save(screenPath) if needShow==True: img.show() img.close()
def grabCaptureDir(hwnd,dirName): win32gui.SetForegroundWindow(hwnd) wLeft, wTop, wRight, wBottom = appGetWindowRect(hwnd) img = ImageGrab.grab(bbox=(wLeft, wTop, wRight, wBottom)) phash=imgHash(img,hashSize,highfreq_factor) screenPath=path.getProjectPath()+"screen\\"+dirName+"\\"+phash+"_"+datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")+".png" if not os.path.exists(path.getProjectPath()+"screen\\"+dirName): os.makedirs(path.getProjectPath()+"screen\\"+dirName) img.save(screenPath) img.close()
def getResImgHashAndSize(fileName): imgPath=path.getResDirPath()+fileName if not os.path.exists(path.getProjectPath()): os.makedirs(path.getProjectPath()) img=Image.open(imgPath) xSize=img.size[0] ySize=img.size[1] hashCode=imgHash(img,hashSize,highfreq_factor) img.close() return hashCode,xSize,ySize
def grabCaptureDef(hwnd,needShow=False): setForegroundWindow(hwnd) wLeft, wTop, wRight, wBottom = appGetWindowRect(hwnd) img = ImageGrab.grab(bbox=(wLeft, wTop, wRight, wBottom)) phash=imgHash(img,hashSize,highfreq_factor).__str__() screenPath=path.getProjectPath()+"screen\\"+phash+"_"+datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")+".png" if not os.path.exists(path.getProjectPath()+"screen"): os.makedirs(path.getProjectPath()+"screen") img.save(screenPath) if needShow==True: img.show() img.close()
def featResImgInWindow(handle, imgName,distance=0.75,threshold=8): imgPath = path.getResDirPath()+imgName if not os.path.exists(path.getProjectPath()): os.makedirs(path.getProjectPath()) targetImg = Image.open(imgPath) wLeft, wTop, wRight, wBottom = appGetWindowRect(handle) winImg = ImageGrab.grab(bbox=(wLeft, wTop, wRight, wBottom)) imgTag = cv2.cvtColor(numpy.asarray(targetImg), cv2.COLOR_RGB2BGR) imgWin = cv2.cvtColor(numpy.asarray(winImg), cv2.COLOR_RGB2BGR) # imgTag = cv2.cvtColor(numpy.asarray(targetImg), cv2.COLOR_RGB2GRAY) # imgWin = cv2.cvtColor(numpy.asarray(winImg), cv2.COLOR_RGB2GRAY) targetImg.close() winImg.close() # fast = cv2.FastFeatureDetector_create(threshold) # keypointTag = fast.detect(imgTag,None) # keypointWin = fast.detect(imgWin,None) # tmpimg=cv2.drawKeypoints(imgTag,keypointTag,outImage=numpy.array([]),color=(0,0,255)) # tmpimg=cv2.drawKeypoints(imgWin,keypointWin,outImage=numpy.array([]),color=(0,0,255)) # cv2.imshow("show key points",tmpimg) # cv2.waitKey(0) orb = cv2.ORB_create(nfeatures = 1000,edgeThreshold =threshold, nlevels = 1, patchSize = threshold) orb.setFastThreshold(0) keypointTag, desTag = orb.detectAndCompute(imgTag, None) keypointWin, desWin = orb.detectAndCompute(imgWin, None) # tmpimg=cv2.drawKeypoints(imgWin,keypointWin,outImage=numpy.array([]),color=(0,0,255)) # tmpimg = cv2.drawKeypoints( # imgTag, keypointTag, outImage=numpy.array([]), color=(0, 0, 255)) # cv2.imshow("show key points", tmpimg) # cv2.waitKey(0) # print(keypointWin,desWin) # bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # matches = bf.match(desTag, desWin) # matches = sorted(matches, key=lambda x: x.distance) bf = cv2.BFMatcher() matches =bf.knnMatch(desTag, desWin, k=2) good = [] for m,n in matches: if m.distance < distance*n.distance: good.append([m]) print(good) img3= cv2.drawMatchesKnn(imgTag, keypointTag, imgWin, keypointWin,good,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) # img3 = cv2.drawMatches(imgTag, keypointTag, imgWin, keypointWin, matches, None, flags=2, singlePointColor=(255, 0, 0),) cv2.imshow("show key points", img3) cv2.waitKey(0)
def grabCaptureRectPer(hwnd,tLeft, tTop, tRight, tBottom,needShow=False): win32gui.SetForegroundWindow(hwnd) xLeft=getPosX(hwnd,tLeft) yLeft=getPosY(hwnd,tTop) xRight=getPosX(hwnd,tRight) yRight=getPosY(hwnd,tBottom) print(xLeft,yLeft , xRight,yRight ) img = ImageGrab.grab(bbox=(xLeft,yLeft , xRight,yRight )) screenPath=path.getProjectPath()+"screen\\rect_per\\"+datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")+"_"+str(xLeft)+"_"+str(yLeft) +"_"+ str(xRight)+"_"+str(yRight) +".png" if not os.path.exists(path.getProjectPath()+"screen\\rect_per"): os.makedirs(path.getProjectPath()+"screen\\rect_per") img.save(screenPath) if needShow==True: img.show() img.close()
def grabCaptureRectPerHash(hwnd,tLeft, tTop, tRight, tBottom,needShow=False): win32gui.SetForegroundWindow(hwnd) xLeft=getPosX(hwnd,tLeft) yLeft=getPosY(hwnd,tTop) xRight=getPosX(hwnd,tRight) yRight=getPosY(hwnd,tBottom) img = ImageGrab.grab(bbox=(xLeft,yLeft , xRight,yRight )) phash=imgHash(img,hashSize,highfreq_factor) screenPath=path.getProjectPath()+"screen\\rect_per\\"+phash+"_"+str(tLeft)+"_"+str(tTop) +"_"+ str(tRight)+"_"+str(tBottom) +".png" if not os.path.exists(path.getProjectPath()+"screen\\rect_per"): os.makedirs(path.getProjectPath()+"screen\\rect_per") img.save(screenPath) if needShow==True: img.show() img.close()
def matchResImgInWindowOneSize(handle, imgName, threshold=0.8, mult=True): imgPath = path.getResDirPath()+imgName if not os.path.exists(path.getProjectPath()): os.makedirs(path.getProjectPath()) targetImg = Image.open(imgPath) targetImgWidth = targetImg.size[0] targetImgHeigth = targetImg.size[1] # 模板图片 temImg = cv2.cvtColor(numpy.asarray(targetImg), cv2.COLOR_RGB2GRAY) # temImg = cv2.cvtColor(numpy.asarray(targetImg), cv2.COLOR_RGB2GRAY) targetImg.close() wLeft, wTop, wRight, wBottom = appGetWindowRect(handle) winImg = ImageGrab.grab(bbox=(wLeft, wTop, wRight, wBottom)) # 对截图缩放,适配资源图片 toMatchWinImgSrc = cv2.cvtColor(numpy.asarray(winImg), cv2.COLOR_RGB2GRAY) toMatchWinImg = cv2.resize( toMatchWinImgSrc, (396, 698), interpolation=cv2.INTER_AREA) winImg.close() res = cv2.matchTemplate(toMatchWinImg, temImg, cv2.TM_CCOEFF_NORMED) xyList = [] if mult == True: loc = numpy.where(res >= threshold) for pt in zip(*loc[::-1]): x = wLeft+int((pt[0]+(targetImgWidth >> 1))) y = wTop+int((pt[1]+(targetImgHeigth >> 1))) xyList.append((x, y)) else: # 单个很不准确 x = wLeft+int((pt[0]+(targetImgWidth >> 1))) y = wTop+int((pt[1]+(targetImgHeigth >> 1))) xyList.append((x, y)) print(xyList[:10]) return xyList
def download_from_url(url, dirPath=None): if dirPath == None: dirPath = path.getProjectPath() + "down" if not os.path.exists(dirPath): os.makedirs(dirPath) # 获取文件长度 try: file_size = int(urlopen(url).info().get('Content-Length', -1)) except Exception as e: print(e) print("错误,访问url: %s 异常" % url) return False fileNames = url.split("/") fileName = fileNames[len(fileNames) - 1] savePath = dirPath + "\\" + fileName # 文件大小 if os.path.exists(savePath): first_byte = os.path.getsize(savePath) else: first_byte = 0 if first_byte >= file_size: return file_size header = {"Range": "bytes=%s-%s" % (first_byte, file_size)} pbar = tqdm(total=file_size, initial=first_byte, unit='B', unit_scale=True, desc=url.split('/')[-1]) # 访问url进行下载 req = requests.get(url, headers=header, stream=True) try: with (open(savePath, 'ab')) as f: for chunk in req.iter_content(chunk_size=1024): if chunk: f.write(chunk) pbar.update(1024) except Exception as e: print(e) pbar.close() log.errUrl(url) return False pbar.close() return True
def downUrl(url, dirPath=None): urlFile = requests.get(url) fileNames = url.split("/") fileName = fileNames[len(fileNames) - 1] if dirPath == None: dirPath = path.getProjectPath() + "down" if not os.path.exists(dirPath): os.makedirs(dirPath) savePath = dirPath + "\\" + fileName if os.path.exists(savePath): log.err("had:" + savePath) return print("down:" + url) with open(savePath, "wb") as code: code.write(urlFile.content)
def initJdSpider(paramFrame, bg='#ddd'): spider = MainSpider() useTag = tk.IntVar() useTag.set(0) openBs = tk.IntVar() openBs.set(0) needDown = tk.IntVar() needDown.set(1) dataTag = tk.StringVar() dataTag.set(confTag) def checkUseTag(): if useTag.get() == 1: spider.useTag = True else: spider.useTag = False checkUseTag() def checkOpenBs(): if openBs.get() == 1: spider.openBs = True else: spider.openBs = False checkOpenBs() def checkNeedDown(): if needDown.get() == 1: spider.needDown = True else: spider.needDown = False checkNeedDown() url = tk.StringVar() if baseUrl == None: url.set(spider.baseUrl) else: url.set(baseUrl) savePath = tk.StringVar() savePath.set(path.getProjectPath() + "down\\down_list_" + str(datetime.date.today())) totalPage = tk.IntVar() totalPage.set(spider.totalPage) dbPathShow = tk.StringVar() dbPathShow.set(dbPath) def startRun(): spider.baseUrl = url.get() spider.savePath = savePath.get() spider.totalPage = totalPage.get() spider.dataTag = dataTag.get() spider.start() def selectDbFile(): dbPath = filedialog.askopenfilename() dbCon.initConnect(dbPath) dbPathShow.set(dbPath) # tk.Checkbutton(paramFrame,bg=bg, text="打开标签模式", variable=useTag, onvalue=1, # offvalue=0, command=checkUseTag).grid(row=0, column=1) tk.Checkbutton(paramFrame, bg=bg, text="打开浏览器模式", variable=openBs, onvalue=1, offvalue=0, command=checkOpenBs).grid(row=0, column=1) tk.Checkbutton(paramFrame, bg=bg, text="自动下载(很慢)", variable=needDown, onvalue=1, offvalue=0, command=checkNeedDown).grid(row=0, column=2) tk.Label(paramFrame, bg=bg, text="url").grid(row=1, column=0) tk.Entry(paramFrame, bg=bg, textvariable=url, width=100).grid(row=1, column=1, columnspan=4) tk.Label(paramFrame, bg=bg, text="保存路径:").grid(row=2, column=0) tk.Entry(paramFrame, bg=bg, textvariable=savePath, width=100).grid(row=2, column=1, columnspan=4) tk.Label(paramFrame, bg=bg, text="翻页数量:").grid(row=3, column=0) tk.Entry(paramFrame, bg=bg, textvariable=totalPage, width=20).grid(row=3, column=1, columnspan=1) tk.Label(paramFrame, bg=bg, text="数据库标签:").grid(row=4, column=0) tk.Entry(paramFrame, bg=bg, textvariable=dataTag, width=20).grid(row=4, column=1, columnspan=1) tk.Label(paramFrame, bg=bg, text="数据库路径:").grid(row=5, column=0) tk.Label(paramFrame, bg=bg, textvariable=dbPathShow).grid(row=5, column=1) tk.Button(paramFrame, bg=bg, text="选择数据库", width=10, height=1, command=selectDbFile).grid(row=5, column=2) tk.Button(paramFrame, bg=bg, text="启动爬虫", width=10, height=1, command=startRun).grid(row=6, column=1) tk.Button(paramFrame, bg=bg, text="关闭", width=10, height=1, command=spider.stop).grid(row=6, column=2)
def saveTmpImg(img,hashCode=""): screenPath=path.getProjectPath()+"screen\\tmp\\"+hashCode+"_"+datetime.datetime.now().strftime("%Y%m%d%H%M%S%f") +".png" if not os.path.exists(path.getProjectPath()+"screen\\tmp"): os.makedirs(path.getProjectPath()+"screen\\tmp") img.save(screenPath)
def getResImgHash(fileName): imgPath=path.getResDirPath()+fileName if not os.path.exists(path.getProjectPath()): os.makedirs(path.getProjectPath()) return imgHash(Image.open(imgPath),hashSize,highfreq_factor)
def matchResImgInWindowPerSize(handle, imgName, threshold=0.8, mult=True): tmp = imgName.split(".") fSplit = tmp[0].split("_") fLen = len(fSplit) targetImgPerLeftX = int(fSplit[fLen-4]) targetImgPerLeftY = int(fSplit[fLen-3]) targetImgPerRightX = int(fSplit[fLen-2]) targetImgPerRightY = int(fSplit[fLen-1]) imgPath = path.getResDirPath()+imgName if not os.path.exists(path.getProjectPath()): os.makedirs(path.getProjectPath()) targetImg = Image.open(imgPath) targetImgWidth = targetImg.size[0] targetImgHeigth = targetImg.size[1] perSizeW = (targetImgPerRightX-targetImgPerLeftX)*0.01 resWinWidth = int(targetImgWidth/perSizeW) perSizeH = (targetImgPerRightY-targetImgPerLeftY)*0.01 resWinHeight = int(targetImgHeigth/perSizeH) # 模板图片 temImg = cv2.cvtColor(numpy.asarray(targetImg), cv2.COLOR_RGB2GRAY) # temImg = cv2.cvtColor(numpy.asarray(targetImg), cv2.COLOR_RGB2GRAY) targetImg.close() wLeft, wTop, wRight, wBottom = appGetWindowRect(handle) winImg = ImageGrab.grab(bbox=(wLeft, wTop, wRight, wBottom)) winNowW = wRight-wLeft winNowH = wBottom-wTop # 对截图缩放,适配资源图片 toMatchWinImgSrc = cv2.cvtColor(numpy.asarray(winImg), cv2.COLOR_RGB2GRAY) toMatchWinImg = cv2.resize( toMatchWinImgSrc, (resWinWidth, resWinHeight), interpolation=cv2.INTER_AREA) winImg.close() scaleValueW = winNowW/resWinWidth scaleValueH = winNowH/resWinHeight res = cv2.matchTemplate(toMatchWinImg, temImg, cv2.TM_CCOEFF_NORMED) xyList = [] if mult == True: loc = numpy.where(res >= threshold) for pt in zip(*loc[::-1]): x = wLeft+int((pt[0]+(targetImgWidth >> 1))*scaleValueW) y = wTop+int((pt[1]+(targetImgHeigth >> 1))*scaleValueW) xyList.append((x, y)) else: # 单个很不准确 x = wLeft+int((pt[0]+(targetImgWidth >> 1))*scaleValueW) y = wTop+int((pt[1]+(targetImgHeigth >> 1))*scaleValueW) xyList.append((x, y)) print(xyList[:10]) return xyList