美文网首页
彻底解决Tkinter库读取jpg/png等文件的bug

彻底解决Tkinter库读取jpg/png等文件的bug

作者: 超哥__ | 来源:发表于2019-10-20 12:58 被阅读0次

众所周知,Tkinter库只能解析gif文件,下面来扩展他,使之能处理jpg/png文件

def PhotoImage(file=None, data=None):
    import io, PIL, base64
    from PIL import ImageTk as tk_
    if file is not None:
        return tk_.PhotoImage(PIL.Image.open(file))
    elif data is not None:
        if type(data) == bytes:
            data = base64.b64decode(data)
        return tk_.PhotoImage(PIL.Image.open(io.BytesIO(data)))
    return None

相关文章

网友评论

      本文标题:彻底解决Tkinter库读取jpg/png等文件的bug

      本文链接:https://www.haomeiwen.com/subject/swoomctx.html