I have a service which takes input a base64 image, converts it to numpy array, performs some operation on it and then converts it back to base64 image, which is sent as output. But the numpy array gets modified during the conversion to base64.
I have created an example to demonstrate the problem.
import base64
from io import BytesIO
import numpy as np
from PIL import Image
img = np.array([[[1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3]]]
).astype(np.uint8)
print(img)
image_format = 'JPEG'
pil_img = Image.fromarray(img)
buff = BytesIO()
pil_img.save(buff, format=image_format)
encrypted_image = base64.b64encode(buff.getvalue()).decode('utf-8')
base64_decoded = base64.b64decode(encrypted_image)
img2 = Image.open(BytesIO(base64_decoded))
img2 = np.array(img2)
print(img2)
Output:
[[[1 2 3]
[1 2 3]]
[[1 2 3]
[1 2 3]]]
[[[1 2 4]
[1 2 4]]
[[1 2 4]
[1 2 4]]]
The last column of the img2 is different to that of img but I expected it to be the same.
Is the conversion to base64 wrong? If in what format should I take the input for my service?
1 Answer 1
Notice that you are not simply doing base64 encoding/decoding.
Instead of x == base64.decode(base64.encode(x))
you are doing
base64.decode(base64.encode(jpeg.deflate(jpeg.compress(x)))) == x
The part of base64 is bit accurate, but the part of JPEG compression will give only an approximation. In other words is not tru that jpeg.deflate(jpeg.compress(x))
always equals x
.
Comments
Explore related questions
See similar questions with these tags.
buff.seek(0); img2 = Image.open(buff)
. Its not the encoding.