Python用imghdr模块识别图片格式
imghdr.what(img_file)
img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'}
代码函数段:即传图像目录路径或者图像路径返回图像路径列表
def get_image_file_list(img_file):imgs_lists = []if img_file is None or not os.path.exists(img_file):raise Exception("not found any img file in {}".format(img_file))img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'}if os.path.isfile(img_file) and imghdr.what(img_file) in img_end:imgs_lists.append(img_file)elif os.path.isdir(img_file):for single_file in os.listdir(img_file):file_path = os.path.join(img_file, single_file)if imghdr.what(file_path) in img_end:imgs_lists.append(file_path)if len(imgs_lists) == 0:raise Exception("not found any img file in {}".format(img_file))return imgs_lists