无法base64.decode的`的输出中指定()`到可变
我想导入的基64编码的图形作为画布背景来使用。根据documentation,base64.decode需要2个参数:输入和输出。我不明白如何将base64.decode()
的输出分配给变量background_image
。这些是有问题的两行代码:无法base64.decode的`的输出中指定()`到可变
background_image = base64.decode(background_image.background_image, x.png)
canvas.create_image(0, 0, image=ImageTk.PhotoImage(file=background_image), anchor=NW)
这是我简化程序的版本。
#imported modules
from tkinter import *
from PIL import ImageTk, Image
import base64
#imported files
import background_image # a .py file containing the base64 encoded graphic string
'''...stuff'''
background_image = base64.decode(background_image.background_image, x.png)
canvas.create_image(0, 0, image=ImageTk.PhotoImage(file=background_image), anchor=NW)
'''...more stuff'''
阅读the documentation更仔细:
base64.decode(input, output)
Decode the contents of the binary
input
file and write the resulting binary data to theoutput
file.input
andoutput
must be file objects.input
will be read untilinput.read()
returns an emptybytes
object.
如果有的话,你应该使用base64.b64decode()
。
background_image = base64.b64decode(background_image.background_image)
非常感谢。如果我想创建一个空文件'file'并且写入'fh.write(base64.b64decode(background_image.background_image))',那么这就完美了,但是我想这个'background_image = base64.b64decode(background_image.background_image)''然后将这个'background = ImageTk.PhotoImage(file = background_image)'放到画布背景中。 – 2014-11-23 15:26:47
你在哪儿从解码您的信息有两个参数,其中一个是输出?这不是真的。它*返回*它的输出。我认为你的问题是试图从一个字符串实例化一个PhotoImage,而不是一个PIL图像。您需要通过包装background_image内容的StringIO对象创建一个PIL.Image,并使用它来实例化PhotoImage。 – deets 2014-11-23 14:21:24
Ups。我刚刚看到你正在使用与我推测不同的功能。蒂姆已经给出了这个部分的正确答案。我的评论的其余部分也不完全正确 - 我看到了错误的文档。然而,你仍然无法按照你想要的方式去做,因为PhotoImage要么像文件一样的对象,要么是base64编码的数据作为字符串。所以,你可以跳过这个解码,并使用光象(数据= background_image.background_image) – deets 2014-11-23 14:32:53
你是说像这样'background_image = ImageTk.PhotoImage(数据= background_image.background_image)'像这样'canvas.create_image(0,0,图像= background_image,anchor = NW)'? – 2014-11-23 14:47:01