How to get image size in async function?

Viewed 123

I want to get the size of the image from a buffer.

import io
from PIL import Image

class Scraper:
   asnyc _save_image(res):
      buffer = await res.body()

      img = Image.open(io.BytesIO(buffer))
      img_w, img_h = img.size


   async def scrape():
      playwright = await async_playwright().start()
      browser = await playwright.chromium.launch( headless = True, devtools = False )
      page = browser.new_page()

      page.on('response', _save_image)
      await page.goto('https://www.example.com')

scraper = Scraper(key, id)
asyncio.run(scraper.scrape())

img = Image.open(io.BytesIO(buffer))

This code above is not an async function. I want know the size of images from the buffer asynchronously. How to so this?

0 Answers
Related