AttributeError'模块'对象没有属性'decodebytes'

时间:2019-05-15 12:49:53

标签: python python-2.7 base64

我正在使用OpenCV对图像进行一些处理。现在,我需要将base64字符串转换为字节。在Python-3.6中,我可以使用base64.decodebytes,但是在Python-2.7中找不到替代模块。 Python-2.7中还有其他替代方法吗?

cvimg = cv2.imdecode(np_img_array, cv2.IMREAD_COLOR)
tmp_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = base64.b64encode(cv2.imencode('.jpg', tmp_img)[1])
img = base64.decodebytes(img)

注意:我正在使用Python-2.7,因为我正在使用的模块之一尚未转换为Python-3.6

1 个答案:

答案 0 :(得分:0)

Python 2仍然内置了base64模块。 使用

base64.standard_b64encode(s)
#and
base64.standard_b64decode(s)
#Where 's' is an encoded string

应该仍然可以工作。

相关问题