Browse Source

图片留白优化

hsun 1 year ago
parent
commit
3598095555
2 changed files with 78 additions and 12 deletions
  1. 76 7
      eta_report2img/html2img_ping.py
  2. 2 5
      eta_report2img/img_pdf_png.py

+ 76 - 7
eta_report2img/html2img_ping.py

@@ -3,6 +3,8 @@ from time import sleep
 from selenium import webdriver
 from selenium.webdriver.chrome.service import Service
 import img_pdf_png
+import os
+from PIL import Image
 
 
 def scroll_page(driver):
@@ -19,7 +21,7 @@ def scroll_page(driver):
         for j in range(n1):
             # print(r"j:{}".format(j))
             if j == n1 - 1 and last_height > 0:  # 截取尾部
-                driver.execute_script("window.scrollBy(0,{})".format(page_height))  # 滚动条滚到底
+                driver.execute_script("window.scrollBy(0,{})".format(last_height))  # 滚动条滚到底
                 print("滚到底了")
             else:
                 driver.execute_script("window.scrollBy(0,{})".format(web_height))
@@ -45,9 +47,18 @@ def html2img(driver, image_folder):
     print("当前窗口网页高度{}".format(web_height))
     last_height = page_height % web_height
     print("底部多出来的部分{}".format(last_height))
-    margin_flag = False
+
+    is_end = 0
     if n1 == 0 or (n1 == 1 and last_height == 0):  # 判断是否需要滚动
-        driver.get_screenshot_as_file(r'{}/{}.png'.format(image_folder, '0'))  # 指定截图保存位置
+        # 去除留白部分, 一般情况下也不会只写一页报告且留那么多空白
+        crop_height = web_height - page_height
+        if crop_height > 0:
+            origin_last_img = r'{}/{}.png'.format(image_folder, 1000)
+            new_last_img = r'{}/{}.png'.format(image_folder, 0)
+            driver.save_screenshot(origin_last_img)
+            crop_img_botton(origin_last_img, new_last_img, page_height)
+        else:
+            driver.get_screenshot_as_file(r'{}/{}.png'.format(image_folder, '0'))
     else:
         j = 0
         while j <= n1:
@@ -55,16 +66,74 @@ def html2img(driver, image_folder):
                 driver.execute_script("window.scrollBy(0,0)")
                 # print(0)
             elif j == n1 and last_height > 0:  # 截取尾部
-                driver.execute_script("$('#app #resetcss').css('margin-bottom', '{}px')".format(web_height))
-                driver.execute_script("window.scrollBy(0,{})".format(web_height))  # 滚动条滚到底
+                # driver.execute_script("$('#app #resetcss').css('margin-bottom', '{}px')".format(web_height))
+                driver.execute_script("window.scrollBy(0,{})".format(last_height))  # 滚动条滚到底
+
+                # 最后一屏先进行一次保存
+                sleep(1)
+                origin_last_img = r'{}/{}.png'.format(image_folder, j + 1000)
+                driver.save_screenshot(origin_last_img)
+
+                # 截取上面重叠的一部分并保存截取后的图片
+                new_last_img = r'{}/{}.png'.format(image_folder, j)
+                crop_height = web_height - last_height
+                crop_img_top(origin_last_img, new_last_img, crop_height)
+
+                is_end = 1
                 print("拉到底拉")
             else:
                 driver.execute_script("window.scrollBy(0,{})".format(web_height))
-            sleep(1)
-            driver.save_screenshot(r'{}/{}.png'.format(image_folder, j))  # 截屏
+
+            if is_end == 0:
+                sleep(1)
+                driver.save_screenshot(r'{}/{}.png'.format(image_folder, j))  # 截屏
             j = j + 1
 
 
+# crop_img_top 裁掉图片上面的部分保留剩下的部分
+def crop_img_top(image_path, output_path, crop_height):
+    # 打开图像文件
+    img = Image.open(image_path)
+
+    # 获取图像的宽度和高度
+    width, height = img.size
+
+    # 计算裁剪的起始坐标
+    start_x = 0
+    start_y = min(height, crop_height)  # 保证不超出图像下边界
+
+    # 裁剪图像,仅保留上半部分
+    cropped_img = img.crop((start_x, start_y, start_x + width, start_y + (height - crop_height)))
+
+    # 保存裁剪后的图像
+    cropped_img.save(output_path)
+
+    # 删除原始图像
+    os.remove(image_path)
+
+
+# crop_img_botton 裁掉图片下面的部分保留上面的部分
+def crop_img_botton(image_path, output_path, keep_height):
+    # 打开图像文件
+    img = Image.open(image_path)
+
+    # 获取图像的宽度和高度
+    width, height = img.size
+
+    # 计算保留部分的起始坐标
+    start_x = 0
+    start_y = 0
+
+    # 裁剪图像,保留从左上角开始指定高度的部分
+    cropped_img = img.crop((start_x, start_y, start_x + width, start_y + keep_height))
+
+    # 保存裁剪后的图像
+    cropped_img.save(output_path)
+
+    # 删除原始图像
+    os.remove(image_path)
+
+
 # 调用截图函数
 if __name__ == "__main__":
     # 创建一个 Chrome WebDriver 实例

+ 2 - 5
eta_report2img/img_pdf_png.py

@@ -35,13 +35,10 @@ def merge_images(image_folder, output_file, file_name, output_type, n=1):
     now_height = 0
     for i in range(image_count):
         img = Image.open(os.path.join(image_folder, target_img[i]))
-        img_size0 = int(img.size[0] / n)
-        img_size1 = int(img.size[1] / n)
-        img = img.resize((img_size0, img_size1))
-        if i != 0:
-            now_height += img.size[1]
         # 纵向拼接图片
         new_img.paste(img, (0, now_height))
+        # 当前粘贴高度自增已拼接的图片高度
+        now_height += img.size[1]
 
     # 默认两种类型都生成
     if output_type == "":