python篇—base64码流转成图片保存
import os
import base64
from io import BytesIO
from PIL import Image
from faker import Faker
fak = Faker()
# 输入为base64格式字符串,输出为PIL格式图片
def base64_to_image(base64_str): # 用 b.show()可以展示
image = base64.b64decode(base64_str, altchars=None, validate=False)
image = BytesIO(image)
image = Image.open(image)
return image
if __name__ == '__main__':
file_path = "/app/yyq/dataset/1putuo_waste_project/important_real_data/log_results"
file_sum = os.listdir(file_path)
# 图片保存路径
save_img = "/app/yyq/dataset/1putuo_waste_project/important_real_data/bs64_img/"
if not os.path.exists(save_img):
os.makedirs(save_img)
for fi in file_sum:
file = open(os.path.join(file_path, fi), "r")
print(fi)
try:
while True:
line = file.readline()
if line:
img = base64_to_image(eval(line)["pic"])
# 随机生成时间用于保存图片
date_ = fi.split("_")[0] + "-" + fak.date(pattern='%H:%M:%S').replace(":", "-")
img_name = save_img + date_ + ".jpeg"
img.save(img_name)
print(eval(line)["processdate"], eval(line)["result"])
else:
break
except:
pass
finally:
print("{}文件处理完成".format(file))
file.close()
这里分享个比较好玩的,随机生成时间
from faker import Faker
fak = Faker()
print('日期:', fak.date(pattern = '%H:%M:%S'))
print(type(fak.date(pattern = '%H:%M:%S')))
print('年:', fak.year())
print('月:',fak.month())
print('日:', fak.day_of_month())
print('星期:', fak.day_of_week())