Re: [問題] 如何檢查圖片是否和毀損

看板Python作者 (艾爾文)時間6年前 (2019/07/29 02:43), 編輯推噓4(407)
留言11則, 2人參與, 6年前最新討論串4/7 (看更多)
幫你簡化一下。 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

07/29 17:03, 6年前 , 1F
雖然和我想的用法不太一樣 但是感謝
07/29 17:03, 1F

07/29 17:04, 6年前 , 2F
只是不知道為什麼都是false
07/29 17:04, 2F

07/29 17:05, 6年前 , 3F
只有第1個代碼不會錯誤
07/29 17:05, 3F

07/29 17:47, 6年前 , 4F
07/29 17:47, 4F

07/29 19:04, 6年前 , 5F

07/29 19:04, 6年前 , 6F
ils/77943578
07/29 19:04, 6F

07/29 19:05, 6年前 , 7F
但是不知道如何指定資料夾而失敗...
07/29 19:05, 7F

07/30 11:25, 6年前 , 8F
你的圖用小畫家打得開嗎?
07/30 11:25, 8F

07/30 11:37, 6年前 , 9F
要不你把//改成\試試.為啥你的資料夾和我的方向不同
07/30 11:37, 9F

07/30 15:32, 6年前 , 10F
斜線不同原來會影響嗎 只在版上寫過一次 而且沒有指定
07/30 15:32, 10F

07/30 15:32, 6年前 , 11F
路徑 是直接執行的 所以沒注意到...
07/30 15:32, 11F
文章代碼(AID): #1TFUpE3e (Python)
討論串 (同標題文章)
文章代碼(AID): #1TFUpE3e (Python)