gpt4 book ai didi

python,opencv,图像数组到二进制

转载 作者:太空狗 更新时间:2023-10-30 02:54:16 24 4
gpt4 key购买 nike

我有一个大图像,使用 python 中的 cv2 模块和我裁剪图像的一些坐标:

  img = cv.imread(image_path)
crop_img = img[y1:y2,x1:x2]
cv.imwrite(cropPath, crop_img)

现在 crop_img 是一个 numpy.ndarray 类型。然后我将此图像保存到磁盘并使用 open() 函数以二进制格式读取其内容

  with open(cropPath, 'rb') as image_file:
content = image_file.read()

我得到了二进制表示。有什么方法可以在不将图像保存到磁盘的情况下执行上述操作。不保存到磁盘会节省很多时间,我找不到任何方法来做到这一点。如果有人能指出正确的方向,那将会很有帮助。

最佳答案

在此线程上找到答案: Python OpenCV convert image to byte string?

可以使用 cv2 中的 imencode 和 tostring 函数将通过 numpy 数组表示的图像转换为字符串

>>> img_str = cv.imencode('.jpg', img)[1].tostring()
>>> type(img_str)
'str'

关于python,opencv,图像数组到二进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46563364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com