博客
关于我
python 将图片与字符串相互转换
阅读量:797 次
发布时间:2023-03-07

本文共 507 字,大约阅读时间需要 1 分钟。

将图片转换为Base64编码的方法如下:方法一:打开图片文件:`with open(image,'rb') as f:`  读取图片内容并进行Base64编码:`str=base64.b64encode(f.read())`  打印编码结果:`print(type(str))`方法二:打开图片文件:`f=open(image,'rb')`  读取图片内容并进行Base64编码:`f_str=base64.b64encode(f.read())`  关闭文件:`f.close()`  打印编码结果:`print(type(f_str))`将Base64编码转换回图片:打开新文件:`file_str=open('2.jpg','wb')`  将编码内容写入文件:`file_str.write(base64.b64decode(str))`  关闭文件:`file_str.close()`

 

说明:本文展示了两种将图片文件转换为Base64编码的方法,并提供了将编码后的内容解码并保存为新图片文件的步骤。代码展示了从读取文件到编码和解码的完整流程,适合用于需要轻量级数据传输或存储的场景。

转载地址:http://gsofk.baihongyu.com/

你可能感兴趣的文章
python调用git bash_Python学习第70课-用Git Bash在命令行打开sublime
查看>>
python | jsonschema,一个实用的 验证 JSON 数据结构 Python 库!
查看>>
python课程的中期报告范文_课题研究中期总结报告范文
查看>>
python | lxml,一个超酷的 关于XML/HTML 文档 Python 库!
查看>>
python | mplfinance,一个有趣的金融数据可视化 Python 库!
查看>>
python | nipy,一个强大的关于 神经影像数据分析 的Python 库!
查看>>
python | NLTK,一个强大的 自然语言处理 Python 库!
查看>>
python | nupic,一个强大的 处理时间序列的Python 库!
查看>>
python | orange3,一个神奇的 Python 库!
查看>>
python | pdfminer,一个神奇的 关于PDF 文件的 Python 库!
查看>>
python | pendulum,一个有趣的 日期和时间 Python 库!
查看>>
python | pluginbase,一个神奇的 关于插件框架 的Python 库!
查看>>
python | ply,一个无敌的 词法和语法分析工具 的Python 库!
查看>>
python | py2exe,一个超酷的 Python 库!
查看>>
python | pyautogui,一个超酷的 Python 库!
查看>>
python | pybaobabdt,一个超强的 决策树可视化 Python 库!
查看>>
python | pycco,一个神奇的 Python 库!
查看>>
python | pyg2plot,一个有趣的 数据可视化 Python 库!
查看>>
python | pymc,一个超强的 Python 库!
查看>>
python | pynsist,一个强大的 Python 库!
查看>>