首页 文章

如何将字符串转换为图像以生成单个可执行文件

提问于
浏览
0

我创建了一个简单的程序,用tkinter创建一个窗口 . 我将画布的背景图像转换为字符串,以便我可以使用主程序编译它以创建单个可执行文件 .

我使用以下代码进行从图像到文本的转换:

import base64

with open("background.png", "rb") as imageFile:
    str = base64.b64encode(imageFile.read())
text_file = open("background.txt", "wb")
text_file.write(str)
text_file.close()

现在的问题是我无法弄清楚如何将文本文件转换回图像 . 这是我的简单窗口程序的简化版本 . 我正在使用Python 3.4并导入 io 并尝试使用 io.StringIO 无济于事 .

from Tkinter import *
from PIL import ImageTk,Image
import io 

...other stuff

backgroundImage=root.PhotoImage(io.StringIO('background.txt')) # This line is my problem
backgroundLabel=root.Label(parent,image=backgroundImage)

...more stuff

1 回答

  • 1

    那么,您希望将实际的base64编码图形作为 .py 文件的一部分吗?然后你需要手动将 background.txt 的内容复制到你的 .py 文件中......就像这样:

    background_image = """\
    019248a8b2f129d    # obviously not real data  ;)
    c12e0284a8172f0
    """.strip()
    

    然后有类似的东西:

    # untested
    backgroundImage = root.PhotoImage(io.StringIO(base64.decode(background_image)))
    ...
    

相关问题