我想使用base64模块将图像编码为字符串。不过我遇到了一个问题。如何指定要编码的图像?我尝试对图像使用目录,但这只会导致目录被编码。我希望对实际的图像文件进行编码。编辑我尝试了以下代码段: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))但我收到以下错误: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我究竟做错了什么?
3 回答
慕标5832272
TA贡献1966条经验 获得超4个赞
使用python 2.x,您可以使用.encode进行简单编码:
with open("path/to/file.png", "rb") as f:
data = f.read()
print data.encode("base64")
添加回答
举报
0/150
提交
取消