11import asyncio
22from playwright .async_api import async_playwright
3- import numpy as np
3+
44from io import BytesIO
5- from PIL import Image
5+ from PIL import Image , ImageGrab
66import os
77
8- async def take_screenshot (url :str , image_name :str = "image.jpeg" , quality :int = 100 ):
8+
9+ async def take_screenshot (url :str , image_name :str = None , quality :int = 100 ):
910 async with async_playwright () as p :
1011 browser = await p .chromium .launch (headless = True )
1112 page = await browser .new_page ()
1213 await page .goto (url )
13- dir_path = os .path .dirname (os .path .realpath (__file__ ))
14- save_path = dir_path + "/saved_screenshots/" + image_name
14+ save_path = None
15+ if image_name is not None :
16+ dir_path = os .path .abspath ('' )
17+ save_path = dir_path + "/scrapegraphai/sreenshot_scraping/saved_screenshots/" + image_name
1518 image_bytes = await page .screenshot (path = save_path ,type = "jpeg" ,full_page = True ,quality = quality )
1619 await browser .close ()
17- return np .array (Image .open (BytesIO (image_bytes )))
20+ return Image .open (BytesIO (image_bytes ))
21+
22+
23+ def select_area_with_opencv (image ):
24+ import cv2 as cv
25+ import numpy as np
26+
27+ fullscreen_screenshot = ImageGrab .grab ()
28+ dw , dh = fullscreen_screenshot .size
29+
30+ def draw_selection_rectanlge (event , x , y , flags , param ):
31+ global ix ,iy ,drawing ,overlay ,img
32+ if event == cv .EVENT_LBUTTONDOWN :
33+ drawing = True
34+ ix ,iy = x ,y
35+ elif event == cv .EVENT_MOUSEMOVE :
36+ if drawing == True :
37+ cv .rectangle (img , (ix , iy ), (x , y ), (41 , 215 , 162 ), - 1 )
38+ cv .putText (img , 'PRESS ANY KEY TO SELECT THIS AREA' , (ix , iy - 10 ), cv .FONT_HERSHEY_SIMPLEX , 1.5 , (55 ,46 ,252 ), 5 )
39+ img = cv .addWeighted (overlay , alpha , img , 1 - alpha , 0 )
40+ elif event == cv .EVENT_LBUTTONUP :
41+ global LEFT ,TOP ,RIGHT ,BOTTOM
42+
43+ drawing = False
44+ if ix < x :
45+ LEFT = int (ix )
46+ RIGHT = int (x )
47+ else :
48+ LEFT = int (x )
49+ RIGHT = int (ix )
50+ if iy < y :
51+ TOP = int (iy )
52+ BOTTOM = int (y )
53+ else :
54+ TOP = int (y )
55+ BOTTOM = int (iy )
56+
57+ global drawing ,ix ,iy ,overlay ,img
58+ drawing = False
59+ ix ,iy = - 1 ,- 1
60+
61+ img = np .array (image )
62+ img = cv .cvtColor (img , cv .COLOR_RGB2BGR )
63+
64+ img = cv .rectangle (img , (0 , 0 ), (image .size [0 ], image .size [1 ]), (0 ,0 ,255 ), 10 )
65+ img = cv .putText (img , 'SELECT AN AREA' , (int (image .size [0 ]* 0.3 ), 100 ), cv .FONT_HERSHEY_SIMPLEX , 2 , (0 ,0 ,255 ), 5 )
66+
67+ overlay = img .copy ()
68+ alpha = 0.3
69+
70+ while True :
71+ cv .namedWindow ('SELECT AREA' , cv .WINDOW_KEEPRATIO )
72+ cv .setMouseCallback ('SELECT AREA' , draw_selection_rectanlge )
73+ cv .resizeWindow ('SELECT AREA' , int (image .size [0 ]/ (image .size [1 ]/ dh )), dh )
74+
75+ cv .imshow ('SELECT AREA' ,img )
76+
77+ if cv .waitKey (20 ) > - 1 :
78+ break
79+
80+ cv .destroyAllWindows ()
81+ return LEFT , TOP , RIGHT , BOTTOM
82+
83+ def select_area_with_ipywidget (image ):
84+ import matplotlib .pyplot as plt
85+ import numpy as np
86+ from ipywidgets import interact , IntSlider
87+ import ipywidgets as widgets
88+ from PIL import Image
89+
90+
91+ # Load an image
92+ # image_path = 'piping-yes-when-running-scripts-from-curl.jpeg' # Replace with your image path
93+ # image = Image.open(image_path)
94+ img_array = np .array (image )
95+
96+ print (img_array .shape )
97+
98+ def update_plot (top_bottom ,left_right ,image_size ):
99+ plt .figure (figsize = (image_size ,image_size ))
100+ plt .imshow (img_array )
101+ plt .axvline (x = left_right [0 ], color = 'blue' , linewidth = 1 )
102+ plt .text (left_right [0 ]+ 1 ,- 25 ,'LEFT' ,rotation = 90 ,color = 'blue' )
103+ plt .axvline (x = left_right [1 ], color = 'red' , linewidth = 1 )
104+ plt .text (left_right [1 ]+ 1 ,- 25 ,'RIGHT' ,rotation = 90 , color = 'red' )
105+
106+
107+ plt .axhline (y = img_array .shape [0 ]- top_bottom [0 ], color = 'green' , linewidth = 1 )
108+ plt .text (- 100 ,img_array .shape [0 ]- top_bottom [0 ]+ 1 ,'BOTTOM' , color = 'green' )
109+ plt .axhline (y = img_array .shape [0 ]- top_bottom [1 ], color = 'darkorange' , linewidth = 1 )
110+ plt .text (- 100 ,img_array .shape [0 ]- top_bottom [1 ]+ 1 ,'TOP' , color = 'darkorange' )
111+ plt .axis ('off' ) # Hide axes
112+ plt .show ()
113+
114+
115+ top_bottom_slider = widgets .IntRangeSlider (
116+ value = [int (img_array .shape [0 ]* 0.25 ), int (img_array .shape [0 ]* 0.75 )],
117+ min = 0 ,
118+ max = img_array .shape [0 ],
119+ step = 1 ,
120+ description = 'top_bottom:' ,
121+ disabled = False ,
122+ continuous_update = True ,
123+ orientation = 'vertical' ,
124+ readout = True ,
125+ readout_format = 'd' ,
126+ )
18127
19- # image_array=asyncio.run(take_screenshot("https://unix.stackexchange.com/questions/690233/piping-yes-when-running-scripts-from-curl"))
20- # print(image_array.shape)
128+ left_right_slider = widgets .IntRangeSlider (
129+ value = [int (img_array .shape [1 ]* 0.25 ), int (img_array .shape [1 ]* 0.75 )],
130+ min = 0 ,
131+ max = img_array .shape [1 ],
132+ step = 1 ,
133+ description = 'left_right:' ,
134+ disabled = False ,
135+ continuous_update = True ,
136+ orientation = 'horizontal' ,
137+ readout = True ,
138+ readout_format = 'd' ,
139+ )
140+ image_size_bt = widgets .BoundedIntText (
141+ value = 10 ,
142+ min = 2 ,
143+ max = 20 ,
144+ step = 1 ,
145+ description = 'Image size:' ,
146+ disabled = False
147+ )
21148
22- def select_area (image_array ):
23- pass
149+ interact (update_plot ,top_bottom = top_bottom_slider ,left_right = left_right_slider ,image_size = image_size_bt )
24150
25- def crop_image (image ,TOP , BOTTOM , LEFT , RIGHT ):
26- return image [ BOTTOM : TOP , LEFT : RIGHT , :]
151+ def crop_image (image ,TOP = None , BOTTOM = None , LEFT = None , RIGHT = None , save : bool = True ):
152+ return image . crop (( LEFT , TOP , RIGHT , BOTTOM ))
27153
28154
155+ # image=asyncio.run(take_screenshot("https://unix.stackexchange.com/questions/690233/piping-yes-when-running-scripts-from-curl"))
156+ # print(select_area_with_opencv(image))
0 commit comments