![Python實(shí)現(xiàn)網(wǎng)頁(yè)截圖(PyQT5)過(guò)程解析_第1頁(yè)](http://file4.renrendoc.com/view/1b3216972521b062604e2656fcd17f38/1b3216972521b062604e2656fcd17f381.gif)
![Python實(shí)現(xiàn)網(wǎng)頁(yè)截圖(PyQT5)過(guò)程解析_第2頁(yè)](http://file4.renrendoc.com/view/1b3216972521b062604e2656fcd17f38/1b3216972521b062604e2656fcd17f382.gif)
![Python實(shí)現(xiàn)網(wǎng)頁(yè)截圖(PyQT5)過(guò)程解析_第3頁(yè)](http://file4.renrendoc.com/view/1b3216972521b062604e2656fcd17f38/1b3216972521b062604e2656fcd17f383.gif)
![Python實(shí)現(xiàn)網(wǎng)頁(yè)截圖(PyQT5)過(guò)程解析_第4頁(yè)](http://file4.renrendoc.com/view/1b3216972521b062604e2656fcd17f38/1b3216972521b062604e2656fcd17f384.gif)
![Python實(shí)現(xiàn)網(wǎng)頁(yè)截圖(PyQT5)過(guò)程解析_第5頁(yè)](http://file4.renrendoc.com/view/1b3216972521b062604e2656fcd17f38/1b3216972521b062604e2656fcd17f385.gif)
下載本文檔
版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
Python實(shí)現(xiàn)?頁(yè)截圖(PyQT5)過(guò)程解析?案說(shuō)明功能要求:實(shí)現(xiàn)?頁(yè)加載后將頁(yè)?截取成長(zhǎng)圖?涉及模塊:PyQT5PIL邏輯說(shuō)明:1:完成窗?設(shè)置,利?PyQT5QWebEngineView加載?頁(yè)地址,待?頁(yè)加載完成后,調(diào)?check_pag;classMainWindow(QMainWindow):def__init__(self,parent=None):super(MainWindow,self).__init__(parent)self.setWindowTitle('易哈佛')self.temp_height=0self.setWindowFlag(Qt.WindowMinMaxButtonsHint,禁?最?化,最?化#self.setWindowFlag(Qt.WindowStaysOnTopHint,窗?頂置窗??邊框False)#True)#self.setWindowFlag(Qt.FramelessWindowHint,True)#defurlScreenShot(self,url):self.browser=QWebEngineView()self.browser.load(QUrl(url))geometry=self.chose_screen()self.setGeometry(geometry)self.browser.loadFinished.connect(self.check_page)self.setCentralWidget(self.browser)defget_page_size(self):size=self.browser.page().contentsSize()self.set_height=size.height()self.set_width=size.width()returnsize.width(),size.height()defchose_screen(self):width,height=750,1370desktop=QApplication.desktop()screen_count=desktop.screenCount()foriinrange(0,screen_count):rect=desktop.availableGeometry(i)s_width,s_height=rect.width(),rect.height()ifs_width>widthands_height>height:returnQRect(rect.left(),rect.top(),width,height)returnQRect(0,0,width,height)if__name__=='__main__':app=QApplication(sys.argv)win=MainWindow()win.show()app.exit(app.exec_())2:收集頁(yè)??度,并計(jì)算分次截屏的次數(shù)和余量?度;實(shí)例化圖?合并?具,設(shè)置定時(shí)器,超時(shí)信號(hào)發(fā)出后,執(zhí)?exe_command;defcheck_page(self):p_width,p_height=self.get_page_size()self.page,self.over_flow_size=divmod(p_height,self.height())ifself.page==0:self.page=1self.ssm=ScreenShotMerge(self.page,self.over_flow_size)self.timer=QTimer(self)self.timer.timeout.connect(self.exe_command)self.timer.setInterval(400)self.timer.start()3:exe_command?來(lái)控制截圖次數(shù),并在每次截圖完成后控制?頁(yè)向下滑屏幕的?度;所有的頁(yè)?都已截取時(shí),完成圖?合并。defexe_command(self):ifself.page>0:self.screen_shot()self.run_js()elifself.page<0:self.timer.stop()self.ssm.image_merge()self.close()
elifself.over_flow_size>0:self.screen_shot()self.=1defrun_js(self):script="""varscroll=function(dHeight){vart=document.documentElement.scrollTopvarh=document.documentElement.scrollHeightdHeight=dHeight||0varcurrent=t+dHeightif(current>h){window.scrollTo(0,document.documentElement.clientHeight)}else{window.scrollTo(0,current)}}"""command=script+'\nscroll({})'.format(self.height())self.browser.page().runJavaScript(command)4:screen_shot在每次截圖完成后將圖?保存,并將圖?對(duì)象由圖?合并根據(jù)保存到列表中。defscreen_shot(self):screen=QApplication.primaryScreen()winid=self.browser.winId()pix=screen.grabWindow(int(winid))name='{}/temp.png'.format(self.ssm.root_path)pix.save(name)self.ssm.add_im(name)5:截圖合并?具,在每次截圖完成后將圖?對(duì)象保存,完成余量截圖的重繪和截圖的合并。classScreenShotMerge():def__init__(self,page,over_flow_size):self.im_list=[]self.page=pageself.over_flow_size=over_flow_sizeself.get_path()defget_path(self):self.root_path=Path(__file__).parent.joinpath('temp')ifnotself.root_path.exists():self.root_path.mkdir(parents=True)self.save_path=self.root_path.joinpath('merge.png')defadd_im(self,path):iflen(self.im_list)==self.page:im=self.reedit_image(path)else:im=Image.open(path)im.save('{}/{}.png'.format(self.root_path,len(self.im_list)+1))self.im_list.append(im)defget_new_size(self):max_width=0total_height=0#計(jì)算合成后圖?的寬度(以最寬的為準(zhǔn))和?度f(wàn)orimginself.im_list:width,height=img.sizeifwidth>max_width:max_width=widthtotal_height+=heightreturnmax_width,total_heightdefimage_merge(self,):iflen(self.im_list)>1:max_width,total_height=self.get_new_size()#產(chǎn)??張空?圖new_img=Image.new('RGB',(max_width-15,total_height),255)x=y=0forimginself.im_list:width,height=img.sizenew_img.paste(img,(x,y))y+=heightnew_img.save(self.save_path)print('截圖成功:',self.save_path)
else:obj=self.im_list[0]width,height=obj.sizeleft,top,right,bottom=0,0,width,heightbox=(left,top,right,bottom)region=obj.crop(box)new_img=Image.new('RGB',(width,height),255)new_img.paste(region,box)new_img.save(self.save_path)print('截圖成功:',self.save_path)defreedit_image(self,path):obj=Image.open(path)width,height=obj.sizeleft,top,right,bottom=0,height-self.over_flow_size,width,heightbox=(left,top,right,bottom)region=obj.crop(box)returnregion截圖功能完整代碼#!/usr/bin/envpython#-*-coding:UTF-8-*-#Author:Leslie-ximportsysfromPyQt5.QtCoreimport*fromPyQt5.QtWidgetsimport*fromPyQt5.QtWebEngineWidgetsimport*fromPILimportImagefrompathlibimportPathclassScreenShotMerge():def__init__(self,page,over_flow_size):self.im_list=[]self.page=pageself.over_flow_size=over_flow_sizeself.get_path()defget_path(self):self.root_path=Path(__file__).parent.joinpath('temp')ifnotself.root_path.exists():self.root_path.mkdir(parents=True)self.save_path=self.root_path.joinpath('merge.png')defadd_im(self,path):iflen(self.im_list)==self.page:im=self.reedit_image(path)else:im=Image.open(path)im.save('{}/{}.png'.format(self.root_path,len(self.im_list)+1))self.im_list.append(im)defget_new_size(self):max_width=0total_height=0#計(jì)算合成后圖?的寬度(以最寬的為準(zhǔn))和?度f(wàn)orimginself.im_list:width,height=img.sizeifwidth>max_width:max_width=widthtotal_height+=heightreturnmax_width,total_heightdefimage_merge(self,):iflen(self.im_list)>1:max_width,total_height=self.get_new_size()#產(chǎn)??張空?圖new_img=Image.new('RGB',(max_width-15,total_height),255)x=y=0forimginself.im_list:width,height=img.sizenew_img.paste(img,(x,y))y+=heightnew_img.save(self.save_path)print('截圖成功:',self.save_path)else:obj=self.im_list[0]width,height=obj.sizeleft,top,right,bottom=0,0,width,heightbox=(left,top,right,bottom)region=obj.crop(box)
new_img=Image.new('RGB',(width,height),255)new_img.paste(region,box)new_img.save(self.save_path)print('截圖成功:',self.save_path)defreedit_image(self,path):obj=Image.open(path)width,height=obj.sizeleft,top,right,bottom=0,height-self.over_flow_size,width,heightbox=(left,top,right,bottom)region=obj.crop(box)returnregionclassMainWindow(QMainWindow):def__init__(self,parent=None):super(MainWindow,self).__init__(parent)self.setWindowTitle('易哈佛')self.temp_height=0self.setWindowFlag(Qt.WindowMinMaxButtonsHint,False)#禁?最?化,最?化#self.setWindowFlag(Qt.WindowStaysOnTopHint,True)#窗?頂置self.setWindowFlag(Qt.FramelessWindowHint,True)#窗??邊框defurlScreenShot(self,url):self.browser=QWebEngineView()self.browser.load(QUrl(url))geometry=self.chose_screen()self.setGeometry(geometry)self.browser.loadFinished.connect(self.check_page)self.setCentralWidget(self.browser)defget_page_size(self):size=self.browser.page().contentsSize()self.set_height=size.height()self.set_width=size.width()returnsize.width(),size.height()defchose_screen(self):width,height=750,1370desktop=QApplication.desktop()screen_count=desktop.screenCount()foriinrange(0,screen_count):rect=desktop.availableGeometry(i)s_width,s_height=rect.width(),rect.height()ifs_width>widthands_height>height:returnQRect(rect.left(),rect.top(),width,height)returnQRect(0,0,width,height)defcheck_page(self):p_width,p_height=self.get_page_size()self.page,self.over_flow_size=divmod(p_height,self.height())ifself.page==0:self.page=1self.ssm=ScreenShotMerge(self.page,self.over_flow_size)self.timer=QTimer(self)self.timer.timeout.connect(self.exe_command)self.timer.setInterval(400)self.timer.start()defexe_command(self):ifself.page>0:self.screen_shot()self.run_js()elifself.page<0:self.timer.stop()self.ssm.image_merge()self.close()elifself.over_flow_size>
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 大型鋼結(jié)構(gòu)廣告牌施工方案
- 太原裝配式圍擋施工方案
- 第13課 網(wǎng)絡(luò)安全防范 教學(xué)設(shè)計(jì) -2024--2025學(xué)年浙教版(2023)初中信息技術(shù)八年級(jí)上冊(cè)
- 干掛大理石門(mén)套施工方案
- 2025至2031年中國(guó)掃頻模擬汽車(chē)運(yùn)輸振動(dòng)臺(tái)行業(yè)投資前景及策略咨詢(xún)研究報(bào)告
- 2025至2031年中國(guó)異型管材行業(yè)投資前景及策略咨詢(xún)研究報(bào)告
- 2025至2031年中國(guó)寬帶抽油機(jī)行業(yè)投資前景及策略咨詢(xún)研究報(bào)告
- 2025至2031年中國(guó)多功能調(diào)音臺(tái)行業(yè)投資前景及策略咨詢(xún)研究報(bào)告
- Module 1 Unit 3 My birthday 單元整體(教學(xué)設(shè)計(jì))-2024-2025學(xué)年滬教牛津版(深圳用)英語(yǔ)五年級(jí)上冊(cè)
- 掛板模板支撐專(zhuān)項(xiàng)施工方案
- 美團(tuán)外賣(mài)騎手服務(wù)合同(2025年度)
- 應(yīng)急預(yù)案解讀與實(shí)施
- 早點(diǎn)出租承包合同(2篇)
- 2025年《國(guó)有企業(yè)領(lǐng)導(dǎo)人員腐敗案例剖析》心得體會(huì)樣本(3篇)
- 廣告行業(yè)安全培訓(xùn)詳細(xì)介紹
- 2025福南平市建武夷水務(wù)發(fā)展限公司招聘21人高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025年上半年工業(yè)和信息化部裝備工業(yè)發(fā)展中心應(yīng)屆畢業(yè)生招聘(第二批)易考易錯(cuò)模擬試題(共500題)試卷后附參考答案
- 內(nèi)鏡室院感知識(shí)培訓(xùn)課件
- 2024年廣州市海珠區(qū)衛(wèi)生健康系統(tǒng)招聘事業(yè)單位工作人員筆試真題
- 一科一品一骨科護(hù)理
- 2025年市場(chǎng)拓展工作計(jì)劃
評(píng)論
0/150
提交評(píng)論