• 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_  
     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_  
     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         

    大致就这么个思路

  • 相关阅读:
    poj 1562 Oil Deposits
    poj 1650 Integer Approximation
    snmp4j 编程
    ubuntu 13.04 163源(亲测可用)
    c语言中static 用法总结(转)
    Spring入门
    Hibernate入门
    Struts2入门教程
    素数距离问题
    ASCII码排序
  • 原文地址:https://www.cnblogs.com/sparkling-ly/p/5466644.html
Copyright © 2020-2023  润新知