简体   繁体   English

将base64的字符串转换回base64字节

[英]Convert string of base64 back to base64 bytes

I've uploaded an image using OpenCV and then encode it with base64 encoding using base64 's b64encode . 我已经使用OpenCV上传了图片,然后使用base64b64encode使用base64编码对其进行了编码。

>>> import cv2
>>> import base64
>>> image = cv2.cvtColor(cv2.imread("some_image.jpg"), cv2.COLOR_BGR2RGB)
>>> image_64 = base64.b64encode(image)
>>> image_64
b'//////////////////...
>>> type(image_64)
<class 'bytes'>

Then I convert it into a string using the str() method. 然后,我使用str()方法将其转换为字符串。 This creates a string of the encoded image. 这将创建一个字符串的编码图像。

>>> image_64str = str(image_64)
>>> image_64str
b'//////////////////...
>>> type(image_64str)
<class 'str'>

Both of them (the <class 'bytes'> type and the <class 'str'> ) looks similar. 两者( <class 'bytes'>类型和<class 'str'> )看起来都相似。 I attempted to decode them using base64 's b64decode and the decode() function. 我试图使用base64b64decodeb64decode decode()函数decode() However, an error occurred when I decoded the image_64str . 但是,当我解码image_64str时发生了错误。

>>> image_64str.decode()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'decode'
>>> base64.b64decode(image_64str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/base64.py", line 87, in b64decode
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

I fully understood what the errors were trying to tell me. 我完全理解错误是想告诉我什么。 But my question is, how can i convert the string of the encoded image ( image_64str ) back to bytes? 但是我的问题是, 如何将编码图像的字符串( image_64str )转换回字节?

I've tried to use base64 's 'b64encode` again on the string. 我尝试再次在字符串上使用base64的'b64encode`。 However, it returns an error. 但是,它返回一个错误。

>>> str_to_b64 = base64.b64encode(image_64str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/base64.py", line 58, in b64encode
    encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'

Please do tell if anybody noticed what i was missing. 请告诉是否有人注意到我失踪了。 I am using Python 3.6. 我正在使用Python 3.6。 Thanks in advance. 提前致谢。

EDIT: Adding more description to my question. 编辑:为我的问题添加更多描述。

I was able to enable AWS API Gateway binary support. 我能够启用AWS API Gateway二进制支持。 My purpose is to pass an image as binary data through a POST request to the API and convert that to a PIL object so that i can process it in the backend using AWS Lambda. 我的目的是通过POST请求将图像作为二进制数据传递给API,并将其转换为PIL对象,以便我可以使用AWS Lambda在后端对其进行处理。 With API Gateway, the binary data was encoded with base64 binary. 使用API​​ Gateway,二进制数据使用base64二进制编码。

I opened the image as binary data using python's open function (there were two images that I wanted to pass through the API). 我使用python的open函数将图像作为二进制数据open (我想通过API传递两个图像)。 Then i use i use a dictionary to hold both the two images binary data, like 然后我用字典来保存两个图像的二进制数据,例如

data = {"data1": img_binary_data_1, "data2": img_binary_data_2}

I send the POST request using python request library. 我使用python request库发送POST请求。 One of the argument that i can pass in the post function is data , so i passed the image data using that. 我可以在post函数中传递的参数之一是data ,因此我使用它传递了图像数据。

I was able to send the request. 我能够发送请求。 In the Lambda backend, I wanted to convert the binary data to a PIL object for further processing. 在Lambda后端中,我想将二进制数据转换为PIL对象以进行进一步处理。 However, it seems that the data was packed into JSON format and the base64 encoded binary image had been turned in to a python string. 但是,似乎数据已打包为JSON格式,并且base64编码的二进制映像已上载为python字符串。 I confirmed this by printing the data in the log of AWS CloudWatch. 我通过在AWS CloudWatch日志中打印数据来确认这一点。

I tried to use .decode() , but base here you cannot decode a string. 我尝试使用.decode() ,但是根据这里您不能解码字符串。

I was able to decode the string using b64decode() , returning a byte object. 我能够使用b64decode()解码字符串,返回一个字节对象。 However when try to convert it to a PIL Object like 但是,当尝试将其转换为PIL对象时

img = imread(io.BytesIO(base64.b64decode(b64_string)))

I received an error saying 我收到一条错误消息,说

OSError: cannot identify image file <_io.BytesIO object at 0x1101dadb0>

I tried some of the solution from this link , but apparently you cannot do this with byte-object. 我从此链接尝试了一些解决方案,但是显然您不能使用字节对象来做到这一点。

I have tried ti use PIL.frombuffer and PIL.frombytes . 我试过使用PIL.frombufferPIL.frombytes However, they returned the not enough data value when i am very sure about the sizer of the image (in this case (256, 256) ). 但是,当我非常确定图像的sizer(在这种情况下(256, 256) )时,它们返回的not enough datanot enough data

So my question is, how can i convert the base64 image in to a PIL object? 所以我的问题是, 如何将base64图像转换为PIL对象? I hope this helps to understand my question better. 我希望这有助于更好地理解我的问题。 Thanks in advance. 提前致谢。

Base64 is a binary -> char encoding so encoding an image makes sense, you get text bytes where a group of 6 bits is considered a character. Base64是二进制-> char编码,因此对图像进行编码很有意义,您将获得文本字节,其中6位一组被视为字符。

Now even if the above bytes are of characters, they are not python strings as python strings are utf-8. 现在,即使上面的字节是字符,它们也不是python字符串,因为python字符串是utf-8。

When you convert the bytes to string it converts them to utf-8 and messes up the base64 padding (only = is allowed for padding) and what you get is a python string. 当您将字节转换为字符串时,它将字节转换为utf-8并弄乱了base64填充(仅=允许填充),您得到的是一个python字符串。

Now get the error when you decode it as it is not base64 encoding anymore. 现在,在解码时会收到错误,因为它不再是base64编码。 You can also not encode the string as base64 is bytes -> char and a string is not bytes. 您还不能对字符串进行编码,因为base64是字节-> char并且字符串不是字节。

Why are you converting the encoded bytes to string anyway? 为什么无论如何要将编码的字节转换为字符串? A little more description of your usecase would help. 对用例进行更多描述会有所帮助。

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

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