批量处理图片转base64编码
# -*- coding:utf-8 -*- """ __project_ = 'ToBase64' __file_name__ = 'PicToBase64' __author__ = 'xbxia' __time__ = '2020/10/26 9:37' __product_name = PyCharm """ import os import re import base64 from queue import Queue times = 0 # 保存结果到txt def save_parse_result(content): f = open('pic_base64_result.txt', 'a+', encoding='utf-8') f.write(content + '\n') f.close() # 匹配图片模式入队列 def put_picfile_to_queue(filepath, q): global times files = os.listdir(filepath) pic_re_pattern = re.compile(r"^.*\.(?:png|jpg|bmp|gif|tif|jpeg)$") for fi in files: fi_d = os.path.join(filepath, fi) if not os.path.isdir(fi_d): if pic_re_pattern.match(fi.lower()): q.put(fi_d) times += 1 else: pass # 队列发请求 def run_post(q): while not q.empty(): filepath = q.get() pic_to_base64(filepath) #图片转base64 def pic_to_base64(file): with open(file, 'rb') as fp: # 二进制方式打开图文件 fpBase64 = base64.b64encode(fp.read()) # 读取文件内容,转换为base64编码 content = '{}\n, base64:{}'.format(file, fpBase64) print(content) save_parse_result(content) if __name__ == '__main__': filepath = r"F:\Data\NewDesktop\1" # 创建Queue对象 file_queue = Queue() put_picfile_to_queue(filepath, file_queue) # 图片队列 run_post(file_queue)
本文来自博客园,作者:ReluStarry,转载请注明原文链接:https://www.cnblogs.com/relustarry/p/13897290.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统