【问题标题】:Read base64 file and convert to jpeg读取base64文件并转换为jpeg
【发布时间】:2019-02-16 04:50:56
【问题描述】:
我有一个包含二进制格式文件名列表的文件夹,我想将所有二进制文件转换为 jpeg 并将其保存在另一个文件夹中。
这是我正在使用的代码,它在处理单个文件时运行良好
import base64
with open('12345-01Image.txt', 'rb') as rf:
b64 = base64.b64decode(rf.read())
with open('12345.jpeg', 'wb') as wf:
wf.write(b64)
我想以二进制格式迭代每个文件并将其保存为 jpeg。
非常感谢您的帮助!!!
【问题讨论】:
标签:
python-3.x
python-2.7
base64
anaconda
【解决方案1】:
这应该可以正常工作。只需使用您的代码添加地图功能即可。
def bs64_txt_image(text_file: str, extension: str='jpeg'):
with open(text_file, 'rb') as bin_bs64:
img_bin = base64.decode(bin_bs64.read())
with open('.'.join([text_file.split('-')[0], extension]), 'wb') as img:
img.write(img_bin)
if __name__ == '__main__':
map(lambda file: bs64_txt_image(file), os.listdir('/path/to/folder'))