Python 图片批量转BASE64
import os
import xlwt
import base64
if __name__ == '__main__':
ws = xlwt.Workbook(encoding='utf8')
worksheet = ws.add_sheet('ImageToBase64')
worksheet.write(0,0,'name')
worksheet.write(0,1,'base64')
excel_row = 1
for filename in os.listdir('rootDir'):
pathname = os.path.join('rootDir', filename)
encoded = base64.b64encode(open(pathname, 'rb').read())
worksheet.write(excel_row, 0, filename.replace('.jpg',''))
bstr = str(encoded,'UTF-8')
print(bstr)
worksheet.write(excel_row, 1, 'data:image/jpeg;base64,'+bstr)
excel_row += 1
exist_file = os.path.exists('imagetobase64.xls')
if exist_file:
os.remove(r'imagetobase64.xls')
ws.save('imagetobase64.xls')