今天写了一个批量读取并显示图片的代码,当做练习,方便以后拿来使用。
import imageio import os import matplotlib.pyplot as plt filepath ="F:/相册/自己/" filename = os.listdir(filepath) for i in filename: image = i.strip().split(".") #这条语句完全是为了练习split方法。 try: img = image[0]+".jpg" imag = os.path.join(filepath,img) im = imageio.imread(imag) except FileNotFoundError: print("出现一个文件读错误") img = image[0]+".PNG" imag = os.path.join(filepath,img) im = imageio.imread(imag) else: print("没有异常") plt.imshow(im) plt.show()