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 25 Python
老生常谈python函数参数的区别(必看篇)
May 29 Python
Python中import机制详解
Nov 14 Python
用tensorflow实现弹性网络回归算法
Jan 09 Python
基于pycharm导入模块显示不存在的解决方法
Oct 13 Python
在Python中COM口的调用方法
Jul 03 Python
python关于矩阵重复赋值覆盖问题的解决方法
Jul 19 Python
基于python实现数组格式参数加密计算
Apr 21 Python
Python调用.net动态库实现过程解析
Jun 05 Python
详解python字符串驻留技术
May 21 Python
Qt自定义Plot实现曲线绘制的详细过程
Nov 02 Python
代码复现python目标检测yolo3详解预测
May 06 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
PHP集成百度Ueditor 1.4.3
2014/11/23 PHP
php中curl使用指南
2015/02/05 PHP
PHP生成器简单实例
2015/05/13 PHP
PHP 中提示undefined index如何解决(多种方法)
2016/03/16 PHP
PHP编程计算日期间隔天数的方法
2017/04/26 PHP
3款实用的在线JS代码工具(国外)
2012/03/15 Javascript
js禁止页面复制功能禁用页面右键菜单示例代码
2013/08/29 Javascript
5个JavaScript经典面试题
2014/10/13 Javascript
JavaScript中获取纯正的undefined的方法
2016/03/06 Javascript
JS判断键盘是否按的回车键并触发指定按钮点击操作的方法
2017/02/13 Javascript
详解node-ccap模块生成captcha验证码
2017/07/01 Javascript
前端主流框架vue学习笔记第一篇
2017/07/26 Javascript
vue mintui-Loadmore结合实现下拉刷新和上拉加载示例
2017/10/12 Javascript
Vuejs 2.0 子组件访问/调用父组件的方法(示例代码)
2018/02/08 Javascript
javascript对HTML字符转义与反转义
2018/12/13 Javascript
layer.prompt输入层的例子
2019/09/24 Javascript
[41:54]2018DOTA2亚洲邀请赛 4.1 小组赛A组加赛 TNC vs Liquid
2018/04/03 DOTA
Python警察与小偷的实现之一客户端与服务端通信实例
2014/10/09 Python
Python 爬虫模拟登陆知乎
2016/09/23 Python
Python利用Beautiful Soup模块创建对象详解
2017/03/27 Python
python对视频画框标记后保存的方法
2018/12/07 Python
python yield和Generator函数用法详解
2020/02/10 Python
英国家具、照明、家居用品网上商店:Wayfair.co.uk
2020/02/13 全球购物
《小石潭记》教学反思
2014/02/13 职场文书
初中生操行评语大全
2014/04/24 职场文书
班干部竞选演讲稿
2014/04/24 职场文书
实习评语大全
2014/04/26 职场文书
走群众路线剖析材料
2014/10/09 职场文书
学习心理学的体会
2014/11/07 职场文书
邀请函样本
2015/02/02 职场文书
试用期解除劳动合同通知书
2015/04/16 职场文书
2015年全国保险公众宣传日活动方案
2015/05/06 职场文书
毕业证明书
2015/06/19 职场文书
党员电教片《信仰》心得体会
2016/01/15 职场文书
详解JAVA中的OPTIONAL
2021/06/14 Java/Android
Python中Selenium对Cookie的操作方法
2021/07/09 Python