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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -27
app.py CHANGED
@@ -27,7 +27,7 @@ download_noto_sans_cjk()
27
 
28
  app = Flask(__name__)
29
 
30
- def take_screenshot(url, width=1080, height=720):
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(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,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=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')
 
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')