tori29umai commited on
Commit
6b2e918
·
verified ·
1 Parent(s): d5da9f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -97
app.py CHANGED
@@ -1,103 +1,65 @@
1
  import gradio as gr
2
- from PIL import Image, ImageOps
3
  import os
4
- import zipfile
 
5
 
 
 
 
 
 
 
 
6
 
7
- def save_image(image, path):
8
- # 画像の保存 (PNG形式で保存)
9
- image.save(path, format='PNG')
 
 
 
 
 
 
 
 
 
10
 
11
- def process_image(image):
12
- # 元の画像のサイズ
13
- original_width, original_height = image.size
14
-
15
- # 新しいサイズ
16
- target_width, target_height = 252, 144
17
-
18
- # 分割後の画像を格納するリスト
19
- outputs = []
20
-
21
- # 画像を4つのセクションに分割するための新しいサイズ
22
- new_width = original_width // 2
23
- new_height = original_height // 2
24
-
25
- # 画像を4つのセクションに分割
26
- for i in range(2):
27
- for j in range(2):
28
- left = new_width * j
29
- upper = new_height * i
30
- right = new_width * (j + 1)
31
- lower = new_height * (i + 1)
32
-
33
- # 画像を切り取る
34
- cropped_image = image.crop((left, upper, right, lower))
35
-
36
- # アスペクト比を保ちながら、リサイズ
37
- cropped_image.thumbnail((target_width, target_height), Image.Resampling.LANCZOS)
38
-
39
- # 新しい黒背景を作成
40
- black_background = Image.new('RGBA', (target_width, target_height), (0, 0, 0, 255))
41
-
42
- # 余白の追加を左上、左下、右上、右下に対応
43
- if i == 0 and j == 0: # 左上
44
- left_offset = (target_width - cropped_image.width) # 右側に余白
45
- top_offset = (target_height - cropped_image.height) # 下側に余白
46
- elif i == 1 and j == 0: # 左下
47
- left_offset = (target_width - cropped_image.width) # 右側に余白
48
- top_offset = 0 # 上側に余白
49
- elif i == 0 and j == 1: # 右上
50
- left_offset = 0 # 左側に余白
51
- top_offset = (target_height - cropped_image.height) # 下側に余白
52
- elif i == 1 and j == 1: # 右下
53
- left_offset = 0 # 左側に余白
54
- top_offset = 0 # 上側に余白
55
 
56
- # 中心に元画像を貼り付け
57
- black_background.paste(cropped_image, (left_offset, top_offset))
58
-
59
- outputs.append(black_background)
60
-
61
- # 出力される4つの画像を返す
62
- return outputs[0], outputs[1], outputs[2], outputs[3]
63
 
64
- def zip_files(zip_files, zip_path):
65
- with zipfile.ZipFile(zip_path, 'w') as zipf:
66
- for file_path in zip_files:
67
- zipf.write(file_path, arcname=os.path.basename(file_path))
68
 
69
- class webui:
70
- def __init__(self):
71
- self.demo = gr.Blocks()
 
72
 
73
- def main(self, image_path):
74
- image = Image.open(image_path)
75
- # 拡張子を取り除いたファイル名を取得
76
- image_name = os.path.splitext(image_path)[0]
77
 
78
- # 画像を処理
79
- output1, output2, output3, output4 = process_image(image)
 
80
 
81
- # 保存先のパスを作成
82
- output1_path = f"{image_name}_1.png"
83
- output2_path = f"{image_name}_2.png"
84
- output3_path = f"{image_name}_3.png"
85
- output4_path = f"{image_name}_4.png"
86
-
87
- # 画像を保存(PNG形式で保存)
88
- save_image(output1, output1_path)
89
- save_image(output2, output2_path)
90
- save_image(output3, output3_path)
91
- save_image(output4, output4_path)
92
 
93
- # 保存したファイルをリストに追加
94
- zip_files_list = [output1_path, output2_path, output3_path, output4_path]
95
- zip_path = f"{image_name}.zip"
96
- zip_files(zip_files_list, zip_path)
97
-
98
- # 出力として画像とzipファイルを返す
99
- outputs = [output1, output2, output3, output4]
100
- return outputs, zip_path
101
 
102
  def launch(self, share):
103
  with self.demo:
@@ -107,18 +69,13 @@ class webui:
107
  submit = gr.Button(value="Start")
108
  with gr.Row():
109
  with gr.Column():
110
- with gr.Tab("output"):
111
- output_0 = gr.Gallery(format="png")
112
- output_file = gr.File()
113
- submit.click(
114
- self.main,
115
- inputs=[input_image],
116
- outputs=[output_0, output_file]
117
- )
118
 
119
  self.demo.queue()
120
  self.demo.launch(share=share)
121
 
122
  if __name__ == "__main__":
123
- ui = webui()
124
  ui.launch(share=True)
 
1
  import gradio as gr
2
+ from PIL import Image
3
  import os
4
+ import cv2
5
+ import numpy as np
6
 
7
+ def process_image(image_path):
8
+ # 画像を読み込む
9
+ image = Image.open(image_path)
10
+
11
+ # 最大解像度を設定(1200x1900)
12
+ max_width = 1900
13
+ max_height = 1200
14
 
15
+ # アスペクト比を保ちながらリサイズ
16
+ width, height = image.size
17
+ if width > max_width or height > max_height:
18
+ # アスペクト比を保ったままリサイズ
19
+ aspect_ratio = width / height
20
+ if width > max_width:
21
+ width = max_width
22
+ height = int(width / aspect_ratio)
23
+ if height > max_height:
24
+ height = max_height
25
+ width = int(height * aspect_ratio)
26
+ image = image.resize((width, height), Image.ANTIALIAS)
27
 
28
+ # 画像を5秒の動画にするためのフレーム数を決定(例えば30fpsで5秒分なので150フレーム)
29
+ fps = 30
30
+ duration = 5
31
+ total_frames = fps * duration
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ # フレームリストを作成
34
+ frames = []
35
+ for _ in range(total_frames):
36
+ # 同じ画像を繰り返してフレームにする(必要に応じて変化を加えることもできる)
37
+ frame = np.array(image)
38
+ frames.append(frame)
 
39
 
40
+ # 動画保存先のパス
41
+ output_video_path = os.path.splitext(image_path)[0] + '.mp4'
 
 
42
 
43
+ # 動画ファイルにフレームを保存
44
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v') # MP4形式
45
+ height, width, _ = frames[0].shape
46
+ video_writer = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
47
 
48
+ # フレームを動画に書き込む
49
+ for frame in frames:
50
+ video_writer.write(frame)
 
51
 
52
+ video_writer.release()
53
+
54
+ return output_video_path
55
 
56
+ class WebUI:
57
+ def __init__(self):
58
+ self.demo = gr.Blocks()
 
 
 
 
 
 
 
 
59
 
60
+ def main(self, image_path):
61
+ output_video = process_image(image_path)
62
+ return output_video
 
 
 
 
 
63
 
64
  def launch(self, share):
65
  with self.demo:
 
69
  submit = gr.Button(value="Start")
70
  with gr.Row():
71
  with gr.Column():
72
+ with gr.Tab("Output"):
73
+ output_mp4 = gr.Gallery()
74
+ submit.click(self.main, inputs=[input_image], outputs=[output_mp4])
 
 
 
 
 
75
 
76
  self.demo.queue()
77
  self.demo.launch(share=share)
78
 
79
  if __name__ == "__main__":
80
+ ui = WebUI()
81
  ui.launch(share=True)