1

我想將圖像剪切成12個相同的框,因爲Instagram移動應用顯示3x4的表格。在簡歷中,我想自動化Image Cutter在Instagram上製作馬賽克。 我下載了Pillow's library使用python自動剪切圖像

這是我寫的代碼:

from PIL import Image 

img = Image.open("Koala.jpg") 

x,y = img.size #assign x,y to image's valors 
a = round(x/3) #widht 
b = round(y/4) #height 

img2 = img.crop((0,0,a,b)) #assing variable img2 to the first box  
img2.save("img2.jpg")  #save the first box 

img3 = img.crop((a,0,a,b)) #assign variable img3 to the box next to img2  
img3.save("img3.jpg")  #save 

img4 = img.crop((2*a,0,a,b)) #same process 
img4.save("img4.jpg") 

我認爲這將使用循環很容易。對不起,我是一個noob,這是我在Python中的第一個腳本。

+0

所以 - 只要注意,沒有在Python需要可變信有一個字母。寫一個'a = round(x/3)#widht'來提醒你和其他人在判斷'a'包含寬度的時候,你可以直接使用'width'和'height'作爲變量名。 – jsbueno

回答

2

是,簡單地用一個雙for循環,

for i in range(3): 
    for j in range(4): 
     img_tmp = img.crop((i*a, j*b, (i+1)*a, (j+1)*b)) 
     img_tmp.save("img_{}_{}.jpg".format(i, j))