我想導入的基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'''
你在哪兒從解碼您的信息有兩個參數,其中一個是輸出?這不是真的。它*返回*它的輸出。我認爲你的問題是試圖從一個字符串實例化一個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