东方耀AI技术分享

标题: ndarray图片数据与base64字符串之间相互转换 [打印本页]

作者: 东方耀    时间: 2020-5-12 16:24
标题: ndarray图片数据与base64字符串之间相互转换
ndarray图片数据与base64字符串之间相互转换




  1. import cv2
  2. import base64
  3. import numpy as np

  4. img_path = "./testfiles/face.jpg"
  5. img = cv2.imread(img_path)
  6. print(type(img), img.shape)
  7. ori_h, ori_w, c = img.shape

  8. # ndarray图片数据转base64编码
  9. pic_str = base64.b64encode(img)
  10. print(type(pic_str))
  11. # <class 'bytes'>变成字符串str类型
  12. pic_str = pic_str.decode()
  13. print(type(pic_str))

  14. cv2.imshow("ori", img)
  15. cv2.waitKey(0)

  16. # base64解码 转成ndarray类型
  17. img_data = base64.b64decode(pic_str)
  18. nparr = np.fromstring(img_data, np.uint8)
  19. print(type(nparr), nparr.shape, nparr.dtype)
  20. nparr = nparr.reshape((ori_h, ori_w, c))
  21. print(type(nparr), nparr.shape, nparr.dtype)
  22. # img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
  23. cv2.imshow("ori_64", nparr)
  24. cv2.waitKey(0)

复制代码



base64编码  对字符串的  而不是图像数据本身


  1. # -*- coding: utf-8 -*-
  2. __author__ = u'东方耀 微信:dfy_88888'
  3. __date__ = '2020/5/12 下午2:55'
  4. __product__ = 'PyCharm'
  5. __filename__ = 'dfy_demo03'


  6. import cv2
  7. import base64
  8. import numpy as np

  9. img_path = "./testfiles/face.jpg"
  10. img = cv2.imread(img_path)
  11. print(type(img), img.shape)
  12. # 将图片编码成流数据,放到内存缓存中,然后转化成string格式
  13. img_str = cv2.imencode('.png', img)[1].tostring()


  14. # base64编码  对字符串的  而不是图像数据本身
  15. pic_str = base64.b64encode(img_str)
  16. print(type(pic_str))
  17. # <class 'bytes'>变成字符串str类型
  18. pic_str = pic_str.decode()
  19. print(type(pic_str))
  20. print("字符串的长度:", len(pic_str))

  21. cv2.imshow("ori", img)
  22. cv2.waitKey(0)

  23. # base64解码 转成ndarray类型
  24. img_data = base64.b64decode(pic_str)
  25. nparr = np.fromstring(img_data, np.uint8)
  26. print(type(nparr), nparr.shape, nparr.dtype)
  27. img_restore = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
  28. print(type(img_restore), img_restore.shape, img_restore.dtype)
  29. cv2.imshow("img_restore", img_restore)
  30. cv2.waitKey(0)


复制代码


# 用于HTTP传输的时候通常会用
# base64.urlsafe_b64encode代替base64.b64encode,
# base64.urlsafe_b64decode代替base64.b64decode
#
# 两者的区别在于base64.urlsafe_b64encode会把+用-代替,/用_代替,避免URL把+``/当成特殊字符解析了

  1. # -*- coding: utf-8 -*-
  2. __author__ = u'东方耀 微信:dfy_88888'
  3. __date__ = '2020/5/12 下午2:55'
  4. __product__ = 'PyCharm'
  5. __filename__ = 'dfy_demo03'


  6. import cv2
  7. import base64
  8. import numpy as np

  9. img_path = "./testfiles/face.jpg"
  10. img = cv2.imread(img_path)
  11. print(type(img), img.shape)
  12. # 将图片编码成流数据,放到内存缓存中,然后转化成string格式
  13. img_str = cv2.imencode('.png', img)[1].tostring()


  14. # base64编码  对字符串的  而不是图像数据本身
  15. # pic_str = base64.b64encode(img_str)
  16. pic_str = base64.urlsafe_b64encode(img_str)
  17. print(type(pic_str))
  18. # <class 'bytes'>变成字符串str类型
  19. pic_str = pic_str.decode()
  20. print(type(pic_str))
  21. print("字符串的长度:", len(pic_str))

  22. cv2.imshow("ori", img)
  23. cv2.waitKey(0)

  24. # base64解码 转成ndarray类型
  25. # img_data = base64.b64decode(pic_str)
  26. img_data = base64.urlsafe_b64decode(pic_str)
  27. nparr = np.fromstring(img_data, np.uint8)
  28. print(type(nparr), nparr.shape, nparr.dtype)
  29. img_restore = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
  30. print(type(img_restore), img_restore.shape, img_restore.dtype)
  31. cv2.imshow("img_restore", img_restore)
  32. cv2.waitKey(0)




复制代码












欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/) Powered by Discuz! X3.4