python 兼容中文路径 + 目标文件是否是图像格式判断

1. 中文路径兼容


python程序如果路径中包含中文字符,不加处理会有类似报错:


'ascii' codec can't decode byte 0xxx in position xx:ordinal not in range(128)


解决方法:


path = unicode(path,'utf-8')




2. 文件是否是图像格式判断



python中的标准库 imghdr 可以用来判断文件是否是图片, imghdr.what(target) 返回 target 的图片类型,如果不是图片,返回 None 。

import imghdr
import glob

paths = glob.glob(os.path.join(input_folder,'*.*'))
imgType_list = {'jpg','bmp','png','jpeg','rgb','tif'}

for item in paths:
    if imghdr.what(item) not in imgType_list:
        print('Not image type!')
        continue
 
原文地址:https://www.cnblogs.com/mtcnn/p/9411701.html