I want to encode an image into a string using the base64 module. I've ran into a problem though. How do I specify the image I want to be encoded? I tried using the directory to the image, but that simply leads to the directory being encoded. I want the actual image file to be encoded.
我想使用base64模块将图像编码为字符串。我遇到了一个问题。如何指定要编码的图像?我尝试将目录用于图像,但这只会导致编码目录。我想要对实际的图像文件进行编码。
EDIT
编辑
I tired this snippet:
我厌倦了这个片段:
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))
but I get the following error:
但是我收到以下错误:
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
What am I doing wrong?
我究竟做错了什么?
4 个解决方案
#1
193
I'm not sure I understand your question. I assume you are doing something along the lines of:
我不确定我理解你的问题。我假设你正在做的事情:
import base64
with open("yourfile.ext", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
You have to open the file first of course, and read its contents - you cannot simply pass the path to the encode function.
您必须首先打开文件,并阅读其内容 - 您不能简单地将路径传递给编码功能。
Edit: Ok, here is an update after you have edited your original question.
编辑:好的,这是您编辑原始问题后的更新。
First of all, remember to use raw strings (prefix the string with 'r') when using path delimiters on Windows, to prevent accidentally hitting an escape character. Second, PIL's Image.open either accepts a filename, or a file-like (that is, the object has to provide read, seek and tell methods).
首先,在Windows上使用路径分隔符时,请记住使用原始字符串(字符串前缀为'r'),以防止意外命中转义字符。其次,PIL的Image.open要么接受文件名,要么接受类似文件(即对象必须提供read,seek和tell方法)。
That being said, you can use cStringIO to create such an object from a memory buffer:
话虽这么说,你可以使用cStringIO从内存缓冲区创建这样一个对象:
import cStringIO
import PIL.Image
# assume data contains your decoded image
file_like = cStringIO.StringIO(data)
img = PIL.Image.open(file_like)
img.show()
#2
48
With python 2.x, you can trivially encode using .encode:
使用python 2.x,您可以使用.encode进行简单的编码:
with open("path/to/file.png", "rb") as f:
data = f.read()
print data.encode("base64")
#3
8
As I said in your previous question, there is no need to base64 encode the string, it will only make the program slower. Just use the repr
正如我在上一个问题中所说,没有必要对字符串进行base64编码,它只会使程序变慢。只需使用repr
>>> with open("images/image.gif", "rb") as fin:
... image_data=fin.read()
...
>>> with open("image.py","wb") as fout:
... fout.write("image_data="+repr(image_data))
...
Now the image is stored as a variable called image_data
in a file called image.py
Start a fresh interpreter and import the image_data
现在,图像作为名为image_data的变量存储在名为image.py的文件中。启动一个新的解释器并导入image_data
>>> from image import image_data
>>>
#4
5
Borrowing from what Ivo van der Wijk and gnibbler have developed earlier, this is a dynamic solution
借用Ivo van der Wijk和gnibbler之前开发的内容,这是一个动态的解决方案
import cStringIO
import PIL.Image
image_data = None
def imagetopy(image, output_file):
with open(image, 'rb') as fin:
image_data = fin.read()
with open(output_file, 'w') as fout:
fout.write('image_data = '+ repr(image_data))
def pytoimage(pyfile):
pymodule = __import__(pyfile)
img = PIL.Image.open(cStringIO.StringIO(pymodule.image_data))
img.show()
if __name__ == '__main__':
imagetopy('spot.png', 'wishes.py')
pytoimage('wishes')
You can then decide to compile the output image file with Cython to make it cool. With this method, you can bundle all your graphics into one module.
然后,您可以决定使用Cython编译输出图像文件以使其更酷。使用此方法,您可以将所有图形捆绑到一个模块中。
#1
193
I'm not sure I understand your question. I assume you are doing something along the lines of:
我不确定我理解你的问题。我假设你正在做的事情:
import base64
with open("yourfile.ext", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
You have to open the file first of course, and read its contents - you cannot simply pass the path to the encode function.
您必须首先打开文件,并阅读其内容 - 您不能简单地将路径传递给编码功能。
Edit: Ok, here is an update after you have edited your original question.
编辑:好的,这是您编辑原始问题后的更新。
First of all, remember to use raw strings (prefix the string with 'r') when using path delimiters on Windows, to prevent accidentally hitting an escape character. Second, PIL's Image.open either accepts a filename, or a file-like (that is, the object has to provide read, seek and tell methods).
首先,在Windows上使用路径分隔符时,请记住使用原始字符串(字符串前缀为'r'),以防止意外命中转义字符。其次,PIL的Image.open要么接受文件名,要么接受类似文件(即对象必须提供read,seek和tell方法)。
That being said, you can use cStringIO to create such an object from a memory buffer:
话虽这么说,你可以使用cStringIO从内存缓冲区创建这样一个对象:
import cStringIO
import PIL.Image
# assume data contains your decoded image
file_like = cStringIO.StringIO(data)
img = PIL.Image.open(file_like)
img.show()
#2
48
With python 2.x, you can trivially encode using .encode:
使用python 2.x,您可以使用.encode进行简单的编码:
with open("path/to/file.png", "rb") as f:
data = f.read()
print data.encode("base64")
#3
8
As I said in your previous question, there is no need to base64 encode the string, it will only make the program slower. Just use the repr
正如我在上一个问题中所说,没有必要对字符串进行base64编码,它只会使程序变慢。只需使用repr
>>> with open("images/image.gif", "rb") as fin:
... image_data=fin.read()
...
>>> with open("image.py","wb") as fout:
... fout.write("image_data="+repr(image_data))
...
Now the image is stored as a variable called image_data
in a file called image.py
Start a fresh interpreter and import the image_data
现在,图像作为名为image_data的变量存储在名为image.py的文件中。启动一个新的解释器并导入image_data
>>> from image import image_data
>>>
#4
5
Borrowing from what Ivo van der Wijk and gnibbler have developed earlier, this is a dynamic solution
借用Ivo van der Wijk和gnibbler之前开发的内容,这是一个动态的解决方案
import cStringIO
import PIL.Image
image_data = None
def imagetopy(image, output_file):
with open(image, 'rb') as fin:
image_data = fin.read()
with open(output_file, 'w') as fout:
fout.write('image_data = '+ repr(image_data))
def pytoimage(pyfile):
pymodule = __import__(pyfile)
img = PIL.Image.open(cStringIO.StringIO(pymodule.image_data))
img.show()
if __name__ == '__main__':
imagetopy('spot.png', 'wishes.py')
pytoimage('wishes')
You can then decide to compile the output image file with Cython to make it cool. With this method, you can bundle all your graphics into one module.
然后,您可以决定使用Cython编译输出图像文件以使其更酷。使用此方法,您可以将所有图形捆绑到一个模块中。