Kivy 无法从外部加载图像 URL

Kivy Cannot Load Image from external URL

我正在开发一个 Kivy 项目,该项目需要加载在外部托管的图像 URL 我试过 image=Image(source=https://www.gogoanime1.com/ac/meta/anime/8349/ghost-in-the-shell-movies.jpg)

并将其作为 layout.add_widgets(image) 添加到我的布局中 此处错误不明确,日志抛出此 *[ERROR] [Image] Error reading file https://www.gogoanime1.com/ac/meta/anime/8349/ghost-in-the-shell-movies.jpg *

我也试过 image=AsyncImage(https://www.gogoanime1.com/ac/meta/anime/8349/ghost-in-the-shell-movies.jpg) 但它仍然失败,可能是由于用户代理的要求,错误日志是

[INFO   ] [Logger      ] Record log in C:\Users\DELL\.kivy\logs\kivy_19-11-29_23.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.gstreamer" 0.1.17
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.1.9
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.1.12
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.1.22
[INFO   ] [Kivy        ] v1.11.1
[INFO   ] [Kivy        ] Installed at "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "C:\Users\DELL\AppData\Local\Programs\Python\Python36\python.exe"
[INFO   ] [Factory     ] 184 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.5.0 NVIDIA 353.62'>
[INFO   ] [GL          ] OpenGL vendor <b'NVIDIA Corporation'>
[INFO   ] [GL          ] OpenGL renderer <b'GeForce GT 525M/PCIe/SSE2'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 5
[INFO   ] [GL          ] Shading version <b'4.50 NVIDIA'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Start application main loop
[INFO   ] [Loader      ] using a thread pool of 2 workers
[ERROR  ] [Loader      ] Failed to load image <https://www.gogoanime1.com/ac/meta/anime/8349/ghost-in-the-shell-movies.jpg>
Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\loader.py", line 342, in _load_urllib
    fd = opener.open(request)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
[ERROR  ] [Loader      ] Failed to load image <https://www.gogoanime1.com/ac/meta/anime/11100/ssssgridman.jpg>
Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\loader.py", line 342, in _load_urllib
    fd = opener.open(request)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
[INFO   ] [WindowSDL   ] exiting mainloop and closing.
[INFO   ] [Base        ] Leaving application in progress...

我该如何解决这个问题,也许使用请求库加载图像。

pypi提供的kivy版本有一个bug已经解决了下一个版本,所以在此之前你有以下选择:

  • 等待kivy的下一个版本,
  • 从 github 安装:pip install git+https://github.com/kivy/kivy.git,
  • 或修改 loader.py 文件(在您的情况下它位于 C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\loader.py),方法是更改​​:
# ...
if (
    Config.has_section('network')
    and 'useragent' in Config.items('network')
):
# ...

if Config.has_option('network', 'useragent'):

我推荐最后一种方法,因为它比其他方法更简单、更快。