本文實(shí)例為大家分享了Python九宮格圖片生成的具體代碼,供大家參考,具體內(nèi)容如下
利用Image類將一張圖片分割成9張,發(fā)朋友圈利器,打包成EXE后,長期使用。
效果大致是:
庫:pillow
源碼:
# pengyouquanPicture.py# 朋友圈九宮格圖片制作 from PIL import Imageimport sys # 先將input image 填充為正方形def fill_image(image): width, height = image.size #選取原圖片長、寬中較大值作為新圖片的九宮格半徑 new_image_length = width if width > height else height #生產(chǎn)新圖片【白底】 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文件和要分割的圖片放在一個(gè)路徑下,人后圖片重命名為1.jpg ,直接執(zhí)行exe 就可以得到9張照片啦。
PS:怎么打包成APP,后面再研究研究。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林站長站。
新聞熱點(diǎn)
疑難解答
圖片精選