python篇---base64码流转成图片保存

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())

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心惠天意

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值