python base64图片还原(输出matplotlib)

将base64图片还原,最终使用matplotlib输出。在此期间,我们也学会了转换到opencv输出

import cv2
import matplotlib.pyplot as plt
import numpy as np

def captcha():
    img_data = 'data:image/png;base64,xxxx   此处省略。。。'

    # 1、信息提取
    result = re.search("data:image/(?P.*?);base64,(?P.*)", img_data, re.DOTALL)
    if result:
        ext = result.groupdict().get("ext")
        data = result.groupdict().get("data")
    else:
        raise Exception("Do not parse!")

    # 2、base64解码,转换为np数组,转换为opencv输出
    img_bytes = base64.b64decode(data)
    nparr = np.fromstring(img_bytes, np.uint8)
    img = cv2.imdecode(nparr, cv2.COLOR_BGR2RGB)

    # 转换为matplotlib输出
    plt.imshow(img)
    plt.show()

感谢大神博客点拨:https://yuki-ho.blog.csdn.net/article/details/78403904

你可能感兴趣的:(python base64图片还原(输出matplotlib))