基于Python實現(xiàn)圖片一鍵切割九宮格的工具
有時候發(fā)微博時候,需要裁切圖片為九宮格,但是ps或者其他工具都太麻煩,這里寫一個python一鍵切割九宮格的工具,以供大家學(xué)習(xí)和使用!
實現(xiàn)代碼
""" 1.將一張圖片填充為正方形 2.將文字加到方形圖片上 3.講圖片切為9張圖并存儲 """ import os from tkinter import filedialog from PIL import Image from future.moves import tkinter # 填充文字的庫 import PIL from PIL import ImageFont,Image,ImageDraw def open_img(): """ 打開圖片 :return: """ root = tkinter.Tk() # 創(chuàng)建一個Tkinter.Tk()實例 root.withdraw() # 將Tkinter.Tk()實例隱藏 default_dir = r"文件路徑" file_path = filedialog.askopenfilename(title=u'選擇文件', initialdir=(os.path.expanduser(default_dir))) if len(file_path) != 0: image = Image.open(file_path) fill_image(image) else: SystemExit() def fill_image(img): """ 將圖片填充為正方形 :param img: 圖片 :return: """ width, height = img.size # 選取長和寬中較大值作為新圖片的 new_image_length = width if width > height else height # 生成新圖片[白底] new_image = Image.new(img.mode, (new_image_length, new_image_length), color='white') # 將之前的圖粘貼在新圖上,居中 if width > height: # 原圖寬大于高,則填充圖片的豎直維度 # (x,y)二元組表示粘貼上圖相對下圖的起始位置 new_image.paste(img, (0, int((new_image_length - height) / 2))) else: new_image.paste(img, (int((new_image_length - width) / 2), 0)) # 圖片上寫上文字 # 設(shè)置字體,如果沒有,也可以不設(shè)置 font = ImageFont.truetype(r"C:\Windows\Fonts\STHUPO.TTF", 50) datas='V:xlzcm88或xlzcm66' bytedatas=datas.encode('UTF-8') draw = ImageDraw.Draw(new_image) draw.text((0,new_image.size[1]/2), u'V:xlzcm88或xlzcm66', font=font) cut_image(new_image) def cut_image(img): """ 切圖 :param img: 填充成方形后的圖片 :return: """ width, height = img.size item_width = int(width / 3) box_list = [] for i in range(0, 3): # 兩重循環(huán),生成9張圖片基于原圖的位置 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) img_list = [img.crop(box) for box in box_list] save_images(img_list) def save_images(img_list): """ 保存切割完成的圖片 :param img_list: 切割后的圖片列表 :return: """ index = 1 files_path = 'Pic' # 若文件夾不存在,則創(chuàng)建 if not os.path.exists(files_path): os.makedirs(files_path) for img in img_list: img.save('./Pic/' + str(index) + '.png', 'PNG') index += 1 print('完成') if __name__ == '__main__': open_img()
方法補(bǔ)充
除了上文的方法,小編還給大家整理了其他圖片切割成九宮格的方法,希望對大家有所幫助
# -*- coding: utf-8 -*- from PIL import Image import sys # 將圖片填充為正方形 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 - height) / 2))) # (x,y)二元組表示粘貼上圖相對下圖的起始位置 else: new_image.paste(image, (int((new_image_length - width) / 2), 0)) return new_image # 切圖 def cut_image(image): width, height = image.size item_width = int(width / 3) box_list = [] # (left, upper, right, lower) for i in range(0, 3): for j in range(0, 3): # print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width)) 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('./output/' + str(index) + '.jpg') index += 1 if __name__ == '__main__': file_path = "./output/girl.jpg" image = Image.open(file_path) image.show() image = fill_image(image) image_list = cut_image(image) save_images(image_list)
到此這篇關(guān)于基于Python實現(xiàn)圖片一鍵切割九宮格的工具的文章就介紹到這了,更多相關(guān)Python圖片切割九宮格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python編碼格式導(dǎo)致csv讀取錯誤問題(csv.reader, pandas.csv_read)
python編碼格式導(dǎo)致csv讀取錯誤問題(csv.reader, pandas.csv_read),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05Python編程使用DRF實現(xiàn)一次性驗證碼OTP
今天講一下如何用 Django REST framework[1](DRF) 來實現(xiàn) OTP,閱讀本文需要一定的 DRF 的基礎(chǔ)知識,有需要的朋友可以借鑒參考下2021-09-09python selenium對應(yīng)的瀏覽器chromedriver版本不一致問題
這篇文章主要介紹了python selenium對應(yīng)的瀏覽器chromedriver版本不一致問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08TensorFlow卷積神經(jīng)網(wǎng)絡(luò)之使用訓(xùn)練好的模型識別貓狗圖片
今天小編就為大家分享一篇關(guān)于TensorFlow卷積神經(jīng)網(wǎng)絡(luò)之使用訓(xùn)練好的模型識別貓狗圖片,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03django自帶serializers序列化返回指定字段的方法
今天小編就為大家分享一篇django自帶serializers序列化返回指定字段的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python 實現(xiàn)刪除某路徑下文件及文件夾的實例講解
下面小編就為大家分享一篇Python 實現(xiàn)刪除某路徑下文件及文件夾的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04