這篇文章主要爲大家詳細介紹了python生成九宮格圖片,利用Image類將一張圖片分割成9張,具有一定的參考價值,感興趣的小夥伴們可以參考一下
本文實例爲大家分享了Python九宮格圖片生成的具體代碼,供大家參考,具體內容如下
利用Image類將一張圖片分割成9張,發朋友圈利器,打包成EXE後,長期使用。
效果大致是:
庫:pillow
源碼:
# pengyouquanPicture.py # 朋友圈九宮格圖片製作 from PIL import Image import sys # 先將input image 填充爲正方形 def fill_image(image): width, height = image.size #選取原圖片長、寬中較大值作爲新圖片的九宮格半徑 new_image_length = width if width > height else height #生產新圖片【白底】 new_image = Image.new(image.mode,(new_image_length, new_image_length), color='white') #將原圖粘貼在新圖上,位置爲居中 if width > height: new_image.paste(image,(0, int((new_image_length-heigth) / 2))) else: new_image.paste(image,(int((new_image_length-width) / 2), 0)) return new_image # 將圖片切割成九宮格 def cut_image(image): width, height = image.size #一行放3張圖 item_width = int(width / 3) box_list = [] for i in range(0,3): for j in range(0,3): box = (j*item_width,i*item_width,(j+1)*item_width,(i+1)*item_width) box_list.append(box) image_list = [image.crop(box) for box in box_list] return image_list #保存圖片 def save_images(image_list): index = 1 for image in image_list: image.save(str(index) + '.png', 'PNG') index += 1 if __name__ == '__main__': file_path = "1.jpg" image = Image.open(file_path) #image.show() image = fill_image(image) image_list = cut_image(image) save_images(image_list)
打包EXE:
pyinstaller.exe -F pengyouquanPicture.py -i "b8.ico"
把EXE文件和要分割的圖片放在一個路徑下,人後圖片重命名爲1.jpg ,直接執行exe 就可以得到9張照片啦。
PS:怎麼打包成APP,後面再研究研究。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。