我想使用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))
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
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
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Jim*_*som 277
我不确定我理解你的问题.我假设你正在做的事情:
import base64
with open("yourfile.ext", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
Run Code Online (Sandbox Code Playgroud)
您必须首先打开文件,并阅读其内容 - 您不能简单地将路径传递给编码功能.
编辑: 好的,这是您编辑原始问题后的更新.
首先,在Windows上使用路径分隔符时,请记住使用原始字符串(字符串前缀为'r'),以防止意外命中转义字符.其次,PIL的Image.open要么接受文件名,要么接受类似文件(即对象必须提供read,seek和tell方法).
话虽这么说,你可以使用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()
Run Code Online (Sandbox Code Playgroud)
Ivo*_*ijk 53
使用python 2.x,您可以使用.encode进行简单的编码:
with open("path/to/file.png", "rb") as f:
data = f.read()
print data.encode("base64")
Run Code Online (Sandbox Code Playgroud)
小智 25
import base64
from PIL import Image
from io import BytesIO
with open("image.jpg", "rb") as image_file:
data = base64.b64encode(image_file.read())
im = Image.open(BytesIO(base64.b64decode(data)))
im.save('image1.png', 'PNG')
Run Code Online (Sandbox Code Playgroud)
Joh*_*ooy 10
正如我在上一个问题中所说,没有必要对字符串进行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))
...
Run Code Online (Sandbox Code Playgroud)
现在,图像存储为image_data名为image.py
Start a fresh interpreter 的文件中调用的变量,并导入image_data
>>> from image import image_data
>>>
Run Code Online (Sandbox Code Playgroud)
借用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')
Run Code Online (Sandbox Code Playgroud)
然后,您可以决定使用Cython编译输出图像文件以使其更酷.使用此方法,您可以将所有图形捆绑到一个模块中.
小智 7
第一个答案将打印带有前缀b'的字符串。这意味着您的字符串将类似于b'your_string'。要解决此问题,请添加以下代码行。
encoded_string= base64.b64encode(img_file.read())
print(encoded_string.decode('utf-8'))
Run Code Online (Sandbox Code Playgroud)
它对我有用
import base64
import requests
# Getting image in bytes
response = requests.get("image_url")
# image encoding
encoded_image = base64.b64encode(response.content)
# image decoding and without it's won't work due to some '\xff' error
decoded_image= base64.b64decode(encoded_image)
Run Code Online (Sandbox Code Playgroud)