我想检查(image_file)变量格式,并验证它是".png“还是".jpg”。
image_str = urlopen(url).read()
image_file = io.BytesIO(image_str)url是来自reddit的随机文件。
发布于 2021-12-27 15:06:03
最好的库可能是imghdr,它检查图像并识别它们的文件类型:
import imghdr
imghdr.what("/path/to/file")
# OR...
imghdr.what("", h=io.BytesIO(imge_str))发布于 2021-12-27 15:11:58
您可以使用endswith
在您的情况下,如果url是一个包含文件的字符串,则可以使用:
if url.endswith('.png'):
...
elif url.endswith('.jpg'):
...https://stackoverflow.com/questions/70497122
复制相似问题