Python uint8array to base64 encode不工作

2024-03-28 22:30:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小函数,在其中我接收图像并总是在回调中运行。我需要将它从ndarray转换为base64编码的字符串,以便能够进一步传输它。然而,我收到一个错误的说法:ndarray不是C传染的。我在其他几张图片上尝试了同样的代码,效果很好。我不知道我做错了什么:

def process_image(image):
    i = np.array(image.raw_data)
    i2 = i.reshape((480,640,4))
    i3 = i2[:,:,:3]

    print('shape : ', i3.shape)
    print('size : ', i3.size)
    print('dtype : ', i3.dtype)

    encode = base64.b64encode(i3)

我得到的结果是:

shape : (480, 640, 3)
size :  921600
dtype :  uint8
Traceback (most recent call last):
  File "D:\Tools\CARLA\CARLA_0.9.5\PythonAPI\examples\ROS_CARLA.py", line 65, in process_image
    encode = base64.b64encode(i3)
  File "C:\Users\u17m36\AppData\Local\Programs\Python\Python37\lib\base64.py", line 58, in b64encode
    encoded = binascii.b2a_base64(s, newline=False)
ValueError: ndarray is not C-contiguous

它不是C-连续数组是什么意思?我怎样才能解决这个问题?你知道吗


Tags: pyimagesizeprocessencodefileprintndarray