使用 base64 对图像文件进行编码
- 2025-02-18 09:23:00
- admin 原创
- 106
问题描述:
我想使用 base64 模块将图像编码为字符串。但我遇到了一个问题。如何指定要编码的图像?我尝试使用目录来编码图像,但这只会导致目录被编码。我希望对实际的图像文件进行编码。
编辑
我尝试了这个片段:
with open("C:Python26seriph1.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:Python26GUI1.2.9.py", line 473, in <module>
app = simpleapp_tk(None)
File "C:Python26GUI1.2.9.py", line 14, in __init__
self.initialize()
File "C:Python26GUI1.2.9.py", line 431, in initialize
self.image = ImageTk.PhotoImage(Image.open(UUU))
File "C:Python26libsite-packagesPILImage.py", line 1952, in open
fp = __builtin__.open(fp, "rb")
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
我做错什么了?
解决方案 1:
我不确定我是否理解了你的问题。我假设你正在做类似的事情:
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()
解决方案 2:
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')
解决方案 3:
只需使用base64.b64encode(img_file.read())
就会返回字节。这意味着您的结果将看起来像b'your_string'
。要解决此问题,您需要像这样解码它:
encoded_string = base64.b64encode(img_file.read())
print(encoded_string.decode('utf-8'))
我在将图像转换为 Base64 字符串时遇到过这种情况。您也可以看看我如何从那里删除它。链接在这里将图像转换为 base64 字符串并修复前缀中的 'b
解决方案 4:
借鉴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编译输出图像文件以使其更酷。使用此方法,您可以将所有图形捆绑到一个模块中。
解决方案 5:
正如我之前在你问题中所说,没有必要对字符串进行 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
>>>
解决方案 6:
这对我来说是工作
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)
扫码咨询,免费领取项目管理大礼包!