我想识别图像的类型以判断它是否是webp
格式,但我不能只使用file
命令,因为图像作为从互联网下载的二进制文件存储在内存中。PIL
到目前为止,我在lib 或imghdr
lib中找不到任何方法来执行此操作
这是我不想做的:
from PIL import Image
import imghdr
image_type = imghdr.what("test.webp")
if not image_type:
print "err"
else:
print image_type
# if the image is **webp** then I will convert it to
# "jpeg", else I won't bother to do the converting job
# because rerendering a image with JPG will cause information loss.
im = Image.open("test.webp").convert("RGB")
im.save("test.jpg","jpeg")
当这"test.webp"
实际上是一个webp
图像时,var image_type
它是否None
表明imghdr
lib 不知道webp
类型,那么有什么方法可以webp
用 python 确定它是一个图像吗?
作为记录,我使用的是 python 2.7