作者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/cn.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