作者alvinlin (艾尔文)
看板Python
标题Re: [问题] 如何检查图片是否和毁损
时间Tue Jul 30 18:57:54 2019
试着把内容印出来就会找到解决方案。
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结果失败...
-----
Sent from JPTT on my iPhone
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 123.192.186.172 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1564484276.A.8A3.html
※ 编辑: alvinlin (123.192.186.172 台湾), 07/30/2019 19:02:05
1F:推 s4028600: 印出来...要怎麽接到下一步... 07/31 01:35
2F:推 s4028600: 这个会连档案一起印出来 但是下面的代码又只吃资料夹路 07/31 01:41
3F:→ s4028600: 径... 07/31 01:41
4F:推 s4028600: 而且只读取最後读取的资料夹 没办法读取下一个资料夹... 07/31 01:46
5F:推 s4028600: imghdr我查了一下是推测图像类型,但是无法判断图片有无 07/31 01:55
6F:→ s4028600: 缺损... 07/31 01:55