Advertisement
robertvari

base64 encodin

Dec 18th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.35 KB | None | 0 0
  1. def encode_image(path):
  2.     file_path = path
  3.     ext = file_path.split('.')[-1]
  4.  
  5.     file_str = base64.b64encode(get_file_data(file_path))
  6.     return u"data:image/{};base64, {}".format(ext, file_str.decode("utf-8"))
  7.  
  8.  
  9. def get_file_data(file_path):
  10.     with open(file_path, 'rb') as f:
  11.         data = f.read()
  12.         f.close()
  13.         return data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement