首页 > 解决方案 > 如何在python中打开svg url

问题描述

我正在尝试编写一个从 wikipedia 抓取 svg url 的程序,我一直在做一些研究,但找不到打开指向 svg 文件的 url 的方法,所以我想知道是否有善良的灵魂可以将我指向正确的方向。

这是到目前为止的程序:

import requests
import os
from io import BytesIO
import matplotlib.pyplot as plt
from PIL import Image
import wikipedia

a = input("Enter drug: ")
piccies = wikipedia.page(a).images
matching = [s for s in piccies if "svg" in s]
print(matching[0])

response = requests.get(matching[0], stream=True)
print(response)
img = Image.open(response.raw)
plt.imshow(img)
plt.show()

以及我得到的当前错误:

 Traceback (most recent call last):
  File "scrape3.py", line 16, in <module>
    img = Image.open(response.raw)
  File "/home/alejandro/anaconda3/lib/python3.8/site-packages/PIL/Image.py", line 2943, in open
    raise UnidentifiedImageError(
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f06a67af310>

我在这里缺少什么?是因为我需要将 url 解码为 jpg 或 png 才能打开它吗?PS:我得到 svg id 以便稍后在 TKinter 代码上使用它的原因,所以如果有办法导出到 TKinter 标签或画布对象,我将不胜感激。谢谢

标签: pythonsvgpython-imaging-library

解决方案


推荐阅读