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

 更新时间:2015年03月14日 15:37:30   作者:八大山人  
这篇文章主要介绍了python将图片文件转换成base64编码的方法,涉及Python操作base64编码的技巧,需要的朋友可以参考下

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

import base64
f=open(r'c:\jb51.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////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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mLQluG4i1yAmQGQfV5gBb0oENApndRpnapJlKwpllWwha//yY9/KZuxSRAhaQWgeRCISRGQw5ghMZwH0QDRJhE1uYvod59VkJFtuYti2J9heYZFGZYAaoYB0AV7eJdOaYgSeRA1CYtQSRBcIJ602ZLhyZ66aRH5s5UFMZyO+ZiOCZwg2pgD8aGRaaIJsQAMQBFpCZsB0JZVsJ4DMX7QKIH7SRA0OhA2mpESaIBEKZHaKItX6YZbsAW7aIsCoQUMOH4M+J+CGaGtaYFt2ZxJyplgSQZbEI4VAQcKsJgJQaLAWaIl6qEE8ZtmqhAXsAY5QxFCOqN8SRC7yJngKafY+Y9zShBWYAVbeJdaoICXIZRbQKOXQQbiaaH1kYtTCqXT6Zp0/7gZ1xmLg5oRdECFCwGiByGfwXmmAiGfAxEDDECpb/GCEnp+BWF+pPoRXFCkdpmDGYEBoIoQlmoQmFqmYrqpxMkAPsCSNAGWEFp8BJEAVnSpYqqpYdqhtGqryOqODBB2opGFglmovloQCkABo/WhY3qi1iqiIaqtHhqccMAANDcafUoGxRitBqEAB8ChF8GpBiEDDJB75uqrGPAAKcQR25oQFrAGwRqvvjoHBRBRJwEHEMAAa8qv0doGFlAA9ToSFLAGRWiw/HoBDXAA7dgRGCAAxgmxGhsAFKAHB/B4GZEADrAGXrmxBvsGFOAAGaA/F+FWBMAAJWuyG9sDDSAABV5gALX0EBdgARCwBg4gHTIbtATBpXpAAGmQARZgAAMAIQPhBnMAUQ/QAAzAAFFgAAMotFgrEJP6ABBAAAQgAGArAGowtQ6ABwpQsFmbtgmhI3PQtnHgpWobt3IrswEBADs=" />

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

在线图片转换BASE64工具

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

相关文章

  • python+logging+yaml实现日志分割

    python+logging+yaml实现日志分割

    这篇文章主要为大家详细介绍了python+logging+yaml实现日志分割,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • python中Array和DataFrame相互转换的实例讲解

    python中Array和DataFrame相互转换的实例讲解

    在本篇文章里小编给大家整理的是一篇关于python中Array和DataFrame相互转换的实例讲解内容,对此有需要的朋友们可以学参考下。
    2021-02-02
  • 使用并行处理提升python for循环速度的过程

    使用并行处理提升python for循环速度的过程

    Python 是一门功能强大的编程语言,但在处理大规模数据或复杂计算任务时,性能可能成为一个瓶颈,这篇文章主要介绍了使用并行处理提升python for循环速度,需要的朋友可以参考下
    2023-06-06
  • python numpy 显示图像阵列的实例

    python numpy 显示图像阵列的实例

    今天小编就为大家分享一篇python numpy 显示图像阵列的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • python+opencv实现车牌定位功能(实例代码)

    python+opencv实现车牌定位功能(实例代码)

    这篇文章主要介绍了python+opencv实现车牌定位功能,需要实现对给定的车牌进行车牌识别,本文通过实例代码讲解,需要的朋友可以参考下
    2019-12-12
  • Python变量命名规范的总结

    Python变量命名规范的总结

    在Python编程中,变量命名规范对于编写优雅和可维护的代码至关重要,本文主要介绍了Python变量命名规范的总结,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • Python安装Bs4及使用方法

    Python安装Bs4及使用方法

    这篇文章主要介绍了Python安装Bs4及使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 学python安装的软件总结

    学python安装的软件总结

    在本篇文章里小编给大家整理了是关于学python安装什么软件的相关知识点内容,有需要的朋友们学习下。
    2019-10-10
  • 纯numpy数值微分法实现手写数字识别

    纯numpy数值微分法实现手写数字识别

    本文主要介绍了纯numpy数值微分法实现手写数字识别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • TensorFlow 模型载入方法汇总(小结)

    TensorFlow 模型载入方法汇总(小结)

    这篇文章主要介绍了TensorFlow 模型载入方法汇总(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06

最新评论