图像(numpy)与Base64互转
写在前面
-
本文内容
图像(numpy)与Base64互转 -
平台/环境
python opencv numpy -
转载请注明出处:
https://blog.csdn.net/qq_41102371/article/details/146369894
目录
- 写在前面
- 代码
- 完
代码
img_convert.py
import cv2
import base64
import numpy as np
def img2base64(img):
_, buffer = cv2.imencode('.png', img)
image_data = buffer.tobytes()
base64_data = base64.b64encode(image_data).decode("utf-8")
return base64_data
def base642img(base64_data):
image_data = base64.b64decode(base64_data)
image_array = np.frombuffer(image_data, dtype=np.uint8)
img = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
return img
if __name__=="__main__":
# read img
img = cv2.imread("./test.png")
# covnert img to Base64
base64_data = img2base64(img)
print(base64_data)
# convert Base64 to img
img = base642img(base64_data)
cv2.imwrite("./test.png", img)
# show img
cv2.imshow("Restored Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
test.png
完
主要做激光/影像三维重建,配准、分割等常用点云算法,熟悉open3d、pcl等开源点云库,技术交流、咨询可私信