Files
python_tools/updata/pic_2py.py
2023-09-15 10:53:44 +08:00

34 lines
920 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import base64
def pictopy(picture_names, py_name):
"""
将图像文件转换为py文件
:param picture_name:
:return:
"""
write_data = []
for picture_name in picture_names:
filename = picture_name.replace('.', '_')
open_pic = open("%s" % picture_name, 'rb')
b64str = base64.b64encode(open_pic.read())
open_pic.close()
# 注意这边b64str一定要加上.decode()
write_data.append('%s = "%s"\n' % (filename, b64str.decode()))
f = open('%s.py' % py_name, 'w+')
for data in write_data:
f.write(data)
f.close()
# pics = ["logo_2.png", "logo.ico"] 中的图片存放在PicToPy.py同一目录中运行完成后
# 会在当前路径看到一个memory_pic.py文件
# pics = ["logo_2.png", "subway.ico"]
pics = ["icon.ico"]
pictopy(pics, 'memory_pic') # 将pics里面的图片写到 memory_pic.py 中
print("ok")