使用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)RGBJPEG
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 = 310 # ROTATE_270 = 411 # TRANSPOSE = 512 # show_img(img1)