识别图片的类型imghdr库
1.识别图片的类型和输出图片的base64
import imghdr
import base64
import os
if __name__ == '__main__':
path = r'1.jpg'
with open(path, 'rb') as img_file:
if not os.path.exists(path):
print('图片文件不存在!\n程序运行结束!')
exit()
else:
# 如果图片不存在,则imghdr.what会报错
img_type = imghdr.what(img_file)
# 输出图片的base64
img_base64 = base64.b64encode(img_file.read())
print(img_type)
print(img_base64)