1、先直接上代码
import random
from PIL import Image
import os
# 照片墙 背景 照片素材 图形
#定义图形
fingure = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
# 图片尺寸 像素
PIC_WIDTH, PIC_HEIGHT = 100,100
# 行数 列数
row_num, column_num = len(fingure),len(fingure[0])
# 背景读取 设置尺寸
img = Image.open("photo/bgimg/14.jpg").resize((column_num*PIC_WIDTH, row_num*PIC_HEIGHT))
#读取照片名 /photo/lolimage
image_names = os.listdir("photo/lolimage")
print(img, image_names)
for row in range(row_num):
for column in range(column_num):
if fingure[row][column]:
pic = Image.open("photo/lolimage/" + random.choice(image_names)).resize((PIC_WIDTH, PIC_HEIGHT))
img.paste(pic, (PIC_WIDTH * column, PIC_HEIGHT * row))
img.save("lol.png")
2、
from PIL import Image
3、我的文件夹
4、运行结果:图形以及图片可以定义