我想使用base64模塊將圖像編碼為字符串。不過我遇到了一個(gè)問題。如何指定要編碼的圖像?我嘗試對(duì)圖像使用目錄,但這只會(huì)導(dǎo)致目錄被編碼。我希望對(duì)實(shí)際的圖像文件進(jìn)行編碼。編輯我嘗試了以下代碼段:with open("C:\Python26\seriph1.BMP", "rb") as f: data12 = f.read() UU = data12.encode("base64") UUU = base64.b64decode(UU) print UUU self.image = ImageTk.PhotoImage(Image.open(UUU))但我收到以下錯(cuò)誤:Traceback (most recent call last): File "<string>", line 245, in run_nodebug File "C:\Python26\GUI1.2.9.py", line 473, in <module> app = simpleapp_tk(None) File "C:\Python26\GUI1.2.9.py", line 14, in __init__ self.initialize() File "C:\Python26\GUI1.2.9.py", line 431, in initialize self.image = ImageTk.PhotoImage(Image.open(UUU)) File "C:\Python26\lib\site-packages\PIL\Image.py", line 1952, in open fp = __builtin__.open(fp, "rb")TypeError: file() argument 1 must be encoded string without NULL bytes, not str我究竟做錯(cuò)了什么?
3 回答

慕標(biāo)5832272
TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用python 2.x,您可以使用.encode進(jìn)行簡單編碼:
with open("path/to/file.png", "rb") as f:
data = f.read()
print data.encode("base64")
添加回答
舉報(bào)
0/150
提交
取消