对2个图片路径a和b的图片进行上下拼接,存成新图片
- User
对2个图片路径a和b的图片进行上下拼接,存成新图片
Assistant
一个简单的图像处理需求!
你可以使用 Python 的 Pillow 库(PIL)轻松实现图片的上下拼接。下面是示例代码:
from PIL import Image def concat_images_vertically(img_path_a, img_path_b, output_path): """ 上下拼接两张图片并保存为新图片 :param img_path_a: 图片A路径 :param img_path_b: 图片B路径 :param output_path: 输出新图片的路径 """ # 打开图片A和B img_a = Image.open(img_path_a) img_b = Image.open(img_path_b) # 确保两张图片宽度一致(如果不一致,会以宽的图片为准,窄的图片居中) width = max(img_a.width, img_b.width) height = img_a.height + img_b.height # 总高度是两张图片高度之和 # 创建一张新的空白图片,尺寸为:宽(以最宽的为准),高(两张图片高度之和) new_img = Image.new('RGB', (width, height)) # 计算图片A和B的粘贴位置(居中) paste_x_a = (width - img_a.width) // 2 paste_y_a = 0 paste_x_b = (width - img_b.width) // 2 paste_y_b = img_a.height # 图片B的Y坐标是图片A的高度 # 粘贴图片A和B到新图上 new_img.paste(img_a, (paste_x_a, paste_y_a)) new_img.paste(img_b, (paste_x_b, paste_y_b)) # 保存新图片 new_img.save(output_path) # 示例调用 img_path_a = 'a.jpg' # 图片A路径 img_path_b = 'b.jpg' # 图片B路径 output_path = 'concat_ab.jpg' # 输出新图片路径 concat_images_vertically(img_path_a, img_path_b, output_path)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.