from PIL import Image
from matplotlib import pyplot as plt
dir = 'C:/Users/user/Desktop/data/img.jpg'
img=Image.open(dir)
img.show() # 调用windows照片查看器
plt.imshow(img) # 嵌入到开发环境比如jupyter中显示图片
plt.show()
dir_ = 'C:/Users/user/Desktop/data/new_img.jpg'
img.save(dir_)
dir_ = 'C:/Users/user/Desktop/data/new_img'
img.save(dir_,format='jpg')
上面是静态指定了图片存储时用的新名,想要实现动态命名新名字的功能,先按如下尝试:
import os
dir_ = 'C:/Users/user/Desktop/data/'
for i in range(10):
save_dir = os.path.join(dir_,str(i))
img.save(save_dir,'jpg')
报错,KeyError,和其它一些信息,未解决。改为如下实现:
dir_ = 'C:/Users/user/Desktop/data/'
for i in range(10):
save_dir = dir_ + '{}.jpg'.format(i+1) # ‘+’字符串连接
img.save(save_dir)