selenium實現chrome分屏截圖的合並


selenium的截圖功能在chrome下無法實現,但是可以操作滾動條來一屏一屏的截圖,然后再合並成一張圖,合並圖片的代碼在網上找的,十分感謝那位朋友,具體解決方案如下:直接上代碼:

  1 def capture(base_url, pix_w, pix_h, filename):
  2     """chrome截屏 
  3     base_url- 要截屏的url
  4     pix_w- 窗口寬 
  5     pix_h- 窗口高
  6     filename-生成截圖的文件名 
  7     """  
  8     try:
  9         driver = webdriver.Chrome() 
 10         driver.set_window_size(pix_w, pix_h+89)
 11         driver.get(base_url)            
 12         time.sleep(2)
 13         img_list = []
 14         i = 0
 15         while i<20:
 16             #滾屏
 17             js="var q=document.body.scrollTop="+ str(i*pix_h)+";"
 18             driver.execute_script(js)
 19             js1 = "return document.body.scrollHeight.toString()+','+document.body.scrollTop.toString()"
 20             js1_result = driver.execute_script(js1)
 21             real_scroll_h, real_top = js1_result.split(',')[0], js1_result.split(',')[1]
 22             #real_scroll_h, real_top 是當前滾動條長度和當前滾動條的top,作為是否繼續執行的依據,由於存在滾動條向下拉動后會加載新內容的情況,所以需要以下的判斷
 23             #如果這次設置的top成功,則繼續滾屏
 24             if real_top == str(i*pix_h):
 25                 i += 1
 26                 driver.save_screenshot('static/capture/wap/wap-'+filename + str(i)+'.png')
 27                 img_list.append('static/capture/wap/wap-'+filename + str(i)+'.png')
 28                 last_t = real_top
 29             else:
 30                 #如果本次設置失敗,看這次的top和上一次記錄的top值是否相等,相等則說明沒有新加載內容,且已到頁面底,跳出循環
 31                 if real_top != last_t:
 32                     last_t = real_top          
 33                 else:
 34                     driver.save_screenshot(filename + str(i+1)+'.png')
 35                     img_list.append(filename + str(i+1)+'.png')
 36                     break
 37         image_merge(img_list, "", filename+'.png')
 38     except Exception,e:
 39         print e
 40 def image_merge(images, output_dir, output_name='merge.jpg', restriction_max_width=None, restriction_max_height=None):  
 41     """垂直合並多張圖片 
 42     images - 要合並的圖片路徑列表 
 43     ouput_dir - 輸出路徑 
 44     output_name - 輸出文件名 
 45     restriction_max_width - 限制合並后的圖片最大寬度,如果超過將等比縮小 
 46     restriction_max_height - 限制合並后的圖片最大高度,如果超過將等比縮小 
 47     """  
 48     def image_resize(img, size=(1500, 1100)):  
 49         """調整圖片大小 
 50         """  
 51         try:  
 52             if img.mode not in ('L', 'RGB'):  
 53                 img = img.convert('RGB')  
 54             img = img.resize(size)  
 55         except Exception, e:  
 56             pass  
 57         return img 
 58     max_width = 0  
 59     total_height = 0  
 60     # 計算合成后圖片的寬度(以最寬的為准)和高度  
 61     for img_path in images:  
 62         if os.path.exists(img_path):  
 63             img = Image.open(img_path)  
 64             width, height = img.size  
 65             if width > max_width:  
 66                 max_width = width  
 67             total_height += height  
 68   
 69     # 產生一張空白圖  
 70     new_img = Image.new('RGB', (max_width, total_height), 255)  
 71     # 合並  
 72     x = y = 0  
 73     for img_path in images:  
 74         if os.path.exists(img_path):  
 75             img = Image.open(img_path)  
 76             width, height = img.size  
 77             new_img.paste(img, (x, y))  
 78             y += height  
 79   
 80     if restriction_max_width and max_width >= restriction_max_width:  
 81         # 如果寬帶超過限制  
 82         # 等比例縮小  
 83         ratio = restriction_max_height / float(max_width)  
 84         max_width = restriction_max_width  
 85         total_height = int(total_height * ratio)  
 86         new_img = image_resize(new_img, size=(max_width, total_height))  
 87   
 88     if restriction_max_height and total_height >= restriction_max_height:  
 89         # 如果高度超過限制  
 90         # 等比例縮小  
 91         ratio = restriction_max_height / float(total_height)  
 92         max_width = int(max_width * ratio)  
 93         total_height = restriction_max_height  
 94         new_img = image_resize(new_img, size=(max_width, total_height))  
 95       
 96     if not os.path.exists(output_dir):  
 97         os.makedirs(output_dir)  
 98     save_path = '%s/%s' % (output_dir, output_name)  
 99     new_img.save(save_path)  
100     for img_path in images:  
101         os.remove(img_path)
102     return save_path         

 

大致就這么個思路


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM