Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -27,7 +27,7 @@ download_noto_sans_cjk()
|
|
27 |
|
28 |
app = Flask(__name__)
|
29 |
|
30 |
-
def take_screenshot(url,
|
31 |
options = webdriver.ChromeOptions()
|
32 |
options.add_argument('--headless')
|
33 |
options.add_argument('--no-sandbox')
|
@@ -35,12 +35,12 @@ def take_screenshot(url, width=1080, height=720):
|
|
35 |
|
36 |
try:
|
37 |
wd = webdriver.Chrome(options=options)
|
38 |
-
wd.set_window_size(
|
39 |
wd.get(url)
|
40 |
wd.implicitly_wait(10)
|
41 |
screenshot = wd.get_screenshot_as_png()
|
42 |
except WebDriverException:
|
43 |
-
#
|
44 |
return Image.new('RGB', (1, 1))
|
45 |
finally:
|
46 |
if wd:
|
@@ -54,34 +54,35 @@ def screenshot():
|
|
54 |
if not url:
|
55 |
return "URL parameter is required.", 400
|
56 |
|
57 |
-
#
|
58 |
-
screenw = request.args.get('screenw', default=
|
59 |
-
screenh = request.args.get('screenh', default=
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
#
|
62 |
-
|
63 |
|
64 |
# リサイズ処理
|
65 |
-
if
|
66 |
-
#
|
67 |
-
|
68 |
-
elif
|
69 |
-
# width
|
70 |
-
aspect_ratio =
|
71 |
-
|
72 |
-
|
73 |
-
elif
|
74 |
-
# height
|
75 |
-
aspect_ratio =
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
resized_image = original_image
|
81 |
-
|
82 |
-
# 画像をメモリ上に保存してレスポンスとして返す
|
83 |
img_io = BytesIO()
|
84 |
-
|
85 |
img_io.seek(0)
|
86 |
|
87 |
return send_file(img_io, mimetype='image/png')
|
|
|
27 |
|
28 |
app = Flask(__name__)
|
29 |
|
30 |
+
def take_screenshot(url, screenw=1080, screenh=720):
|
31 |
options = webdriver.ChromeOptions()
|
32 |
options.add_argument('--headless')
|
33 |
options.add_argument('--no-sandbox')
|
|
|
35 |
|
36 |
try:
|
37 |
wd = webdriver.Chrome(options=options)
|
38 |
+
wd.set_window_size(screenw, screenh) # Set window size based on screenw and screenh
|
39 |
wd.get(url)
|
40 |
wd.implicitly_wait(10)
|
41 |
screenshot = wd.get_screenshot_as_png()
|
42 |
except WebDriverException:
|
43 |
+
# Return a blank image as a placeholder if there's an error
|
44 |
return Image.new('RGB', (1, 1))
|
45 |
finally:
|
46 |
if wd:
|
|
|
54 |
if not url:
|
55 |
return "URL parameter is required.", 400
|
56 |
|
57 |
+
# ウィンドウサイズのパラメーターを取得
|
58 |
+
screenw = request.args.get('screenw', default=1080, type=int)
|
59 |
+
screenh = request.args.get('screenh', default=720, type=int)
|
60 |
+
|
61 |
+
# 画像リサイズのパラメーターを取得
|
62 |
+
width = request.args.get('width', type=int)
|
63 |
+
height = request.args.get('height', type=int)
|
64 |
|
65 |
+
# スクリーンショットを取得
|
66 |
+
image = take_screenshot(url, screenw=screenw, screenh=screenh)
|
67 |
|
68 |
# リサイズ処理
|
69 |
+
if width and height:
|
70 |
+
# 両方のパラメーターが提供された場合
|
71 |
+
image = image.resize((width, height), Image.ANTIALIAS)
|
72 |
+
elif width:
|
73 |
+
# width のみが提供された場合、アスペクト比に基づいて height を計算
|
74 |
+
aspect_ratio = image.height / image.width
|
75 |
+
height = int(width * aspect_ratio)
|
76 |
+
image = image.resize((width, height), Image.ANTIALIAS)
|
77 |
+
elif height:
|
78 |
+
# height のみが提供された場合、アスペクト比に基づいて width を計算
|
79 |
+
aspect_ratio = image.width / image.height
|
80 |
+
width = int(height * aspect_ratio)
|
81 |
+
image = image.resize((width, height), Image.ANTIALIAS)
|
82 |
+
|
83 |
+
# 結果画像をメモリに保存してレスポンスとして返却
|
|
|
|
|
|
|
84 |
img_io = BytesIO()
|
85 |
+
image.save(img_io, 'PNG')
|
86 |
img_io.seek(0)
|
87 |
|
88 |
return send_file(img_io, mimetype='image/png')
|