图片不形式数据之间的转换(PIL.Image,cv2,bytes,base64等)

一、常用图片库:
opencv、PIL

二、图片的数据类型及可能情况
1.rgb或bgr byte array是一个可变的序列,每个元素的值的取值范围是[0, 255]

import cv2
img = cv2.imread(1.jpg", 1)       #img为
cv2.imwrite("out.jpg", img)


2.二进制流< class 'bytes'>

with open(img_url, 'rb') as f:
  a = f.read()             # a为二进制流


3.BytesIO对象

b = io.BytesIO(a)       # b为BytesIO对象, BytesIO实现了在内存中读写Bytes


4.pil image

import io
from PIL import Image
img = Image.open(b)      # b为BytesIO对象,img为PIL图片对象
img.save("out.jpg")

三、之间的转换关系

  1. BytesIO对象 <-> 二进制

    • 二进制 (a)-> BytesIO对象(b)
      b = io.BytesIO(a)
    • BytesIO对象(b) -> 二进制(a)
      a = b.getvalue()
  2. BytesIO对象 <-> np序列

    • BytesIO对象(a) -> np序列(b)
      b = cv2.imdecode(np.frombuffer(a.getvalue(), np.uint8), 1)
    • np序列(b) -> BytesIO对象(a)
      is_success, buffer = cv2.imencode(".jpg", b)
      a = io.BytesIO(buffer)
  3. 二进制 <-> np序列

    • 二进制 -> np序列
      np序列 = cv2.imdecode(np.frombuffer(二进制, np.uint8), 1)
    • np序列 -> 二进制
      二进制 = np序列.tobytes()
    • cv2.imread格式的图片转成字节流                                                                              success, encoded_image = cv2.imencode(".jpg", img)    # 对数组的图片格式进行编码  img_bytes = encoded_image.tostring()        # 将数组转为bytes
  4. PIL <-> rgb序列

    • np序列->PIL
      PIL_image = Image.fromarray(np序列)
    • PIL -> np序列
      np序列 = numpy.array(PIL_image)
  5. PIL <-> BytesIO对象

    • BytesIO对象 -> PIL
      PIL_image = Image.open(BytesIO对象)
    • PIL -> BytesIO对象
      b_img= io.BytesIO()      # 创建一个空的Bytes对象
      PIL_image.save(b_img, format='JPEG')   # 保存为JPEG格式                                        b_img= b_img.getvalue()  # 获取图片二进制数据 
  6. PIL <-> 二进制

    • PIL -> 二进制
      b = io.BytesIO()
      PIL_image.save(b, format='JPEG')
      b.getValue()

    • 二进制 -> PIL
      b = io.BytesIO(二进制)
      PIL = Image.open(b)

  7. bgr和rgb的array转换
    RGB序列 = cv2.cvtColor(BGR序列,cv2.COLOR_BGR2RGB)

  8. imencode()+imdecode()使用                                                            
    cv2.imdecode()函数从指定的内存缓存中读取数据,并把数据转换(解码)成图像格式;主要用于从网络传输数据中恢复出图像。
    cv2.imencode()函数是将图片格式转换(编码)成流数据,赋值到内存缓存中;主要用于图像数据格式的压缩,方便网络传输。

    # -*- coding: utf-8 -*-
    import numpy as np
    import urllib
    import cv2
     
    img = cv2.imread('0122.jpg')
    # '.jpg'表示把当前图片img按照jpg格式编码,按照不同格式编码的结果不一样
    img_encode = cv2.imencode('.jpg', img)[1]
    # imgg = cv2.imencode('.png', img)
     
    data_encode = np.array(img_encode)
    str_encode = data_encode.tostring()
     
    # 缓存数据保存到本地,以txt格式保存
    with open('img_encode.txt', 'w') as f:
        f.write(str_encode)
        f.flush    #刷新缓冲区
     
    with open('img_encode.txt', 'r') as f:
        str_encode = f.read()
     
    nparr = np.fromstring(str_encode, np.uint8)
    img_decode = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    #image = np.asarray(bytearray(str_encode), dtype="uint8")    #也可用这两句替换上面两句
    #image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    cv2.imshow("img_decode", img_decode)
    cv2.waitKey()

你可能感兴趣的:(opencv,计算机视觉,python)