如何在python中转换base64图片字符串并将其保存在本地目录中

时间:2019-07-12 09:32:48

标签: python django

如何将包含图像的base64字符串转换为图像格式并将其保存到本地系统。这是我的字符串:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/4QCSRXhpZgAASUkqAAgAAAAEAA4BAgAbAAAAPgAAADEBAgAHAAAAWQAAABICAwACAAAAAQABAGmHBAABAAAAYAAAAAAAAABwcm9mZXNzaW9uYWwgd29tYW4gcGljdHVyZQBHb29nbGUAAwAAkAcABAAAADAyMjACoAQAAQAAAIQDAAADoAQAAQAAAGQCAAAAAAAA/9sAQwAGBAUGBQQGBgUGBwcGCAoQCgoJCQoUDg8MEBcUGBgXFBYWGh0lHxobIxwWFiAsICMmJykqKRkfLTAtKDAlKCko/9sAQwEHBwcKCAoTCgoTKBoWGigoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo/8IAEQgCZAOEAwERAAIRAQMRAf/EAB0AAAEFAQEBAQAAAAAAAAAAAAEAAgMEBQYHCAn/x

我需要将此字符串转换为jpg / png / jpeg格式,并作为图像存储在本地系统中。我已经尝试过this问题,但没有得到解决。在html变量中,我得到了一些字符串格式的文本和图像。这是我的代码:

views.py

html = request.POST.get('html')
soup = BeautifulSoup(html,features="lxml")
html_image = soup.find_all('img')[0].get('src')
b = html_image.encode()
html_title = soup.get_text()
with open("new_image.png", "wb") as new_file:
                new_file.write(base64.decodebytes(b))

0 个答案:

没有答案