zoukankan      html  css  js  c++  java
  • python将base64后的图片字符串重新转为图片

    import base64
    import hashlib
    
    
    def md5_value(content, salt=''):
        """
        
        :param content: 
        :param salt: 
        :return: 
        """
        if salt:
            content = '{}{}'.format(content, salt)
        return hashlib.md5(content.encode()).hexdigest().upper()
    
    
    def convert_base64_src_to_img_file(src=None):
        """
        
        :param src: 
        :return: 
        """
        test_src = 'data:image/gif;base64,R0lGODlhSwBLAMQAAO3t7dvb2+Tk5MjIyLu7u/Hx8fr6+tHR0c3Nzb+/v8TExNbW1t/f3/b29ujo6P///7a2tgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS41LWMwMTQgNzkuMTUxNDgxLCAyMDEzLzAzLzEzLTEyOjA5OjE1ICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ0MgKE1hY2ludG9zaCkiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6MzJDOTQ1Mjk0RTU3MTFFMzgzNTZBMEMyNzU1OUN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IclujgyUcN4rWjBsmAAAT2XiQYUMvBjnKbGpQJEGCA0QG0iAoAoO4Bx1WlHg3c0TQqnak6CFS1ykYmD5Rcx2DssWBkWCpjeySweJZKTiAHwLR1UyTO3Ci4jihgIDfsU70B2Foq4FKaAloOAPRlU+BJgAUD3i6cTLmy5cuYM2vezLmz5xohAAA7'
        if src is None:
            src = test_src
        data = src.split('base64,')[-1]
        print(data)
        mime = src.split('data:image/')[-1].split(';base64')[0]
        image_data = base64.b64decode(data)
    
        file_name = md5_value(data)
        print(file_name)
    
        # file_name = '111'
        file_path = r'D:images{}.{}'.format(file_name, mime)
        with open(file_path, 'wb') as f:
            f.write(image_data)
    
    
    if __name__ == '__main__':
        convert_base64_src_to_img_file()
    

    以上。

  • 相关阅读:
    LDAP安装配置(windows)
    chrome postman插件手动安装
    mabatis insert into on duplicate key
    ZOJ 3641 <并查集+STL>
    ZOJ 3633 <rmq 重点在于转化>
    POJ 2817 状态DP 字符串找最多的重复
    POJ 2771 简单二分图匹配
    POJ 1149 最大流<建图> PIGS
    POJ 3692 二分图最大独立点集
    POJ 2239 简单的二分图求最大匹配
  • 原文地址:https://www.cnblogs.com/lovebkj/p/13758946.html
Copyright © 2011-2022 走看看