天天看點

python微信朋友圈刷圖_python制作微信朋友圈九分圖

話不多說直接上效果

python微信朋友圈刷圖_python制作微信朋友圈九分圖

借助于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微信朋友圈刷圖_python制作微信朋友圈九分圖

好了,要處理的圖檔自備,趕快使用python試一試吧