使用PIL.Image進行簡單的圖像處理
1 # coding=utf-8 2 3 from PIL import Image 4 import matplotlib.pyplot as plt 5 6 def show_img(img): 7 plt.figure('Image') 8 plt.imshow(img) 9 plt.axis('off') # 關閉坐標軸 10 plt.show() 11 12 13 '''載入&存儲''' 14 15 img1 = Image.open('./bg-body-3.jpg') 16 img1.save('./保存的圖片.png', 'png') 17 18 19 '''基本屬性展示''' 20 21 print(img1.size) # 圖片尺寸 22 print(img1.mode) # 色彩模式 23 print(img1.format) # 圖片格式
(1920, 983) RGB JPEG
1 '''裁剪&旋轉''' 2 3 box = (1000,200,1500,800) 4 region = img1.crop(box) # 裁剪 5 region = region.transpose(Image.FLIP_TOP_BOTTOM) # 翻轉 6 img1.paste(region,box) # 粘貼 7 show_img(img1)
1 img1 = img1.rotate(180) # 旋轉 2 show_img(img1) 3 4 # 各種變形方式 5 img1 = img1.transpose(Image.FLIP_TOP_BOTTOM) 6 # FLIP_LEFT_RIGHT = 0 7 # FLIP_TOP_BOTTOM = 1 8 # ROTATE_90 = 2 9 # ROTATE_180 = 3 10 # ROTATE_270 = 4 11 # TRANSPOSE = 5 12 # show_img(img1)