【问题标题】:How can I make a numpy ndarray from network binary image bytes?如何从网络二进制图像字节制作一个 numpy ndarray?
【发布时间】:2021-07-28 21:22:01
【问题描述】:

后台:

我想使用 grpc 协议将二进制字节图像从其他服务传输到 python 服务。并且python服务将处理opencv lib中的图像。我从其他服务下载图像并将图像数据从内存发送到 python 服务。

问题是如何在python中高效地将二进制字节图像转换为opencv。?

在 python 服务之前。
它从本地路径读取图像。 img 的类型是 numpy.ndarray

  img = cv2.imread(img_path)
  img = cv2.resize(img, (224,224))

希望如下,以及如何简单高效地实现convert功能!

   img = convert(image)  #image is the binary bytes image from grpc protocol
   img = cv2.resize(img, (224,224))

这里有一些类似的问题。但它从本地读取数据并转换为opencv。

参考:

注意:我是 python 新手

python的版本是python3

【问题讨论】:

    标签: python-3.x image numpy opencv multidimensional-array


    【解决方案1】:

    由于大多数服务接收带有字节字符串的图像,因此有一种方法可以有效地将其转换为 numpy 格式

    import numpy as np
    import cv2
    
    def convert(byte_str):
      image_np = np.from_buffer(byte_str, dtype=np.uint8) # Get a 1d-array of an image
      return cv2.imdecode(image_np, cv2. IMREAD_COLOR) # Gets back a numpy array
    
    img = convert(image)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 2022-10-14
      • 2012-10-03
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多