from PIL import Image
import os
# def load_images_from_folder(folder_path):
# # 获取文件夹中所有图片的路径,并加载它们
# images = [Image.open(os.path.join(folder_path, file_name))
# for file_name in os.listdir(folder_path)
# if file_name.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
# # 假设图片在文件夹内是无序的,我们不对其进行排序
# return images
def load_images_from_folder(folder_path):
# 获取文件夹中所有图片的路径,并按字母顺序排序
files = [file_name for file_name in sorted(os.listdir(folder_path))]
print(files)
images = [Image.open(os.path.join(folder_path, file_name))
for file_name in files
if file_name.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
return images
# def combine_images_in_rows(images, max_width_per_row, padding):
# num_images = len(images)
# num_rows = (num_images + max_width_per_row - 1) // max_width_per_row
#
# # 计算带间隔的总宽度和高度
# width, height = images[0].size
# combined_image_width = (max_width_per_row * width) + ((max_width_per_row - 1) * padding)
# combined_image_height = (num_rows * height) + ((num_rows - 1) * padding)
#
# # 创建一个白色背景的空白图像
# mode = 'RGB' # 确保模式是RGB以便使用白色背景
# combined_image = Image.new(mode, (combined_image_width, combined_image_height), 'white')
#
# current_x = padding // 2 # 起始位置为padding的一半,以居中对齐
# current_y = padding // 2
# for idx, image in enumerate(images):
# # 调整图片大小以适应间隔
# image = image.resize((width + padding, height + padding), Image.ANTIALIAS)
# position = (current_x, current_y)
# combined_image.paste(image, position)
#
# # 更新位置信息,准备下一张图片
# current_x += width + padding
#
# # 换行逻辑
# if (idx + 1) % max_width_per_row == 0:
# current_x = padding // 2
# current_y += height + padding
#
# return combined_image
def combine_images_in_rows(images, max_width_per_row, padding):
num_images = len(images)
num_rows = (num_images + max_width_per_row - 1) // max_width_per_row
# 计算带间隔的总宽度和高度
width, height = images[0].size
combined_image_width = (max_width_per_row * (width + padding * 2)) - padding # 两侧都要加间隔
combined_image_height = (height + padding) * num_rows - padding # 顶部不需要间隔
# 创建一个白色背景的空白图像
mode = 'RGB' # 确保模式是RGB以便使用白色背景
combined_image = Image.new(mode, (combined_image_width, combined_image_height), 'white')
current_x = padding # 起始位置为padding
current_y = padding
for idx, image in enumerate(images):
# 不需要调整图片大小以适应间隔,只需直接粘贴
position = (current_x, current_y)
combined_image.paste(image, position)
# 更新位置信息,准备下一张图片
current_x += width + padding * 2 # 包括两侧的间隔
# 换行逻辑
if (idx + 1) % max_width_per_row == 0:
current_x = padding # 重置为第一列的间隔
current_y += height + padding # 向下移动一个图片加间隔的高度
return combined_image
# 不同的文件夹列表
folders = [
r'E:\EGE-UNet\hello\isic2017photo\131',
r'E:\EGE-UNet\hello\isic2017photo\238',
r'E:\EGE-UNet\hello\isic2017photo\281',
r'E:\EGE-UNet\hello\isic2017photo\317'
]
# 从每个文件夹中加载图片,并合并所有图片列表
# all_images = []
# for folder in folders:
# all_images.extend(load_images_from_folder(folder))
all_images = []
for folder in folders:
# 确保每个文件夹都使用相同的原始顺序加载图片
all_images.extend(load_images_from_folder(folder))
# 每张图片之间的间隔(单位:像素)
padding = 10
# 每行可以容纳的图片最大宽度
max_width_per_row = 14
# 合并图片
combined_image = combine_images_in_rows(all_images, max_width_per_row, padding)
# 保存合并后的图片
output_path = '2017.jpg'
combined_image.save(output_path)
print(f'合并后的图片已保存到 {output_path}')
图像拼合的代码。一个文件夹是一行
最新推荐文章于 2024-10-06 13:30:57 发布