作者s4028600 (佑)
看板Python
標題Re: [問題] 如何檢查圖片是否和毀損
時間Tue Jul 30 00:42:09 2019
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://webptt.com/m.aspx?n=bbs/Python/M.1564418531.A.5CA.html
1F:→ alvinlin: 請用 os.walk() 07/30 11:41
這樣嗎
可是無法讀取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
2F:→ s4028600: 話說這能做成直接執行嗎 在資料夾執行py檔就開始跑流程 07/30 15:48
3F:→ s4028600: 那種... 07/30 15:48