Cenk Bircanoglu
Cenk Bircanoglu

Reputation: 529

Pillow encoding and decoding result with different base64

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?

Upvotes: 1

Views: 468

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

The fact that your base-64 strings differ does not necessarily mean that you have lost information. If you want a better analysis/answer you will need to provide your images and base-64 representations in their exact, original form - probably via Dropbox or Google Drive - because most websites strip images of geographic and other information which affects the base-64 representation.

So, going back to your question, there are several possible explanations of why the base-64 representation could differ:

  • firstly, a PNG image often includes a date/time of last change as the ancillary tIME chunk, so merely opening and resaving a solid black image 1 second later could change the base-64 representation

  • secondly, although lossless, it is not guaranteed that two libraries will make the same decisions in terms of bit-depth, palette, alpha, filtering and so on. For example, one library may choose to save an image with a limited number of colours as a palette image, whereas another may choose an RGB representation and another may choose an RGBA representation. See here. One may choose an 8-bit per sample PNG, another may choose a 16-bit per sample PNG.

Upvotes: 1

Related Questions