[TIL#10] Python 自學 day10 創造圖片的拼貼-PIL crop

Hi 各位學習python的朋友們, 今天要分享大家在python學習路上可能會想做的應用~如何做照片的拼貼或者剪取需要的圖片區域!
讓大家可以自行設計,酷炫的照片拼貼,放在facebook、instagram、Line當作背景圖,那就讓我們開始吧!

Step1: 將照片讀取至python環境中,假設現在有張紅點的圖片,放在
路徑為F:\python-practice\til10\\,檔名為til10_example_1.png

from PIL import Image
I = Image.open(r'F:\python-practice\til10\\til10_example_1.png')
I.show()

Step2: 紅點旁邊白邊太多,需要整理成適當大小,盡量減少白邊的部分,溫馨提示
可以使用小畫家,看看需要保留的區域起點與終點。

I = Image.open(r'F:\python-practice\til10\\til10_example_1.png')
width, height = I.size
print("Original Image size windth and height",width,height)
left = 100
top = 65
right = 150
bottom = 112
im1 = I.crop((left, top, right, bottom))
width, height = im1.size
print("New Image size windth and height",width,height)
im1.save( r'.\\'+ "samll_image.png")
Console輸出結果:
Original Image size windth and height 261 169
New Image size windth and height 50 47
我們成功將白邊刪去,將圖片保流中心原點部分,並將處理後的圖片,存成新圖片
small_image.png,主要就是使用PIL 中圖片.crop的function,輸入圖片的
left、top、rigth和bottom位置,就會自動剪裁了!

Step3: 瓦特歐這邊用9個不同顏色的點當作拼貼,接續上面剪取圖片大小的方式,
寫成一個對應的function: Image_Cut,省者大家去重複寫相同的程式碼,glob.glob是可以將資料夾內的路徑給尋找出來的function

def Image_Cut(ori_image):
    width, height = ori_image.size
    print("Image size windth and height",width,height)
    left = 100
    top = 65
    right = 150
    bottom = 112
    im1 = ori_image.crop((left, top, right, bottom))
    width, height = im1.size
    print("After cut Image size windth and height",width,height)
    return im1

image_path= glob.glob(r'F:\python-practice\til10\*.png')
print("image_path:",len(image_path))

I_Width=50
I_High=47

Merge_Image = Image.new('RGB', (I_Width*3, I_High*3))
temp_row_counter=0
temp_col_counter=0

for temp_path in image_path:
    print(temp_path)
    print(temp_row_counter,temp_col_counter)
    I1 = Image.open(temp_path)
    I1 = Image_Cut(I1)
    Merge_Image.paste(I1, (I_Width*temp_row_counter, I_High*temp_col_counter ,(I_Width*(temp_row_counter+1)), (I_High*(temp_col_counter+1))))
    temp_row_counter = temp_row_counter + 1
    if(temp_row_counter > 2):
        temp_row_counter = 0
        temp_col_counter = temp_col_counter+ 1
    
Merge_Image.save( r'.\\'+ "merged.png")   
Console 輸出結果:
image_path: 9
F:\python-practice\til10\til10_example_1.png
0 0
Image size windth and height 261 169
After cut Image size windth and height 50 47
F:\python-practice\til10\til10_example_2.png
1 0
Image size windth and height 261 169
After cut Image size windth and height 50 47
F:\python-practice\til10\til10_example_3.png
2 0
Image size windth and height 261 169
After cut Image size windth and height 50 47
F:\python-practice\til10\til10_example_4.png
0 1
Image size windth and height 261 169
After cut Image size windth and height 50 47
F:\python-practice\til10\til10_example_5.png
1 1
Image size windth and height 261 169
After cut Image size windth and height 50 47
F:\python-practice\til10\til10_example_6.png
2 1
Image size windth and height 261 169
After cut Image size windth and height 50 47
F:\python-practice\til10\til10_example_7.png
0 2
Image size windth and height 261 169
After cut Image size windth and height 50 47
F:\python-practice\til10\til10_example_8.png
1 2
Image size windth and height 261 169
After cut Image size windth and height 50 47
F:\python-practice\til10\til10_example_9.png
2 2
Image size windth and height 261 169
After cut Image size windth and height 50 47
經由上方Console可以觀察到,陸續將照片讀取進python環境,並且剪成對應大小
,我們成功將9張圖片,整合成一張merge.png!

瓦特歐Python介紹系列:
[TIL#1] Python 自學 day1 Anaconda
[TIL#2] Python 自學 day2 變數
[TIL#3] Python 自學 day3 流程控制
[TIL#4] Python 自學 day4 製作執行檔
[TIL#5] Python 自學 day5 執行檔更換icon
[TIL#6] Python 自學 day6 PIL浮水印、圖片大小變更
[TIL#7] Python 自學 day7 大量圖片Resize 處理 懶人包
[TIL#8] Python 自學 day8 GUI製作- 使用Tkinter Grid管理器
[TIL#9] Python 自學 day9 GUI製作 放入圖片 grid 版本
[TIL#10] Python 自學 day10 創造圖片的拼貼
[TIL#11] Python 自學 day11 matplotlib 統計圖、長條圖、圓餅圖、散佈圖


Leave a Comment