diff --git a/src/cluster/video_apps/make_grid_upload.py b/src/cluster/video_apps/make_grid_upload.py index e75c526..66df8dd 100644 --- a/src/cluster/video_apps/make_grid_upload.py +++ b/src/cluster/video_apps/make_grid_upload.py @@ -45,40 +45,8 @@ with downloader_image.imports(): :return: 图片路径 """ try: - cell_height = image_size + text_height - cell_width = image_size - # 提取图片路径和文字说明 - image_paths = [] - captions = [] - for info in image_info_list: - captions.append(info["title"]) - if "resize:200" in info["cover"]: - info["cover"] = info["cover"].replace("resize:200", f"resize:{image_size}") - image_paths.append(info["cover"]) - - # 检查输入 - if len(image_paths) != len(captions): - raise ValueError("图片数量与文字说明数量不匹配") - - # 处理图片(包括从网络获取) - loaded_images = [] - for path in image_paths: - if path.startswith(('http://', 'https://')): - try: - response = requests.get(path) - response.raise_for_status() - img = Image.open(BytesIO(response.content)) - loaded_images.append(img) - except requests.RequestException as e: - raise FileNotFoundError(f"无法从网络获取图片: {path}, 错误: {e}") - elif os.path.exists(path): - img = Image.open(path) - loaded_images.append(img) - else: - raise FileNotFoundError(f"找不到图片文件: {path}") - # 计算网格大小 - n = len(image_paths) + n = len(image_info_list) max_cols = min(4, n) # 最大列数为6或图片数量 min_rows = math.ceil(n / max_cols) # 最小行数 @@ -100,6 +68,39 @@ with downloader_image.imports(): if best_rows > 4 or best_cols > 4: raise ValueError(f"图片数量({n})过多,无法在4x4网格内合理展示") + cell_height = image_size + text_height + cell_width = image_size + # 提取图片路径和文字说明 + image_paths = [] + captions = [] + for info in image_info_list: + captions.append(info["title"]) + if "resize:200" in info["cover"]: + info["cover"] = info["cover"].replace("resize:200", f"resize:{image_size}") + image_paths.append(info["cover"]) + + # 检查输入 + if len(image_paths) != len(captions): + raise ValueError("图片数量与文字说明数量不匹配") + + # 处理图片(包括从网络获取) + loaded_images = [] + for path in image_paths: + if path.startswith(('http://', 'https://')): + try: + async with httpx.AsyncClient(timeout=60) as client: + response = await client.get(path) + response.raise_for_status() + img = Image.open(BytesIO(response.content)) + loaded_images.append(img) + except requests.RequestException as e: + raise FileNotFoundError(f"无法从网络获取图片: {path}, 错误: {e}") + elif os.path.exists(path): + img = Image.open(path) + loaded_images.append(img) + else: + raise FileNotFoundError(f"找不到图片文件: {path}") + # 创建画布 canvas_width = best_cols * (cell_width + separator) - separator canvas_height = best_rows * (cell_height + separator) - separator