老大让把某个时间段的用户做成头像墙,还要做成某个特定形状的,如缩略图,那好吧,搞起来
# -*- coding:utf-8 -*-
import random
import PIL.Image as Image
import os
IMAGES_PATH = './img/' # 头像目录
IMAGES_FORMAT = ['.png'] # 图片格式
IMAGE_SIZE = 256 # 每张小图片的大小
# 大图的行列,乘积不能超过图片的数量!
IMAGE_ROW = 30
IMAGE_COLUMN = 30
# 输出路径
IMAGE_SAVE_PATH = './avatar/'
image_names = [name for name in os.listdir(IMAGES_PATH) for item in IMAGES_FORMAT if os.path.splitext(name)[1] == item]
# 图像拼接函数
def image_compose():
for i in range(1, int(len(image_names) / (IMAGE_ROW * IMAGE_COLUMN)) + 1):
to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE, IMAGE_ROW * IMAGE_SIZE))
for y in range(1, IMAGE_ROW + 1):
for x in range(1, IMAGE_COLUMN + 1):
from_image = Image.open(IMAGES_PATH + image_names[(i - 1) * IMAGE_ROW * IMAGE_COLUMN + IMAGE_COLUMN * (y - 1) + x - 1]).resize((IMAGE_SIZE, IMAGE_SIZE), Image.ANTIALIAS)
to_image.paste(from_image, ((x - 1) * IMAGE_SIZE, (y - 1) * IMAGE_SIZE))
to_image.save(IMAGE_SAVE_PATH + str(random.random()) + ".png")
if __name__ == '__main__':
image_compose()
然后,然后,然后组成的形状呢?? 抱歉,暂时没实现! 上面只是初级的代码,只好先用PS调一下吧,后面有时间再研究更多更复杂的功能。 如果读者有更快更好的算法,欢迎赐教!