简体   繁体   English

如何使用 python 对图像进行 base64 编码

[英]How to base64 encode an image using python

I have a stream of data that comes from a device and I need to convert it into a jpeg and then base64 encode it to transfer it over the.network.我有来自设备的 stream 数据,我需要将其转换为 jpeg,然后 base64 对其进行编码以通过 .network 传输。

My Python 2.7 code so far looks like this:到目前为止,我的 Python 2.7 代码如下所示:

from PIL import Image
import io

image = Image.open(io.BytesIO(self.imagebuffer)) # Image buffer contains the image data from the device.
image.save("test.jpg") # Writes the image to the disk in jpeg format
image.show() # Opens the image using the OS image view

I can see I have the image I want and can save it to the disk in jpeg format.我可以看到我有我想要的图像,可以将它以 jpeg 格式保存到磁盘。

What I don't understand is if I can base64 encode the image from the image object or if I need to write it to the disk first.我不明白的是我是否可以 base64 对image object 中的图像进行编码,或者我是否需要先将其写入磁盘。 I would like to avoid writing it if possible.如果可能的话,我想避免写它。

The 2nd question I have is what is PIL doing in this process?我的第二个问题是PIL在这个过程中做了什么? Is it taking the data and putting the required special codes into the file to make it a jpeg file?是获取数据并将所需的特殊代码放入文件中以使其成为 jpeg 文件吗? I think the answer tot his is yes as I can change the file extension to .bmp and the correct file is written on the disk.我认为他的答案是肯定的,因为我可以将文件扩展名更改为.bmp ,并将正确的文件写入磁盘。

So in summary, is it possible to get a base64 encoded version of my jpeg file from the image object without writing it to disk first?因此,总而言之,是否可以从image object 中获取我的 jpeg 文件的 base64 编码版本,而无需先将其写入磁盘?

Try this code 试试这个代码

Image base64 encoded format 图像base64编码格式

Python code: Python代码:

import os
import base64

image = 'test.jpg'

encoded_string = ""
with open(image, "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())
    file = encoded_string
  1. Save the img in a buffer - doesn't touch disk 将img保存在缓冲区中-不接触磁盘
  2. regex for jpeg headers and footers - fault tolerance ( Header: FF D8 FF, Footer: FF D9) jpeg页眉和页脚的正则表达式-容错(页眉:FF D8 FF,页脚:FF D9)
  3. base64 the data base64数据
  4. flush to file 刷新到文件

This code does the job: 这段代码完成了这项工作:

from PIL import Image
import io
import base64
import cStringIO

image = Image.open(io.BytesIO(imagebuffer))
encodingbuffer = cStringIO.StringIO()
image.save(encodingbuffer, format="JPEG")
encodedimage = base64.b64encode(encodingbuffer.getvalue())

I`m working with .dwg file and Python 2.7, this works for me:我正在使用 .dwg 文件和 Python 2.7,这对我有用:

import os
import base64

# Open the file
infile = open(input_file, 'r')
# 'r' says we are opening the file to read, infile is the opened file object that we will read from
# encode file to base64
base64EncodedStr = base64.b64encode(infile.read())

Please try this to base64 encode an image using python请尝试使用 base64 对图像进行编码 python

import base64

with open("test.jpg", "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())
    print(encoded_string)

OR或者

import base64

image = open('test.jpeg', 'rb')
image_read = image.read()
image_encode = base64.b64encode(image_read)
print(image_encode)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM