data science/python
image byte 데이터 <-> numpy string
꼰대코더
2024. 4. 29. 09:50
인터넷 스트림으로 이미지를 전송할 경우 이미지 데이터는 string 형식으로 변환해야 한다.
import numpy as np import base64 import sys def base64_encode_image(a): # base64 encode the input NumPy array return base64.b64encode(a).decode("utf-8") def base64_decode_image(a, dtype, shape): # if this is Python 3, we need the extra step of encoding the # serialized NumPy string as a byte object if sys.version_info.major == 3: a = bytes(a, encoding="utf-8") # convert the string to a NumPy array using the supplied data # type and target shape a = np.frombuffer(base64.decodestring(a), dtype=dtype) a = a.reshape(shape) # return the decoded image return a |