Image圖像數據的常用格式轉化操作實現記錄【Image、Array、Bytes相互轉化】

      最近的工作中用到的圖像數據處理比較多一點,很多地方都需要對Array、Image以及二進制字節流數據Bytes進行相互之間的轉化,總計記錄了整個過程中自己的實現方法,放在這裏備忘,需要的可以拿去使用,所有實現的方法均已經過測試可以直接使用。下面是具體的代碼實現:

#!usr/bin/env python
# encoding:utf-8
from __future__ import division

'''
__Author__:沂水寒城
功能:Image 圖像數據的常用格式轉化操作實現記錄
'''


import io
import sys
import requests
import numpy as np
from PIL import Image



if sys.version_info==2:
    reload(sys)
    sys.setdefaultencoding('utf-8')


def img2Array(pic='a.png'):
    '''
    Image 轉化爲 array 格式
    '''
    img=Image.open(pic)
    arr=np.asarray(img)
    print('arr_shape: ', arr.shape)
    print('arr_value: ', arr)
    return arr 


def array2Img(arr):
    '''
    array 轉化爲 Image 格式
    '''
    image=Image.fromarray(arr.astype('uint8')).convert('RGB')
    image.show()


def byte2Img():
    '''
    二進制字節流數據轉化爲Image格式
    '''
    url="http://seopic.699pic.com/photo/50014/4148.jpg_wh1200.jpg"
    content = requests.get(url, stream=True).content
    byteStream = io.BytesIO(content)  
    image = Image.open(byteStream)
    image.show()


def img2Byte(pic='a.png'):
    '''
    Image 轉化爲二進制字節流格式 
    '''
    img=Image.open(pic)
    imgByte=io.BytesIO()
    img.save(imgByte,format='JPEG')
    byte_res=imgByte.getvalue()
    print('byte_res: ', byte_res)


def byte2Array():
    '''
    二進制字節流數據轉化爲 Array 格式
    '''
    url="http://seopic.699pic.com/photo/50014/4148.jpg_wh1200.jpg"
    content = requests.get(url, stream=True).content
    byteStream = io.BytesIO(content)  
    image = Image.open(byteStream)
    arr=np.asarray(image)
    print('arr_shape: ', arr.shape)
    print('arr_value: ', arr)
    return arr 


def array2Byte(arr):
    '''
    Array 格式轉化爲二進制字節流格式
    '''
    img=Image.fromarray(arr.astype('uint8')).convert('RGB')
    imgByte=io.BytesIO()
    img.save(imgByte,format='JPEG')
    byte_res=imgByte.getvalue()
    print('byte_res: ', byte_res)


if __name__ == '__main__':
    #Image 轉化爲 array 格式
    arr=img2Array(pic='a.png')
    #array 轉化爲 Image 格式
    array2Img(arr)
    #二進制字節流數據轉化爲Image格式
    byte2Img()
    #Image 轉化爲二進制字節流格式
    img2Byte(pic='a.png')
    #二進制字節流數據轉化爲 Array 格式
    byte2Array()
    #Array 格式轉化爲二進制字節流格式
    array2Byte(arr)

        運行結果如下所示:

('arr_shape: ', (331L, 500L, 3L))
('arr_value: ', array([[[100,  64,  66],
        [108,  72,  74],
        [109,  73,  75],
        ...,
        [ 83,  68,  65],
        [ 83,  68,  65],
        [131, 116, 113]],

       [[ 99,  63,  65],
        [104,  68,  70],
        [105,  69,  71],
        ...,
        [ 84,  69,  66],
        [ 83,  68,  65],
        [130, 115, 112]],

       [[103,  67,  69],
        [106,  70,  72],
        [106,  70,  72],
        ...,
        [ 81,  66,  61],
        [ 81,  66,  63],
        [129, 114, 111]],

       ...,

       [[131,  85,  51],
        [136,  90,  56],
        [141,  95,  59],
        ...,
        [ 92,  61,  40],
        [ 94,  66,  45],
        [140, 113,  94]],

       [[133,  87,  53],
        [139,  93,  59],
        [144,  98,  62],
        ...,
        [ 97,  66,  45],
        [ 92,  64,  43],
        [136, 109,  88]],

       [[138,  89,  56],
        [145,  96,  63],
        [151, 103,  67],
        ...,
        [108,  78,  54],
        [ 96,  68,  46],
        [138, 111,  90]]], dtype=uint8))
[Finished in 0.4s]

('byte_res: ', '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\x1d\x1a\x1c\x1c $.\')
('arr_shape: ', (685L, 1200L, 3L))
('arr_value: ', array([[[  1, 124, 218],
        [  0, 125, 218],
        [  0, 125, 218],
        ...,
        [  0,  48, 114],
        [  0,  48, 114],
        [  0,  48, 114]],

       [[  0, 125, 218],
        [  0, 125, 218],
        [  0, 125, 218],
        ...,
        [  0,  49, 115],
        [  0,  49, 115],
        [  0,  49, 115]],

       [[  1, 126, 219],
        [  1, 126, 219],
        [  1, 126, 219],
        ...,
        [  1,  50, 116],
        [  1,  50, 116],
        [  1,  50, 116]],

       ...,

       [[159, 214, 244],
        [162, 217, 247],
        [163, 218, 248],
        ...,
        [128, 193, 225],
        [127, 192, 224],
        [127, 190, 221]],

       [[158, 213, 243],
        [161, 216, 246],
        [161, 218, 247],
        ...,
        [118, 183, 215],
        [118, 181, 212],
        [117, 180, 211]],

       [[158, 215, 244],
        [160, 217, 246],
        [159, 216, 245],
        ...,
        [125, 190, 220],
        [124, 187, 218],
        [123, 186, 217]]], dtype=uint8))
[Finished in 1.1s]
('arr_shape: ', (331L, 500L, 3L))
('arr_value: ', array([[[100,  64,  66],
        [108,  72,  74],
        [109,  73,  75],
        ...,
        [ 83,  68,  65],
        [ 83,  68,  65],
        [131, 116, 113]],

       [[ 99,  63,  65],
        [104,  68,  70],
        [105,  69,  71],
        ...,
        [ 84,  69,  66],
        [ 83,  68,  65],
        [130, 115, 112]],

       [[103,  67,  69],
        [106,  70,  72],
        [106,  70,  72],
        ...,
        [ 81,  66,  61],
        [ 81,  66,  63],
        [129, 114, 111]],

       ...,

       [[131,  85,  51],
        [136,  90,  56],
        [141,  95,  59],
        ...,
        [ 92,  61,  40],
        [ 94,  66,  45],
        [140, 113,  94]],

       [[133,  87,  53],
        [139,  93,  59],
        [144,  98,  62],
        ...,
        [ 97,  66,  45],
        [ 92,  64,  43],
        [136, 109,  88]],

       [[138,  89,  56],
        [145,  96,  63],
        [151, 103,  67],
        ...,
        [108,  78,  54],
        [ 96,  68,  46],
        [138, 111,  90]]], dtype=uint8))
('byte_res: ', '\xff\xd8\xff\xe0\x00\x10JFIF')

     這裏需要說明的是,byte_res 結果內容是非常大的,我貼上原始結果數據的時候提示【內容過長無法發佈】,所以上面的byte_res我都是隻截取了前面一小部分的數據貼出來的,僅作爲示意。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章