Python41 深度学习常用库 PIL库(图像处理)
Python41 深度学习常用库 PIL库(图像处理)
·
一、展示图像、获得图像的处理模式和大小
"""
PIL库
安装pillow-pil
"""
from PIL import Image
import matplotlib.pyplot as plt
img=Image.open("qisui.jpg")
plt.imshow(img)
plt.show()
# 获得图像的模式
img_mode=img.mode
print(img_mode)
width,height=img.size
print(width,height)
二、图像旋转
# 对图片进行旋转
img_rotate=img.rotate(45)
plt.imshow(img_rotate)
plt.show()
三、图像剪切
# 对图片进行剪切
from PIL import Image
import matplotlib.pyplot as plt
img1=Image.open("qisui.jpg")
# 剪切图片 四个参数分别是(左上角点的x、y坐标 右下角点的x、y坐标)
img1_crop_result=img1.crop((126,0,381,249))
# 保存图片
img1_crop_result.save("sisui_crop_result.jpg")
# 展示图片
plt.imshow(img1_crop_result)
plt.show()
四、图像缩放
"""
图片的缩放
"""
from PIL import Image
import matplotlib.pyplot as plt
image=Image.open("qisui.jpg")
width,height=image.size
# 缩放
img2_resize_result=image.resize((int(width*0.6),int(height*0.6)))
print(img2_resize_result)
# 保存图片
img2_resize_result.save("qisui_resize_result.jpg")
# 展示图片
plt.imshow(img2_resize_result)
plt.show()
五、图片的左右旋转和图片的上下旋转
"""
图片的左右旋转和图片的上下旋转
"""
from PIL import Image
import matplotlib.pyplot as plt
from PIL.Image import Transpose
img=Image.open('qisui.jpg')
# 左右反转
img_lr=img.transpose(Transpose.FLIP_LEFT_RIGHT)
plt.imshow(img_lr)
plt.show()
# 上下翻转
img_bt=img.transpose(Transpose.FLIP_TOP_BOTTOM)
plt.imshow(img_bt)
plt.show()
更多推荐



所有评论(0)