Re: [问题] 如何检查图片是否和毁损

楼主: alvinlin (林矜业)   2019-07-30 18:57:54
试着把内容印出来就会找到解决方案。
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结果失败...
作者: s4028600 (佑)   2019-07-31 01:35:00
印出来...要怎么接到下一步...这个会连档案一起印出来 但是下面的代码又只吃资料夹路径...而且只读取最后读取的资料夹 没办法读取下一个资料夹...imghdr我查了一下是推测图像类型,但是无法判断图片有无缺损...

Links booklink

Contact Us: admin [ a t ] ucptt.com