东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 2777|回复: 0
打印 上一主题 下一主题

[课堂笔记] ndarray图片数据与base64字符串之间相互转换

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14431
QQ
跳转到指定楼层
楼主
发表于 2020-5-12 16:24:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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)




复制代码







让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-4-25 17:26 , Processed in 0.181576 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表