python将图片转换为base64

脚本转换

# 二进制方式打开图片文件
import base64

f = open(r"C:\Users\Admin\Desktop\新建文件夹 (2)\Snipaste_2022-09-03_21-08-19.png", "rb")

#读取文件的内容转换为base64编码
ls_f = str(base64.b64encode(f.read()))
bs_f = open(r"C:\Users\Admin\Desktop\新建文件夹 (2)\bs.txt","w+")
bs_f.write(ls_f)
print(ls_f)
f.close()

前端转换

# Base64 Encoder - encodes a folder of PNG files and creates a .py file with definitions
import PySimpleGUI as sg
import os
import base64

'''
    将图片转换为base64格式
    先选择存放图片的文件夹
    然后会生成一个outpt.py文件夹。里面存放了转换后的内容。用sublime打开。
    input:  folder with .png .ico .gif 's
    output: output.py file with variables
'''

def main():
    OUTPUT_FILENAME = 'output.py'

    folder = sg.popup_get_folder('Source folder for images\nImages will be encoded and results saved to %s'%OUTPUT_FILENAME,
                               title='Base64 Encoder')

    if not folder:
        sg.popup_cancel('Cancelled - No valid folder entered')
        return
    try:
        namesonly = [f for f in os.listdir(folder) if f.endswith('.png') or f.endswith('.ico') or f.endswith('.gif')]
    except:
        sg.popup_cancel('Cancelled - No valid folder entered')
        return

    outfile = open(os.path.join(folder, OUTPUT_FILENAME), 'w')

    for i, file in enumerate(namesonly):
        contents = open(os.path.join(folder, file), 'rb').read()
        encoded = base64.b64encode(contents)
        outfile.write('\n{} = {}'.format(file[:file.index(".")], encoded))
        sg.OneLineProgressMeter('Base64 Encoding', i+1, len(namesonly), key='-METER-')

    outfile.close()
    sg.popup('Completed!', 'Encoded %s files'%(i+1))

if __name__ == '__main__':
    main()

你可能感兴趣的:(python,python,开发语言)