When handling image data in Python, it may be handled by "numpy (opencv)", "pillow", and "byte". A memorandum for converting each type.
num_bytes = num_numpy.tobytes()
numpy -> pil
num_pil = Image.fromarray(num_numpy)
pil -> bytes
num_byteio = io.BytesIO()
num_byteio.save(num_pil, format='png')#Temporarily make it png
num_bytes = num_byteio.getvalue()
pil -> numpy
num_numpy = np.asarray(num_pil)
bytes -> numpy
num_byteio = io.BytesIO(num_bytes)
with Image.open(num_byteio) as img
num_numpy = np.asarray(img)
bytes -> pil
num_byteio = io.BytesIO(num_bytes)
num_pil = Image.open(num_byteio)
Recommended Posts