Open starsky0426 opened 9 months ago
from PIL import Image from io import BytesIO import base64 img = Image.open(file_name) # path to file img_buffer = BytesIO() img.save(img_buffer, format=img.format) byte_data = img_buffer.getvalue() base64_str = base64.b64encode(byte_data) # bytes base64_str = base64_str.decode("utf-8") # str
As the code above in README, if we decode the base64_str back to PIL image, and compare with the image directly opened with Image.open(fillepath), they have minor differences.
Image.open(fillepath)
we can compare them by converting images to numpy array like below:
image = Image.open(BytesIO(base64.urlsafe_b64decode(base64_str))) diff = np.array(image) - np.array(Image.open(image_path)) print(np.max(diff))
The correct way to encode an image to base64 string is as below:
with open(image_path, 'rb') as f: base64_str = base64.b64encode(f.read()).decode('utf-8') return base64_str
As the code above in README, if we decode the base64_str back to PIL image, and compare with the image directly opened with
Image.open(fillepath)
, they have minor differences.we can compare them by converting images to numpy array like below:
The correct way to encode an image to base64 string is as below: