Question about python and base64 encoding

Hi everyone, I have a question. I was writing the code, and it was supposed to get a link to the image. But this link was, as I understood, encoded in base64, because when I output it to the console, it was written 'data:image/png; base64,' and after the comma there was an encoded link. Well, I decided to decode,everything seemed to go fine, but when I decoded from base64, and after that I started decoding from UTF-8, the error 'Like UTF-8 can't decode the first character'came out. Well I I decided to output a link to the console, after decoding from base64, and oh my God, I thought there would be a regular link to the image, but there was something like: '\x89PNG\r\n\x1a\n\x00\x00'(and it was sooo big). What's it? Also some kind of encoding? What should I do?

from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options 
import base64

def abstract(text):
    chrome_options = Options()  
    chrome_options.add_argument("--headless")
    
    driver = webdriver.Chrome(chrome_options=chrome_options)
    driver.get('https://coo.by/writer_new')
    
    content = driver.find_element_by_css_selector('#text')
    content.send_keys(text)
    
    seal = driver.find_element_by_css_selector('#write')
    seal.click()
    sleep(5)
    
    image = driver.find_element_by_id('list_img').get_attribute('src')
    print(image)
    image_x = image.split(',')[1]
    print(image_x)
    a = image_x.encode('UTF-8')
    b = base64.b64decode(a)
    print(b)
    png_recovered = b.decode('UTF-8')

    return png_recovered 

image = abstract('Andrei')
print(image)
Author: Pustovoy Andrei, 2020-06-20

1 answers

What you got (b'\x89PNG\r\n\x1a\n\x00\x00') is already an image in bytes. So just open the png file (as you can see from the signature) to write bytes and write the result you received there

with open("image.png", "wb") as fp:
    fp.write(b)
 1
Author: Михаил Муругов, 2020-06-20 07:53:00