fix(ScreenShotScraper): static import of optional dependencies

This commit is contained in:
Federico Aguzzi 2024-09-04 14:56:35 +02:00
parent 16ab1bf3d9
commit 52fe441c5a
3 changed files with 16 additions and 2 deletions

View File

@ -94,7 +94,8 @@ more-browser-options = [
screenshot_scraper = [ screenshot_scraper = [
"surya-ocr>=0.5.0; python_version >= '3.10'", "surya-ocr>=0.5.0; python_version >= '3.10'",
"matplotlib>=3.7.2; python_version >= '3.10'", "matplotlib>=3.7.2; python_version >= '3.10'",
"ipywidgets>=8.1.0; python_version >= '3.10'" "ipywidgets>=8.1.0; python_version >= '3.10'",
"pillow>=10.4.0",
] ]
[build-system] [build-system]

View File

@ -161,6 +161,10 @@ idna==3.7
# via yarl # via yarl
imagesize==1.4.1 imagesize==1.4.1
# via sphinx # via sphinx
importlib-metadata==8.4.0
# via sphinx
importlib-resources==6.4.4
# via matplotlib
iniconfig==2.0.0 iniconfig==2.0.0
# via pytest # via pytest
isort==5.13.2 isort==5.13.2
@ -450,8 +454,10 @@ typing-extensions==4.12.2
# via pydantic # via pydantic
# via pydantic-core # via pydantic-core
# via pyee # via pyee
# via pylint
# via sf-hamilton # via sf-hamilton
# via sqlalchemy # via sqlalchemy
# via starlette
# via streamlit # via streamlit
# via typing-inspect # via typing-inspect
# via uvicorn # via uvicorn
@ -471,3 +477,6 @@ uvicorn==0.30.5
# via burr # via burr
yarl==1.9.4 yarl==1.9.4
# via aiohttp # via aiohttp
zipp==3.20.1
# via importlib-metadata
# via importlib-resources

View File

@ -3,7 +3,6 @@ screenshot_preparation module
""" """
import asyncio import asyncio
from io import BytesIO from io import BytesIO
from PIL import Image, ImageGrab
from playwright.async_api import async_playwright from playwright.async_api import async_playwright
import numpy as np import numpy as np
from io import BytesIO from io import BytesIO
@ -18,6 +17,10 @@ async def take_screenshot(url: str, save_path: str = None, quality: int = 100):
Returns: Returns:
PIL.Image: The screenshot of the webpage as a PIL Image object. PIL.Image: The screenshot of the webpage as a PIL Image object.
""" """
try:
from PIL import Image
except:
raise ImportError("The dependencies for screenshot scraping are not installed. Please install them using `pip install scrapegraphai[screenshot_scraper]`.")
async with async_playwright() as p: async with async_playwright() as p:
browser = await p.chromium.launch(headless=True) browser = await p.chromium.launch(headless=True)
@ -43,6 +46,7 @@ def select_area_with_opencv(image):
try: try:
import cv2 as cv import cv2 as cv
from PIL import ImageGrab
except ImportError: except ImportError:
raise ImportError("The dependencies for screenshot scraping are not installed. Please install them using `pip install scrapegraphai[screenshot_scraper]`.") raise ImportError("The dependencies for screenshot scraping are not installed. Please install them using `pip install scrapegraphai[screenshot_scraper]`.")