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

众所周知,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

推荐阅读更多精彩内容