Various transcoding (bytes, string, base64, numpy array, io, BufferedReader)

Conversions between bytes and string

  Python3 most important new features probably be the text and binary data made a clearer distinction. Text always Unicode, represented by str type binary data type represented by bytes. Str Python3 not mix any bytes and implicit manner, which is particularly clear that the distinction between the two. Byte strings and can not be joined packages, the bag can not search string in bytes (or vice versa), nor can the string parameter passed as a function of bytes of the packet (or vice versa)

B = B1 ' SDF ' 
S1 = ' SAG ' 
Print (type (B1), type (S1)) # <class 'bytes'> <class 'STR'> 
# bytes decoded by utf8 manner as STR 
B2 = b1.decode ( ' utf8 ' ) 

# STR by way utf8 encoded into bytes 
S2 = s1.encode ( ' utf8 ' )

 

base64 transcoding

import base64

with open("img.png", "rb") as f:
    # b64encode是编码,b64decode是解码
    base64_data = base64.b64encode(f.read())

    print(base64_data)
    r = base64.b64decode(base64_data)
    print(type(r), r)
    f.close()

 

bytes 转成 numpy array

import cv2
import numpy as np

b = b'aaaaaaaaa' # bytes

image_array1 = np.frombuffer(b, dtype=np.uint8) # numpy array

img_decode = cv2.imdecode(image_array1, 1) # 效果等同于cv2.imread()

 

BytesIO 和 StringIO

  Python3 in BytesIO and StringIO biggest advantage is that you can read and write operation is performed in memory, with respect to the disk-level IO only saving time but also save a probability of error

StringIO

from IO Import the StringIO 
F = the StringIO () 
f.write ( ' Hello ' ) # memory writing operation level 
Print (f.getvalue ()) # get file contents (disk IO read operation corresponding to ()) 

# can also be used initializing the StringIO a str, then read the same document reads as: 
from IO Import the StringIO 
F = the StringIO ( ' the Hello \ Nhi \ nGoodbye!!! ' )
 the while True: 
    S = f.readline ()
     IF S == '' :
         BREAK 
    Print (s.strip ())
 '' '
Hello!
Hi!
Goodbye!
'''

 

BytesIO

  StringIO operation can only be str, if you are working with binary data, you need to use BytesIO, BytesIO achieve read and write bytes in memory

from IO Import BytesIO 
F = BytesIO () 
f.write ( ' Chinese ' .encode ( ' UTF-. 8 ' ))
 Print (f.getvalue ()) # B '\ XE4 \ XB8 \ XAD \ XE6 \ X96 \ the x87' 
# write is not str, but after UTF-8 encoded bytes. 

# And StringIO the like, bytes can be used to initialize a BytesIO, then read the same document reads as: 
from IO Import BytesIO 
F = BytesIO (B ' \ XE4 \ XB8 \ XAD \ XE6 \ X96 \ the x87 ' ) 
reached, f.read () # b '\ XE4 \ XB8 \ XAD \ XE6 \ X96 \ x87'

 

Note pointer problem

  .getvalue () can check the situation after the entire input .write () initial position is 0, the number of input into how many, after the initial position of the same structure directly or 0 .seek () is to go directly to a position .tell ( ) you can view the current location .read () End automatically read pointer position to the end

the StringIO = F ( ' ABC ' ) 
reached, f.read () # Returns 'abc' 
reached, f.read () # Returns '' as used after a read pointer moves 
f.getvalue () # Returns 'abc' is not because getvalue Effect pointer by 

F = the StringIO ( '' ) 
f.write ( ' ABC ' ) 
reached, f.read () # returns '' has as write pointer has moved 
f.getvalue () # returns 'abc' is not a pointer because getvalue Effect 
f.seek (0) # solution: the seek pointer to zero 
reached, f.read () # returns 'abc'

 

 

BufferedReader

  BufferedReader Filestorage type can convert transmitted data into BufferedReader postman type, then it can be converted into numpy array, the operation of cv2

= request.file.get IMG ( ' XXX ' ) # is assumed to pass the postman image to the server, the server receives for the IMG 

# turn into BufferedReader format 
img_buff = BufferedReader (IMG) 

# obtain byte data 
img_byte = BufferedReader.read ( img_buff) 

# transfected into numpy array 
nparr = np.frombuffer (img_byte, DTYPE = np.uint8) 

# transfer data to the same effect cv2.imread 
img_decode = cv2.imdecode (nparr,. 1 )
 Print (img_decode.shape)

 

 

 

 

 

 

                           

Guess you like

Origin www.cnblogs.com/zhuminghui/p/11359858.html