Search code examples
pythonpython-3.xiopython-imaging-libraryplaywright

bytesio type abnormal Two pictures saved in the same way are of different types : <class '_io.BytesIO'> and <class 'bytes'>


I found a strange thing. When I successively saved the pictures into bytesio (), their types were completely different. This makes the image processing behind me impossible (for some special reasons, I can't save the image as root / a.png and so on).

import asyncio
from playwright.async_api import async_playwright
import time
from io import BytesIO 

async def main():
    async with async_playwright() as p:
        browser =await p.chromium.launch(headless=False)
        context = await browser.new_context(ignore_https_errors = True)
        page = await context.new_page()
        
        await page.goto("https://stackoverflow.com/")

        screen_first = BytesIO()
        screen_second = BytesIO()

        time.sleep(1)
        screen_first.write(await page.screenshot(full_page=True, type="png"))

        await page.fill(f'input >> nth=0','test')

        time.sleep(1)
        screen_second = await page.screenshot(full_page=True, type="png")

        print(id(screen_first))  #1594966975968    
        print(type(screen_first)) #<class '_io.BytesIO'>
        #print(screen_first)
        
        print(id(screen_second)) #1594944534144
        print(type(screen_second)) #<class 'bytes'> 
        #print(screen_second)
        time.sleep(5)

asyncio.run(main())

I tried the Google solution, but they all asked for the specific file path. I searched for IO keywords in stack overflow and found no similar problems. Thank you for your answers


Solution

  • They are saved in the same way, but in the second case you are overwriting the variable with return value of .screenshot method, which seems to be of the type bytes