python将图片文件转换成base64编码的方法


Posted in Python onMarch 14, 2015

本文实例讲述了python将图片文件转换成base64编码的方法。分享给大家供大家参考。具体实现方法如下:

import base64
f=open(r'c:\3water.gif','rb') #二进制方式打开图文件
ls_f=base64.b64encode(f.read()) #读取文件内容,转换为base64编码
f.close()

调用方法如下:

<img src="R0lGODlh1wBOAPcAAAAAAP///7a4u+jq7bG1ucrN0N7g4tLU1u3u7+Tl5uHi49na26quscLGybe9wc7R07rBxfHz9O/x8ubo6d3f4Nrc3dXY2evs7Ojp6cnKyv/++f7788G5rc+tgdKzi72zpf3376eAUK6FVLGIV597TrSLWrWNXbOLXMWkfOTNsu3dyvfu47CIWr+XbJV8YZSFdPDk1vX08+3s67Snm927n9q1mdewldO9rtGkh8uYfIdoV8WuoXpYRujm5djW1bZ0Vrx9YI5fSsCBZcWKcJyTj7BpS35QPbKtq4ZJMq5kSKpYPZlTO5GLiYpBLX8+K6KbmZFEMcK+vby4t7q2taJJN6FGN6dSRMSMgtCjm9aup+LFwOfRze3c2fPo5vnz8pQ9L59GOK1dULNpXbl0ab6AdsqXj9y5tJ5BNZw9NL27u7GwsKOiooKBgf39/fv7+/n5+fb29vPz8/Ly8vHx8e/v7+Tk5N/f39fX19DQ0MnJycDAwLy8vK6urqGhoZSUlIeHh3p6emxsbF9fX1FRUURERDY2NikpKQMDA////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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vod59VkJFtuYti2J9heYZFGZYAaoYB0AV7eJdOaYgSeRA1CYtQSRBcIJ602ZLhyZ66aRH5s5UFMZyO+ZiOCZwg2pgD8aGRaaIJsQAMQBFpCZsB0JZVsJ4DMX7QKIH7SRA0OhA2mpESaIBEKZHaKItX6YZbsAW7aIsCoQUMOH4M+J+CGaGtaYFt2ZxJyplgSQZbEI4VAQcKsJgJQaLAWaIl6qEE8ZtmqhAXsAY5QxFCOqN8SRC7yJngKafY+Y9zShBWYAVbeJdaoICXIZRbQKOXQQbiaaH1kYtTCqXT6Zp0/7gZ1xmLg5oRdECFCwGiByGfwXmmAiGfAxEDDECpb/GCEnp+BWF+pPoRXFCkdpmDGYEBoIoQlmoQmFqmYrqpxMkAPsCSNAGWEFp8BJEAVnSpYqqpYdqhtGqryOqODBB2opGFglmovloQCkABo/WhY3qi1iqiIaqtHhqccMAANDcafUoGxRitBqEAB8ChF8GpBiEDDJB75uqrGPAAKcQR25oQFrAGwRqvvjoHBRBRJwEHEMAAa8qv0doGFlAA9ToSFLAGRWiw/HoBDXAA7dgRGCAAxgmxGhsAFKAHB/B4GZEADrAGXrmxBvsGFOAAGaA/F+FWBMAAJWuyG9sDDSAABV5gALX0EBdgARCwBg4gHTIbtATBpXpAAGmQARZgAAMAIQPhBnMAUQ/QAAzAAFFgAAMotFgrEJP6ABBAAAQgAGArAGowtQ6ABwpQsFmbtgmhI3PQtnHgpWobt3IrswEBADs=" />

关于图片转base64还可参考在线工具:

在线图片转换BASE64工具

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python高级用法总结
May 26 Python
python让列表倒序输出的实例
Jun 25 Python
python去除文件中重复的行实例
Jun 29 Python
django.db.utils.ProgrammingError: (1146, u“Table‘’ doesn’t exist”)问题的解决
Jul 13 Python
使用python Fabric动态修改远程机器hosts的方法
Oct 26 Python
我用Python抓取了7000 多本电子书案例详解
Mar 25 Python
在windows下使用python进行串口通讯的方法
Jul 02 Python
如何在Windows中安装多个python解释器
Jun 16 Python
解决Keras使用GPU资源耗尽的问题
Jun 22 Python
python搜索算法原理及实例讲解
Nov 18 Python
在 Golang 中实现 Cache::remember 方法详解
Mar 30 Python
如何利用python和DOS获取wifi密码
Mar 31 Python
python在Windows8下获取本机ip地址的方法
Mar 14 #Python
python检测远程端口是否打开的方法
Mar 14 #Python
python检测远程udp端口是否打开的方法
Mar 14 #Python
python检测远程服务器tcp端口的方法
Mar 14 #Python
python循环监控远程端口的方法
Mar 14 #Python
python通过shutil实现快速文件复制的方法
Mar 14 #Python
python监控网站运行异常并发送邮件的方法
Mar 13 #Python
You might like
解析zend studio中直接导入svn中的项目的方法步骤
2013/06/21 PHP
解析mysql中UNIX_TIMESTAMP()函数与php中time()函数的区别
2013/06/24 PHP
php smarty truncate UTF8乱码问题解决办法
2014/06/13 PHP
解密ThinkPHP3.1.2版本之模板继承
2014/06/19 PHP
使用GDB调试PHP代码,解决PHP代码死循环问题
2015/03/02 PHP
php中将一个对象保存到Session中的方法
2015/03/13 PHP
PHP实现json_decode不转义中文的方法
2017/05/20 PHP
yii2.0整合阿里云oss上传单个文件的示例
2017/09/19 PHP
iframe 异步加载技术及性能分析
2011/07/19 Javascript
JS关闭窗口或JS关闭页面的几种代码分享
2013/10/25 Javascript
4种JavaScript实现简单tab选项卡切换的方法
2016/01/06 Javascript
基于JavaScript实现动态添加删除表格的行
2016/02/01 Javascript
一步一步封装自己的HtmlHelper组件BootstrapHelper(二)
2016/09/14 Javascript
用jQuery实现优酷首页轮播图
2017/01/09 Javascript
基于React实现表单数据的添加和删除详解
2017/03/14 Javascript
Element-ui之ElScrollBar组件滚动条的使用方法
2018/09/14 Javascript
Vue+Element实现网页版个人简历系统(推荐)
2019/12/31 Javascript
微信小程序订阅消息(java后端实现)开发
2020/06/01 Javascript
vue 判断页面是首次进入还是再次刷新的实例
2020/11/05 Javascript
python使用nntp读取新闻组内容的方法
2015/05/08 Python
Django基础之Model操作步骤(介绍)
2017/05/27 Python
Python实现的txt文件去重功能示例
2018/07/07 Python
Python 单元测试(unittest)的使用小结
2018/11/14 Python
python动态视频下载器的实现方法
2019/09/16 Python
使用python批量转换文件编码为UTF-8的实现
2020/04/03 Python
Python实现邮件发送的详细设置方法(遇到问题)
2021/01/18 Python
利用纯CSS3实现tab选项卡切换示例代码
2016/09/21 HTML / CSS
Toppik顶丰增发纤维官网:解决头发稀疏
2017/12/30 全球购物
惊艳的手工时装首饰:Migonne Gavigan
2018/02/23 全球购物
学生个人求职自荐信格式
2013/09/23 职场文书
医科大学毕业生自荐信
2014/02/03 职场文书
2015年学校食堂工作总结
2015/04/22 职场文书
小区物业管理2015年度工作总结
2015/10/22 职场文书
评奖评优个人先进事迹材料
2015/11/04 职场文书
mongodb数据库迁移变更的解决方案
2021/09/04 MongoDB
Windows11性能真的上涨35%? 桌面酷睿i9实测结果公开
2021/11/21 数码科技