无法在pyhton中使用base64编码的数据 [英] Not able to use base64 encoded data in pyhton
问题描述
所以我有这段代码,该代码使用base_64编码的数据(图像数据),将其转换为图像并在其上运行google vision(OCR).但是base_64编码的数据存在一些问题.即使认为它是正确的,它也会引发以下错误.
So i have this code which uses a base_64 encoded data(image data) , converts it into an image and runs google vision (OCR) on it . But there is some issue with the base_64 encoded data . Even thought it is correct it throws the following error .
无效的base64编码的字符串:数据字符(167737)的数量不能超过4的倍数1
Invalid base64 encoded string: number of data characters (167737) cannot be 1 more than a multiple of 4
因为该字符串是2000行,所以我无法在此处输入它, b'/9j/4af ..... TE2f//Z'
就是这样开始和结束并包含167737个字符.所以我现在该怎么办,因为即时通讯无法将其转换回图像文件.只是为了测试代码,我使用的是我转换为base_64的图像,并使用该字符串将其传递给其他代码,该代码应该在转换后的图像上运行ocr后为我提供文本输出.
Since that string is 2000 lines i cannot input it here , b'/9j/4af..........TE2f//Z'
this is how it starts and ends and has 167737 characters in it.
So what do i do now as im not able to convert it back to image file . And just to test the code i am using an image which i converted to base_64 and using that string to pass it to a different code which is supposed to give me the text output after running ocr on the converted image.
推荐答案
添加缺少的填充.
padding = len(data) % 4
if padding > 0:
data += b'='* (4 - padding)
base64.b64decode(data)
这篇关于无法在pyhton中使用base64编码的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!