解码base 64编码图形时无法理解错误

时间:2014-11-23 12:47:09

标签: python python-3.x

我导入了一个文件background_image.txt,其中包含字符串引用background_image基本64位编码图形。当我编译程序时,我在错误日志中得到错误:

Traceback (most recent call last):
  File "main.py", line 23, in <module>
    background_image = io.StringIO(base64.decode(background_image.background_image))
TypeError: decode() missing 1 required positional argument: 'output'

这是第23行

background_image = io.StringIO(base64.decode(background_image.background_image)) # line 23

这是我程序的缩减版。该程序使用tkinter创建一个窗口,使用base 64编码图形作为画布背景。

# modules
from tkinter import *
from PIL import ImageTk, Image
import io
import base64

# imported files
import background_image 

# variables 
background_image = io.StringIO(base64.decode(background_image.background_image)) # line 23

# window creation
root = Tk()

# canvas creation
canvas = Canvas(root, width=600, height=600, bd=-2) 
canvas.pack()

# canvas attributes 
background = ImageTk.PhotoImage(file=background_image)
canvas.create_image(0, 0, image=background, anchor=NW)
text = canvas.create_text(125, 75, anchor=CENTER)

# end
root.after(0, display)
root.mainloop()

1 个答案:

答案 0 :(得分:3)

根据the documentation,base64.decode有两个参数:输入和输出。

您当然希望使用返回已解码字符串的base64.b64decode(s)