帮你简化一下。
from PIL import Image
import os
import imghdr
def is_valid_image(filename):
valid = True
try:
Image.open(filename).load()
except OSError:
valid = False
return valid
YOUR_IMAGE_FOLDER = "Z://Path//" # 更改成你要的资料夹
get_your_image_files = os.listdir(YOUR_IMAGE_FOLDER)
for (i, get_your_image_file) in enumerate(get_your_image_files):
print("Your image", i+1, get_your_image_file, ' result is: ', is_valid_image(YOUR_IMAGE_FOLDER+get_your_image_file))
https://i.imgur.com/Irh9xmY.jpg
https://i.imgur.com/5jcw0ns.jpg
或是
import os
import imghdr
YOUR_IMAGE_FOLDER = "Z://Path//" # 更改成你要的资料夹
get_your_image_files = os.listdir(YOUR_IMAGE_FOLDER)
for (i, get_your_image_file) in enumerate(get_your_image_files):
print("Your image:", i+1, get_your_image_file, ' is: ', imghdr.what(YOUR_IMAGE_FOLDER+get_your_image_file))
https://i.imgur.com/yerfD0M.jpg
https://i.imgur.com/3fW8PnG.jpg
※ 引述《s4028600 (佑)》之铭言:
: 我在网上爬到这个
: https://www.dust8.com/2017/04/22/python-broken-image/
: 我将他给的代码存成py档
: 放在图片堆中执行
: 就闪一下消失了
: 想要读取所有资料夹下的图片
: 或是读取同资料夹的所有图片
: 我要如何改造这个代码
: 求教学