python PIL 生成照片墙
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                python PIL 生成照片墙
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                特殊節日,先水一篇,后續補充詳細解釋,可以生成照片墻
import sys, os, shutil, math from PIL import Image import randomrow = 8 col = 16 nw = 400 nh = 400path = os.getcwd() # 獲得當前路徑def generate_img_tmp():if os.path.exists('tmp'): # tmpshutil.rmtree('tmp')os.makedirs('tmp')file_ls = os.listdir(path + "/picture") # 圖片所在文件夾i = 0 # 圖片計數for file in file_ls:name, extension = os.path.splitext(file)if (extension == '.png' or extension == '.jpg' or extension == '.jpeg') and name != 'splicing_picture': # select the imagei += 1print('%s...%s%s' % (i, name, extension))os.chdir(path)im = Image.open(path + "/picture/" + file)w, h = im.sizeif nw == nh:if w >= h:box = ((w - h) // 2, 0, (w + h) // 2, h)else:box = (0, (h - w) // 2, w, (h + w) // 2)region = im.crop(box)else:region = imsname = '%s%s' % (str(i), '.png')os.chdir('tmp')region.save(sname, 'png')def generate_img_name(num):return str(random.randint(1, num))def generate_img_wall():os.chdir(path)os.chdir('tmp')picture_len = len(os.listdir(path + "/tmp"))dest_im = Image.new('RGBA', (col * nw, row * nh),(255, 255, 255))for x in range(1, col + 1):for y in range(1, row + 1):try:print(x, y)src_im = Image.open("%s.png" % generate_img_name(picture_len)) # 隨機返回圖片名resize_im = src_im.resize((nw, nh), Image.ANTIALIAS) # 重置大小dest_im.paste(resize_im, ((x - 1) * nw, (y - 1) * nh)) # 貼圖except IOError:passfor x in range(1, col + 1):for y in range(1, row + 1):print(x, y)try:if x % 4 == 0 and y % 5 == 0:src_im = Image.open("%s.png" % generate_img_name(picture_len))resize_im = src_im.resize((nw * 2, nh * 2), Image.ANTIALIAS)dest_im.paste(resize_im, ((x - 1) * nw, (y - 1) * nh))except IOError:passos.chdir(path) # ensure the pathdest_im.save('photo_wall.png', 'png')dest_im.show() # finishif __name__ == "__main__":generate_img_tmp()generate_img_wall()總結
以上是生活随笔為你收集整理的python PIL 生成照片墙的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 机器学习理论入门:第二章 经典监督学习算
 - 下一篇: 交互式计算机图形学总结:第一章 图形系统