Re: [問題] 如何檢查圖片是否和毀損
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
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
07/30 15:48, 2F
→
07/30 15:48,
6年前
, 3F
07/30 15:48, 3F
討論串 (同標題文章)
完整討論串 (本文為第 5 之 7 篇):
Python 近期熱門文章
PTT數位生活區 即時熱門文章