I am trying to convert from base64 to PIL image and PIL image to base64. And I am using these scripts.
def convert_base64_2_pil_image(image_b64: bytes) -> Image:
image_str = base64.b64decode(image_b64)
return Image.open(io.BytesIO(image_str))
def convert_pil_image_2_base64(image: Image, format: str = 'PNG') -> bytes:
buffered = io.BytesIO()
image.save(buffered, format=format)
buffered.seek(0)
return base64.b64encode(buffered.getvalue())
When I try to convert one base64 string to a PIL image and again convert that PIL image to base64 the base64 strings are not the same. I am losing some information.
image_b64 = b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
image = convert_base64_2_pil_image(image_b64)
image_b64_converted = convert_pil_image_2_base64(image)
assert image_b64_converted == image_b64
How can I fix this problem?