D:\Wdpython\environment\Scripts\python.exe D:/Wdpython/爬虫/测试8.py
图片列表 10 [‘刘亦菲/刘亦菲_1.jpg’, ‘刘亦菲/刘亦菲_11.jpg’, ‘刘亦菲/刘亦菲_12.jpg’, ‘刘亦菲/刘亦菲_13.jpg’, ‘刘亦菲/刘亦菲_15.jpg’, ‘刘亦菲/刘亦菲_2.jpg’, ‘刘亦菲/刘亦菲_3.jpg’, ‘刘亦菲/刘亦菲_4.jpg’, ‘刘亦菲/刘亦菲_5.jpg’, ‘刘亦菲/刘亦菲_8.jpg’]
x_list [1454.0, 1472.0, 1473.0, 1479.0, 1479.0, 1479.0, 1479.0, 1481.0, 1483.0, 1484.0]
y_list [826.0, 834.0, 920.0, 926.0, 1336.0, 1800.0, 1971.0, 1972.0, 1972.0, 2222.0]
x_new, y_new 1479 1800
x_s 1454 1336
x_s 1481 2222
x_s 1479 1972
x_s 1479 1972
x_s 1479 1971
x_s 1483 926
x_s 1473 826
x_s 1484 834
x_s 1479 1800
x_s 1472 920
图片已经合成了
进程已结束,退出代码0
# @Author : 小红牛
# 微信公众号:wdPython
import os
import PIL.Image as Image
def resize_by_width(infile, image_size):
"""按照宽度进行所需比例缩放"""
im = Image.open(infile)
(x, y) = im.size
lv = round(x / image_size, 2) + 0.01
x_s = int(x // lv)
y_s = int(y // lv)
print("x_s", x_s, y_s)
out = im.resize((x_s, y_s), Image.LANCZOS)
return out
def get_new_img_xy(infile, image_size):
"""返回一个图片的宽、高像素"""
im = Image.open(infile)
(x, y) = im.size
lv = round(x / image_size, 2) + 0.01
x_s = x // lv
y_s = y // lv
# print("x_s", x_s, y_s)
# out = im.resize((x_s, y_s), Image.ANTIALIAS)
return x_s, y_s
# 定义图像拼接函数
def image_compose(image_colnum, image_size, image_rownum, image_names, image_save_path, x_new, y_new):
to_image = Image.new('RGB', (image_colnum * x_new, image_rownum * y_new)) # 创建一个新图
# 循环遍历,把每张图片按顺序粘贴到对应位置上
total_num = 0
for y in range(1, image_rownum + 1):
for x in range(1, image_colnum + 1):
from_image = resize_by_width(image_names[image_colnum * (y - 1) + x - 1], image_size)
# from_image = Image.open(image_names[image_colnum * (y - 1) + x - 1]).resize((image_size,image_size ), Image.ANTIALIAS)
to_image.paste(from_image, ((x - 1) * x_new, (y - 1) * y_new))
total_num += 1
if total_num == len(image_names):
break
to_image.save(image_save_path) # 保存新图
def get_image_list_fullpath(dir_path):
file_name_list = os.listdir(dir_path)
image_fullpath_list = []
for file_name_one in file_name_list:
file_one_path = os.path.join(dir_path, file_name_one)
if os.path.isfile(file_one_path):
image_fullpath_list.append(file_one_path)
else:
img_path_list = get_image_list_fullpath(file_one_path)
image_fullpath_list.extend(img_path_list)
return image_fullpath_list
def merge_images(image_dir_path, image_size, image_colnum,image_save_path):
# 获取图片集地址下的所有图片名称
image_fullpath_list = get_image_list_fullpath(image_dir_path)
print("图片列表", len(image_fullpath_list), image_fullpath_list)
# image_rownum = 4 # 图片间隔,也就是合并成一张图后,一共有几行
image_rownum_yu = len(image_fullpath_list) % image_colnum
if image_rownum_yu == 0:
image_rownum = len(image_fullpath_list) // image_colnum
else:
image_rownum = len(image_fullpath_list) // image_colnum + 1
x_list = []
y_list = []
for img_file in image_fullpath_list:
img_x, img_y = get_new_img_xy(img_file, image_size)
x_list.append(img_x)
y_list.append(img_y)
print("x_list", sorted(x_list))
print("y_list", sorted(y_list))
x_new = int(x_list[len(x_list) // 5 * 4])
y_new = int(y_list[len(y_list) // 5 * 4])
print(" x_new, y_new", x_new, y_new)
image_compose(image_colnum, image_size, image_rownum, image_fullpath_list, image_save_path, x_new, y_new) # 调用函数
print('图片已经合成了')
if __name__ == '__main__':
image_dir_path = '刘亦菲/' # 图片集地址
image_size = 1200 # 每张小图片的大小
image_colnum = 3 # 合并成一张图后,一行有几个小图
image_save_path = '刘亦菲/合成图.png' # 图片转换后的地址
merge_images(image_dir_path, image_size, image_colnum, image_save_path)