幫你簡化一下。
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檔
: 放在圖片堆中執行
: 就閃一下消失了
: 想要讀取所有資料夾下的圖片
: 或是讀取同資料夾的所有圖片
: 我要如何改造這個代碼
: 求教學