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

看板Python作者 (佑)時間6年前 (2019/07/30 00:42), 6年前編輯推噓0(003)
留言3則, 2人參與, 6年前最新討論串5/7 (看更多)
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結果失敗... -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.41.72.118 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1564418531.A.5CA.html

07/30 11:41, 6年前 , 1F
請用 os.walk()
07/30 11:41, 1F
這樣嗎 可是無法讀取D:\long\Desktop\image下的圖片 也只會讀取最後一個子資料夾中的圖片 沒有改的頭緒了... import os for dirPath, dirNames, fileNames in os.walk("D:\long\Desktop\image\\"): print (dirPath) path =(dirPath+"\\") 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)) os.system("pause") ※ 編輯: s4028600 (125.224.230.63 臺灣), 07/30/2019 15:26:32

07/30 15:48, 6年前 , 2F
話說這能做成直接執行嗎 在資料夾執行py檔就開始跑流程
07/30 15:48, 2F

07/30 15:48, 6年前 , 3F
那種...
07/30 15:48, 3F
文章代碼(AID): #1TFo7ZNA (Python)
文章代碼(AID): #1TFo7ZNA (Python)