From 04e2a310c6d9b07e8a15c0f44d6306f5e4cfd2b6 Mon Sep 17 00:00:00 2001 From: qq_38870145 Date: Tue, 1 Apr 2025 18:47:00 +0800 Subject: [PATCH] Tue Apr 1 18:47:00 CST 2025 inscode --- outlook.py | 232 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 148 insertions(+), 84 deletions(-) diff --git a/outlook.py b/outlook.py index 2c22152..42a2dec 100644 --- a/outlook.py +++ b/outlook.py @@ -11,14 +11,18 @@ import os from PIL import Image from time import sleep -def genDir(): +# cut +cut_img_paths=[] +# full +full_img_paths=[] +def gen_dir(): cur_timestemp = int(round(time.time() * 1000)) dir = './screen_shot/outlook/{ts}'.format(ts=cur_timestemp) os.makedirs(dir, exist_ok=True) return dir -def cutImg(screenshot_path,element,baseDir,loc): +def cut_img(screenshot_path,element,baseDir,loc): # 获取元素坐标和尺寸 location = element.location # {x: number, y: number} size = element.size # {width: number, height: number} @@ -32,87 +36,148 @@ def cutImg(screenshot_path,element,baseDir,loc): # 使用 Pillow 裁剪图像 image = Image.open(screenshot_path) cropped_image = image.crop((left, top, right, bottom)) - cropped_image.save(f"{baseDir}/cut_{loc}.png") - -class ScreenShot: - __JS__ = { - 'scroll_to_bottom': "window.scroll({top:document.body.clientHeight,left:0,behavior:'auto'});", - 'scroll_to_y': "window.scroll({top:%d,left:0,behavior:'auto'});", - } - __base_end__ = 'tmp_end.png' - __scroll_bottom__ = 'scroll_to_bottom' - __scroll_y__ = 'scroll_to_y' - __body__ = '//body' - __height__ = 'height' - __clear_shell__ = 'rm -rf *.png' - __RGB__ = 'RGB' - - @classmethod - def screen_shot(cls, driver, title, uploader_url='', delete=False): - dir=genDir() - # 当前滚动高度 - scrollTop=driver.execute_script( - 'return document.getElementById("ConversationReadingPaneContainer").childNodes[1].childNodes[0].scrollTop;') - # 可滚动高度范围 - scrollHeight=driver.execute_script( - 'return document.getElementById("ConversationReadingPaneContainer").childNodes[1].childNodes[0].scrollHeight;') - # 渲染的可视区域 - clientHeight=driver.execute_script( - 'return document.getElementById("ConversationReadingPaneContainer").childNodes[1].childNodes[0].clientHeight;') - - print('scrollTop', scrollTop,type(scrollTop)) - print('scrollHeight',scrollHeight) - print('clientHeight', clientHeight) - lastScrollTop=scrollTop - # 次数索引 - i=0 - - # 等待元素加载并可见 - element = WebDriverWait(driver, 10).until( - EC.visibility_of_element_located((By.ID, "ConversationReadingPaneContainer")) - ) - - if lastScrollTop 0: + break + else: + # 完整email + img_path=f'{dir}/full_screen_email.png' + driver.save_screenshot(img_path) + + cut_len=len(cut_img_paths) + first=0 + second=1 + + + print('cut_img_paths',cut_img_paths) + print('cut_len',cut_len) + # while second= 1: # 第二张图之后裁剪 + width, height = img.size + # 确保裁剪高度不超出原图范围 + actual_crop = min(crop_height, height) + print('actual_crop',actual_crop) + # 左上 + img = img.crop((0, actual_crop, width, height)) + images.append(img) + + # 计算总高度和最大宽度 + total_height = sum(img.height for img in images) + max_width = max(img.width for img in images) + + # 创建空白画布并拼接 + merged = Image.new('RGB', (max_width, total_height)) + y_offset = 0 + for img in images: + # 居中处理宽度不一致的图片 + x_offset = (max_width - img.width) // 2 + merged.paste(img, (x_offset, y_offset)) + y_offset += img.height + + merged.save(output_path) +def join_images(png1, png2, size=0, output=f'{dir}/temp_result.png'): + """ + 图片拼接 + :param png1: 图片1 + :param png2: 图片2 + :param size: 两个图片重叠的距离 + :param output: 输出的图片文件 + :return: + """ + size = size * 2 + img1, img2 = Image.open(png1), Image.open(png2) + size1, size2 = img1.size, img2.size + joint = Image.new('RGB', (size1[0], size1[1] + size2[1] - size)) + loc1, loc2 = (0, 0), (0, size1[1] - size) + joint.paste(img1, loc1) + joint.paste(img2, loc2) + joint.save(output) + sleep(.5) def run(): global driver @@ -177,8 +242,7 @@ def mapMSg(): # return base64; # }) # """) - cur_timestemp = int(round(time.time() * 1000)) - ScreenShot.screen_shot(driver, 'outlook_' + str(cur_timestemp)) + screen_shot(driver) # print('base64Data',base64Data) # email_box=driver.find_element(By.ID,':mt') break -- GitLab