Created
September 7, 2014 08:22
-
-
Save yuheiomori/d3113ea8c221e1a0207c to your computer and use it in GitHub Desktop.
画像ファイルをbase64に変換する、またファイルに書き戻す
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
import base64 | |
def convert_file_to_b64_string(file_path): | |
"""ファイルをbase64にエンコードする | |
""" | |
with open(file_path, "rb") as f: | |
return base64.b64encode(f.read()) | |
def convert_b64_string_to_file(s, outfile_path): | |
"""base64をデコードしてファイルに書き込む | |
""" | |
with open(outfile_path, "wb") as f: | |
f.write(base64.b64decode(s)) | |
if __name__ == "__main__": | |
s = convert_file_to_b64_string("icon.PNG") | |
convert_b64_string_to_file(s, "./copy.PNG") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment