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////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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ZFGZYAaoYB0AV7eJdOaYgSeRA1CYtQSRBcIJ602ZLhyZ66aRH5s5UFMZyO+ZiOCZwg2pgD8aGRaaIJsQAMQBFpCZsB0JZVsJ4DMX7QKIH7SRA0OhA2mpESaIBEKZHaKItX6YZbsAW7aIsCoQUMOH4M+J+CGaGtaYFt2ZxJyplgSQZbEI4VAQcKsJgJQaLAWaIl6qEE8ZtmqhAXsAY5QxFCOqN8SRC7yJngKafY+Y9zShBWYAVbeJdaoICXIZRbQKOXQQbiaaH1kYtTCqXT6Zp0/7gZ1xmLg5oRdECFCwGiByGfwXmmAiGfAxEDDECpb/GCEnp+BWF+pPoRXFCkdpmDGYEBoIoQlmoQmFqmYrqpxMkAPsCSNAGWEFp8BJEAVnSpYqqpYdqhtGqryOqODBB2opGFglmovloQCkABo/WhY3qi1iqiIaqtHhqccMAANDcafUoGxRitBqEAB8ChF8GpBiEDDJB75uqrGPAAKcQR25oQFrAGwRqvvjoHBRBRJwEHEMAAa8qv0doGFlAA9ToSFLAGRWiw/HoBDXAA7dgRGCAAxgmxGhsAFKAHB/B4GZEADrAGXrmxBvsGFOAAGaA/F+FWBMAAJWuyG9sDDSAABV5gALX0EBdgARCwBg4gHTIbtATBpXpAAGmQARZgAAMAIQPhBnMAUQ/QAAzAAFFgAAMotFgrEJP6ABBAAAQgAGArAGowtQ6ABwpQsFmbtgmhI3PQtnHgpWobt3IrswEBADs=" />

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

在线图片转换BASE64工具

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

以上是 python将图片文件转换成base64编码的方法 的全部内容, 来源链接: utcz.com/z/359560.html

回到顶部