我在一个系统上工作,它接收bytes
个对象,其中包含一个格式的图像,这是事先不知道的,应该与它一起工作.我正在用PIL
为我做这项工作.The documentation表示PIL.Image.open()
可以识别文件,即使在传递文件对象时也是如此.我试过了,它与其他文件格式兼容.但SVG
个文件似乎存在问题.
有没有办法让PIL
本身工作,或者我必须通过一些其他方法识别图像,然后强制PIL.Image.open()
将图像读取为SVG
?
MRE:
#!/usr/bin/env python3
import requests
import io
import PIL.Image
with requests.get("https://github.githubassets.com/favicons/favicon.svg") as r:
data = r.content
with io.BytesIO(data) as image:
PIL.Image.open(image)
Traceback (most recent call last):
File "MRE.py", line 11, in <module>
PIL.Image.open(image)
File "/usr/lib/python3.11/site-packages/PIL/Image.py", line 3283, in open
raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7fe38b2457b0>