본문 바로가기

연구 관련

How to Convert Image to Base64 String in Python

반응형

Convert Image to Base64 String

import base64
b64_bytes = base64.b64encode(img_file.read())
print(b64_str)

output:

b'f4nhf4ngd4bjfYrjeofidIbhc4XecobdcoffZ4Lfb4XcaoHfc4~~~~~~~~TS11UzyHVEOfUUawUEe2'

The output type is bytes starting with b’.

Or you can say your base64 encoded string is in the pair of single quotation.

 

In order to remove b’ from the prefix of base64 code in Python,

b64_str = b64_bytes.decode('utf-8')
print(b64_str)

 

반응형