广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python篇---base64码流转成图片保存
  • 181
分享到

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

python深度学习开发语言 2023-09-02 09:09:17 181人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

python篇—base64码流转成图片保存 import osimport base64from io import BytesIOfrom PIL import Imagefrom faker i

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

import osimport base64from io import BytesIOfrom PIL import Imagefrom faker import Fakerfak = 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 imageif __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 Fakerfak = 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())

在这里插入图片描述

来源地址:https://blog.csdn.net/m0_46825740/article/details/127869841

--结束END--

本文标题: python篇---base64码流转成图片保存

本文链接: https://www.lsjlt.com/news/390523.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作