python製作微信朋友圈九分圖
阿新 • • 發佈:2019-01-03
話不多說直接上效果
藉助於python的一個強大的圖形處理庫 PIL(python Image Library)
編碼思路:
1、建立大的底部正方形底板
2、將原圖按比例繪製上去
3、將新圖按照從左到右,從上到下順序取區域,切分
from PIL import Image def cutImage(): file_path = "img/Penguins.jpg" img = Image.open(file_path) width, height = img.size # 取最大值作為新圖的長寬 max_len = max(width,height) # 白底空白圖 new_img = Image.new(img.mode, (max_len, max_len), color="white") # 將原圖畫面按比例填充到新畫布上 if width > height: new_img.paste(img, (0, int((max_len - height)/2))) else: new_img.paste(img, (int((max_len - width)/2), 0)) # 九分圖中每個圖的長寬 cut_width = int(max_len / 3) index = 1 for i in range(3): for j in range(3): # 按照從左到右從上到下順序切分 box = (j*cut_width, i*cut_width, (j+1)*cut_width, (i+1)*cut_width) # crop 從影象中提取某個矩形大小的影象(左上座標,右下座標) cut_img = new_img.crop(box) cut_img.save('./img/'+str(index)+'.png','PNG') index += 1 if __name__ == "__main__": cutImage()
好了,要處理的圖片自備,趕快使用python試一試吧