提交 04e2a310 编写于 作者: Q qq_38870145

Tue Apr 1 18:47:00 CST 2025 inscode

上级 efc92edc
......@@ -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,24 +36,19 @@ 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()
cut_file_name=f"{baseDir}/cut_{loc}.png"
cropped_image.save(cut_file_name)
global cut_img_paths
cut_img_paths.append(cut_file_name)
def screen_shot(driver):
dir=gen_dir()
# global cut_img_paths
# cut_img_paths=[]
# global full_img_paths
# full_img_paths=[]
# 当前滚动高度
scrollTop=driver.execute_script(
'return document.getElementById("ConversationReadingPaneContainer").childNodes[1].childNodes[0].scrollTop;')
......@@ -72,12 +71,16 @@ class ScreenShot:
EC.visibility_of_element_located((By.ID, "ConversationReadingPaneContainer"))
)
window_height = driver.execute_script('return window.innerHeight;')
if lastScrollTop<scrollHeight:
img_path=f'{dir}/custom_{i}.png'
driver.save_screenshot(img_path)
cutImg(img_path,element,dir,i)
cut_img(img_path,element,dir,i)
full_img_paths.append(img_path)
# 滚动底部 + 渲染高度 翻页滚动
while lastScrollTop < scrollHeight:
while math.ceil(lastScrollTop+1) < scrollHeight:
i += 1
# 加渲染高度 翻页 好拼接
lastScrollTop += clientHeight
......@@ -87,16 +90,77 @@ class ScreenShot:
sleep(.5)
img_path = f'{dir}/custom_{i}.png'
driver.save_screenshot(img_path)
cutImg(img_path, element, dir, i)
cut_img(img_path, element, dir, i)
full_img_paths.append(img_path)
end_rect_bottom = driver.execute_script(
'return document.querySelector(".T_6Xj").getBoundingClientRect().bottom;')
end_rect_top = driver.execute_script('return document.querySelector(".T_6Xj").getBoundingClientRect().top;')
# end
if end_rect_top < window_height and end_rect_bottom > 0:
break
else:
# 完整email
img_path=f'{dir}/full_screen_email.png'
driver.save_screenshot(img_path)
@classmethod
def __join_images__(cls, png1, png2, size=0, output='result.png'):
cut_len=len(cut_img_paths)
first=0
second=1
print('cut_img_paths',cut_img_paths)
print('cut_len',cut_len)
# while second<cut_len:
# gen_img_path = f'{dir}/temp_result_{i}.png'
# if first ==0:
# img1_path=cut_img_paths[first]
# else:
# img1_path= gen_img_path
# join_images(img1_path,cut_img_paths[second],0,gen_img_path)
# second += 1
# print('second',second)
# 裁剪顶部高度 渲染高度
crop_height=driver.execute_script('return document.getElementById("ConversationReadingPaneContainer").firstChild.offsetHeight;')
# 8px padding
vertical_concatenate_with_crop(cut_img_paths, f'{dir}/result_email.png',crop_height+16)
def vertical_concatenate_with_crop(image_paths, output_path, crop_height=100):
"""
纵向拼接多图,并对第二张图裁剪指定高度
:param image_paths: 图片路径列表(按顺序从上到下)
:param output_path: 输出文件路径
:param crop_height: 第二张图裁剪的高度(默认 100px)
"""
images = []
# 读取并处理图片
for idx, path in enumerate(image_paths):
img = Image.open(path)
if idx >= 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
......@@ -108,11 +172,12 @@ class ScreenShot:
size = size * 2
img1, img2 = Image.open(png1), Image.open(png2)
size1, size2 = img1.size, img2.size
joint = Image.new(cls.__RGB__, (size1[0], size1[1] + size2[1] - 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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册