【问题标题】:Converting python2 PIL to python3 code将 python2 PIL 转换为 python3 代码
【发布时间】:2017-01-24 03:43:02
【问题描述】:

我正在尝试从 python2 转换为 python3 的程序使用PIL python 图像库。

我尝试从网络下载缩略图以在 tkinter 风格的 gui 中显示。这是我认为有问题的代码行:

# grab the thumbnail jpg.
req = requests.get(video.thumb)
photo = ImageTk.PhotoImage(Image.open(StringIO(req.content)))

# now this is in PhotoImage format can be displayed by Tk.
plabel = tk.Label(tf, image=photo)
plabel.image = photo
plabel.grid(row=0, column=2)

程序停止并给出一个TypeError,这是backtrce:

Traceback (most recent call last):   File "/home/kreator/.local/bin/url-launcher.py", line 281, in <module>
    main()
  File "/home/kreator/.local/bin/url-launcher.py", line 251, in main
    runYoutubeDownloader()
  File "/home/kreator/.local/bin/url-launcher.py", line 210, in runYoutubeDownloader
    photo = ImageTk.PhotoImage(Image.open(StringIO(req.content)))
    TypeError: initial_value must be str or None, not bytes

这里如何满足python3的要求?

【问题讨论】:

标签: python python-3.5


【解决方案1】:

在这种情况下,您可以看到您导入的库不支持 Python3。这不是您可以在自己的代码中解决的问题。

缺乏 Python3 支持是因为 PIL 已经停产了一段时间。有一个积极维护的分叉,我建议您使用它:https://pillow.readthedocs.io/

您可以从pypi下载。

【讨论】:

    猜你喜欢
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2022-08-05
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多