python将图片文件转换成base64编码
在网页里调用图片除了可以直接给定图片路径外,还可以使用base64编码,下面的python代码可以将图片转换成base64编码
import base64 f=open(r'c:\sharejs.jpg','rb') #二进制方式打开图文件 ls_f=base64.b64encode(f.read()) #读取文件内容,转换为base64编码 f.close()
调用方法
<img src="data:image/bmp;base64,Qk0eAgAAAAAAALYAAAAoAAAAEgAAABIAAAABAAgAAAAAAAAAAA DEDgAAxA4AACAAAAAgAAAAFSph/ySn4f8jRGP/mt70/zg1M/9DqMr/YWJg/yZqtf8EAwP/Xouz/5O qpP81Vn7/O4ut/xQqQP8TUJL/TmaU/y9ylv8nWKH/g3Z1/x3d9v+JvNj/LCYk/yQcGf8Nb7T/HUKV /w0bJ/8XW7L/YNT7/7b1/P+Niof/I43F/////wAfFhYVFhUWFhYVFRUVFhUWFR8AABYCBwcYARgJC wQNDQICDQ0NBAAABAIaEQcHCwsNDQICBA0NDQIEAAAEAgcHHg4CCw4eFAIPAg0NAgQAAAQLBwceDg sQEQMJHh4GDQ0CBAAAFQsaGgcRDwULCw8UCQ0CDQsEAAAEAAcREQ4LCQEXDgAAAQIZEAQAABUCGAc RGAAJAxsbDAsKAhkQBAAAFQwYGAcHAAMDAxQUFAodDQIVAAAWHgEYGB4YAwMDHBwDCgYNDRUAABUQ AQEYBx4RFBQCFA8CBAACFQAABAgeAR4RAREZAAAJFA4ADQsVAAAECBkBAR4FHhEFHBwUCgoCAhUAA BUICB4eHgEHHBwcHAMUCgICFQAAFQgIDQEeEwEPAwMUCQkAAgwWAAACCAgIAQETExoAGhcXHg4TGR UAABUGFggZDAUFBQwMEBAMBQ8EBgAAHwQKEhISEhISEhIdEhISHRIfAAA=" />