『Python』PIL图像处理_形变操作

 使用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)

原文地址:https://www.cnblogs.com/hellcat/p/6854929.html