Kivy 无法从外部 URL 加载图像

Posted

技术标签:

【中文标题】Kivy 无法从外部 URL 加载图像【英文标题】:Kivy Cannot Load Image from external URL 【发布时间】:2020-03-24 17:03:15 【问题描述】:

我正在开发一个 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...

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

【问题讨论】:

【参考方案1】:

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'):

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

【讨论】:

像魅力一样工作,希望在本地编译时不会出现问题:-)

以上是关于Kivy 无法从外部 URL 加载图像的主要内容,如果未能解决你的问题,请参考以下文章

适用于 Android 的 Kivy 应用程序无法加载图像

例外:SDL2:无法加载图像 - 在 android 上运行 kivy apk 时

SDL2 错误:使用 pyinstaller 冻结 kivy 应用程序时“无法加载图像 <default.png>”

无法从 URL 加载图像

Cordova 5.3.1 和 iOS9 平台 - 我无法从外部源加载图像和脚本

FOP 无法从 url 加载图像