原图片
生成的图片
# -*- coding: utf-8 -*-
from PIL import Image
'''
srouce_file:原图路径
out_file:保存的路径
gap_width:图片间隔
gap_color:间隔颜色,参考Image.new()函数
x_num:水平方向数量
y_num:竖直方向数量
mode:参考Image.new()函数
'''
def picture_puzzle(srouce_file, out_file, gap_width, gap_color, x_num, y_num, mode):
im = Image.open(srouce_file)
im1 = Image.new(mode, (im.size[0] + gap_width * (x_num - 1), im.size[1] + gap_width * (y_num - 1)), gap_color)
for i in range(0, x_num):
for j in range(0, y_num):
sim = im.crop((i * im.size[0] / x_num, j * im.size[1] / y_num, (i + 1) * im.size[0] / x_num, (j + 1) * im.size[1] / y_num))
im1.paste(sim, tuple(map(int,(i * (im.size[0] / x_num + gap_width), j * (im.size[1] / y_num + gap_width),
i * (im.size[0] / x_num + gap_width) + im.size[0] / x_num,
j * (im.size[1] / y_num + gap_width) + im.size[1] / y_num))))
im1.save(out_file)
if __name__ == '__main__':
picture_puzzle('bc.jpg','out.jpg',20,'white',5,5,'RGB')