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实现360的字符显示界面
Feb 21 Python
Python ValueError: invalid literal for int() with base 10 实用解决方法
Jun 21 Python
详解Python中contextlib上下文管理模块的用法
Jun 28 Python
python实现稀疏矩阵示例代码
Jun 09 Python
Java编程迭代地删除文件夹及其下的所有文件实例
Feb 10 Python
python requests爬取高德地图数据的实例
Nov 10 Python
python爬虫项目设置一个中断重连的程序的实现
Jul 26 Python
python实现多线程端口扫描
Aug 31 Python
Python 类,property属性(简化属性的操作),@property,property()用法示例
Oct 12 Python
Python imutils 填充图片周边为黑色的实现
Jan 19 Python
如何在django中添加日志功能
Feb 06 Python
基于python实现地址和经纬度转换
May 19 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的Json中文处理解决方案
2016/09/29 PHP
使用laravel指定日志文件记录任意日志
2019/10/17 PHP
script不刷新页面的联动前后代码
2013/09/18 Javascript
可插入图片的TEXT文本框
2013/12/27 Javascript
JS实现可关闭的对联广告效果代码
2015/09/14 Javascript
JQuery validate插件验证用户注册信息
2016/05/11 Javascript
javascript中获取元素标签中间的内容的实现方法
2016/10/08 Javascript
微信小程序 swiper制作tab切换实现附源码
2017/01/21 Javascript
详解Vue学习笔记入门篇之组件的内容分发(slot)
2017/07/17 Javascript
Vue之Watcher源码解析(1)
2017/07/19 Javascript
vue中各组件之间传递数据的方法示例
2017/07/27 Javascript
javascript基础进阶_深入剖析执行环境及作用域链
2017/09/05 Javascript
Angular2的管道Pipe的使用方法
2017/11/07 Javascript
JavaScript满天星导航栏实现方法
2018/03/08 Javascript
使用apifm-wxapi模块中的问题及解决方法
2019/08/05 Javascript
javascript中innerHTML 获取或替换html内容的实现代码
2020/03/17 Javascript
vue中v-for循环选中点击的元素并对该元素添加样式操作
2020/07/17 Javascript
在Python程序中实现分布式进程的教程
2015/04/28 Python
总结Python编程中三条常用的技巧
2015/05/11 Python
python实现数独算法实例
2015/06/09 Python
NLTK 3.2.4 环境搭建教程
2018/09/19 Python
在python中bool函数的取值方法
2018/11/01 Python
浅析Python 读取图像文件的性能对比
2019/03/07 Python
python爬取基于m3u8协议的ts文件并合并
2019/04/26 Python
django的ORM操作 删除和编辑实现详解
2019/07/24 Python
Python Gluon参数和模块命名操作教程
2019/12/18 Python
详解vscode实现远程linux服务器上Python开发
2020/11/10 Python
摩托车和ATV零件、配件和服装的首选在线零售商:MotoSport
2017/12/22 全球购物
main 主函数执行完毕后,是否可能会再执行一段代码,给出说明
2012/12/05 面试题
劳动工资科岗位职责范本
2014/03/02 职场文书
个人借款担保书
2014/04/02 职场文书
保密工作责任书
2014/04/16 职场文书
英文投诉信格式
2015/07/03 职场文书
2016形势与政策学习心得体会
2016/01/12 职场文书
php修改word的实例方法
2021/11/17 PHP
【DOTA2】半决赛强强对话~ PSG LGD vs EHOME - DPC 2022 CN REGIONAL FINALS WINTER
2022/04/02 DOTA