# -*- coding: utf-8 -*- #~ #------------------------------------------------------------------ #~ module:base64 os re #~ Filename:transfer.py #~ Description:transfer pacific file into base64 string and write into #~ a text file with form of Markdown quote #~ Function : #~ def get_filename(filepath):return files' name under filepath #~ def name_fliter(file_name_list):remove files that are not image form #~ def trans(file_name_list): base64encode image and write into txt #~ Date: 2020-1-15 #~ Author:Alexi Bi #~ Email:alexi_bi@qq.com #~ #------------------------------------------------------------------ #~ #------------------------------------------------------------------
import base64 import os import re
# return files' name under filepath def get_filename(filepath): file_name_list=[] for root,dirs,files in os.walk(filepath): file_name_list+=files return file_name_list # remove files that are not image form def name_fliter(file_name_list): for words in file_name_list[::-1]: if not (re.match(r'.*((.jpg)|(.png)|(.jpeg)|(.psd)|(.bmp))$',words)): file_name_list.remove(words) # write into a txt file def trans(file_name_list): if len(file_name_list)!=0: fs=open(text,'a+') for files in file_name_list: type=os.path.splitext(files)[1][1:] f=open(path+'\\'+files,'rb') ls_f=base64.b64encode(f.read()) fs.write('['+files+']:data:image/'+type+';base64,'+ls_f.decode()+'\n') f.close() fs.close()