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

看板Python作者 (艾爾文)時間6年前 (2019/07/30 18:57), 6年前編輯推噓4(402)
留言6則, 1人參與, 6年前最新討論串6/7 (看更多)
試著把內容印出來就會找到解決方案。 import os import imghdr path = 'Z://oswalk' for root, dirs, files in os.walk(path): for f in files: print(os.path.join(root, f), imghdr.what(os.path.join(root, f))) https://i.imgur.com/uUssjxt.jpg
※ 引述《s4028600 (佑)》之銘言: : import os : path = 'D:\long\Desktop\image\\' : def is_valid_jpg(jpg_file): : with open(jpg_file, 'rb') as f: : f.seek(-2, 2) : buf = f.read() : return buf == b'\xff\xd9' : def is_valid_png(png_file): : with open(png_file, 'rb') as f: : f.seek(-2, 2) : buf = f.read() : return buf == b'\x60\x82' : for file in os.listdir(path): : pic_file = path + file : isJpg = is_valid_jpg(pic_file) : isPng = is_valid_png(pic_file) : print("jpeg : %s, png %s, file %s " % (isJpg, isPng, file)) : 之前找到的就算能讀取 : 也無法正確判斷 : 這是我重新找到的 : 稍微修改成一個資料夾下的圖片 : 問題來了 : 我如果要讀取一個資料夾下的所有子資料夾下的圖片 : 要怎麼改 : 原本是打算用glob.glob結果失敗... ----- Sent from JPTT on my iPhone -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.192.186.172 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1564484276.A.8A3.html ※ 編輯: alvinlin (123.192.186.172 臺灣), 07/30/2019 19:02:05

07/31 01:35, 6年前 , 1F
印出來...要怎麼接到下一步...
07/31 01:35, 1F

07/31 01:41, 6年前 , 2F
這個會連檔案一起印出來 但是下面的代碼又只吃資料夾路
07/31 01:41, 2F

07/31 01:41, 6年前 , 3F
徑...
07/31 01:41, 3F

07/31 01:46, 6年前 , 4F
而且只讀取最後讀取的資料夾 沒辦法讀取下一個資料夾...
07/31 01:46, 4F

07/31 01:55, 6年前 , 5F
imghdr我查了一下是推測圖像類型,但是無法判斷圖片有無
07/31 01:55, 5F

07/31 01:55, 6年前 , 6F
缺損...
07/31 01:55, 6F
文章代碼(AID): #1TG2AqYZ (Python)
文章代碼(AID): #1TG2AqYZ (Python)