作者alvinlin (艾爾文)
看板Python
標題Re: [問題] 如何檢查圖片是否和毀損
時間Mon Jul 29 02:43:24 2019
幫你簡化一下。
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檔
: 放在圖片堆中執行
: 就閃一下消失了
: 想要讀取所有資料夾下的圖片
: 或是讀取同資料夾的所有圖片
: 我要如何改造這個代碼
: 求教學
-----
Sent from JPTT on my iPhone
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.192.186.172 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1564339406.A.0E8.html
推 s4028600: 雖然和我想的用法不太一樣 但是感謝 07/29 17:03
→ s4028600: 只是不知道為什麼都是false 07/29 17:04
→ s4028600: 只有第1個代碼不會錯誤 07/29 17:05
→ s4028600: ils/77943578 07/29 19:04
→ s4028600: 但是不知道如何指定資料夾而失敗... 07/29 19:05
→ alvinlin: 你的圖用小畫家打得開嗎? 07/30 11:25
→ alvinlin: 要不你把//改成\試試.為啥你的資料夾和我的方向不同 07/30 11:37
推 s4028600: 斜線不同原來會影響嗎 只在版上寫過一次 而且沒有指定 07/30 15:32
→ s4028600: 路徑 是直接執行的 所以沒注意到... 07/30 15:32