我想使用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
我做错了什么?
答案 0 :(得分:241)
我不确定我理解你的问题。我假设你正在做一些事情:
import base64
with open("yourfile.ext", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
当然,您必须首先打开文件并阅读其内容 - 您不能简单地将路径传递给编码功能。
修改强> 好的,这是您编辑原始问题后的更新。
首先,在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()
答案 1 :(得分:52)
使用python 2.x,您可以使用.encode进行简单编码:
with open("path/to/file.png", "rb") as f:
data = f.read()
print data.encode("base64")
答案 2 :(得分:9)
正如我在上一个问题中所说,没有必要对字符串进行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))
...
现在,图像作为名为image_data
的变量存储在名为image.py
的文件中
启动一个新的解释器并导入image_data
>>> from image import image_data
>>>
答案 3 :(得分:5)
借用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')
然后,您可以决定使用Cython编译输出图像文件以使其凉爽。使用此方法,您可以将所有图形捆绑到一个模块中。
答案 4 :(得分:2)
.data("ui-autocomplete")._renderItem = function (ul, item) {
const htmlstring = `<li class="ui-menu-item" role="presentation"><a class="ui-corner-all" tabindex="-1"></a></li>`;
const $li = $(htmlstring);
const id = $(ul).find('li').length + 1;
$li.find('a').attr("data-id", item.value).attr("id", id).html(item.label);
return $li.appendTo(ul);
};
答案 5 :(得分:2)
它对我有用
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)
答案 6 :(得分:0)
第一个答案将打印带有前缀b'的字符串。 这意味着您的字符串将类似于b'your_string'。要解决此问题,请添加以下代码行。
encoded_string= base64.b64encode(img_file.read())
print(encoded_string.decode('utf-8'))