soiz commited on
Commit
93d4d7b
·
verified ·
1 Parent(s): 6c1d814

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -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(width, height) # Adjust the window size here
39
  wd.get(url)
40
  wd.implicitly_wait(10)
41
  screenshot = wd.get_screenshot_as_png()
42
- except WebDriverException as e:
43
- # If there's an error, return a blank image as a placeholder
44
  return Image.new('RGB', (1, 1))
45
  finally:
46
  if wd:
@@ -54,16 +54,34 @@ def screenshot():
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
- # Take the screenshot of the provided URL with specified width and height
62
- image = take_screenshot(url, width=screenw, height=screenh)
63
 
64
- # Save the screenshot to an in-memory file and return as response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  img_io = BytesIO()
66
- image.save(img_io, 'PNG')
67
  img_io.seek(0)
68
 
69
  return send_file(img_io, mimetype='image/png')
 
35
 
36
  try:
37
  wd = webdriver.Chrome(options=options)
38
+ wd.set_window_size(width, height)
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
  if not url:
55
  return "URL parameter is required.", 400
56
 
57
+ # パラメーターを取得
58
+ screenw = request.args.get('screenw', default=None, type=int)
59
+ screenh = request.args.get('screenh', default=None, type=int)
60
 
61
+ # スクリーンショットを撮影
62
+ original_image = take_screenshot(url)
63
 
64
+ # リサイズ処理
65
+ if screenw and screenh:
66
+ # 両方のパラメーターが指定されている場合
67
+ resized_image = original_image.resize((screenw, screenh))
68
+ elif screenw:
69
+ # width のみが指定されている場合、高さを比率に基づいて自動計算
70
+ aspect_ratio = original_image.height / original_image.width
71
+ resized_height = int(screenw * aspect_ratio)
72
+ resized_image = original_image.resize((screenw, resized_height))
73
+ elif screenh:
74
+ # height のみが指定されている場合、幅を比率に基づいて自動計算
75
+ aspect_ratio = original_image.width / original_image.height
76
+ resized_width = int(screenh * aspect_ratio)
77
+ resized_image = original_image.resize((resized_width, screenh))
78
+ else:
79
+ # 両方のパラメーターが指定されていない場合
80
+ resized_image = original_image
81
+
82
+ # 画像をメモリ上に保存してレスポンスとして返す
83
  img_io = BytesIO()
84
+ resized_image.save(img_io, 'PNG')
85
  img_io.seek(0)
86
 
87
  return send_file(img_io, mimetype='image/png')