代码改变世界

numpy:DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly

2021-09-28 22:36  dreamboy2000  阅读(979)  评论(0编辑  收藏  举报

报错信息如下:

DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
  nparr = np.fromstring(imgString,np.uint8)

在做图像base64与numpy array(cv2传统格式)之间的转换测试的时候出现了以上报错,将原np.fromstring函数更新为np.frombuffer即可,如下:

import base64
import numpy as np
import cv2


with open("/home/images/friend.jpg","rb") as f:#转为二进制格式
    base64_data = base64.b64encode(f.read())#使用base64进行加密
    
    #base64转换为numpy array格式:
    imgData = base64.b64decode(base64_data)
    # nparr = np.fromstring(imgData, np.uint8) #报错
    nparr = np.frombuffer(imgData,np.uint8) 
    image = cv2.imdecode(nparr,cv2.IMREAD_COLOR)
       
       cv2.imwrite("/home/images/friend_copy.jpg",image)