MakiAi commited on
Commit
1e6a621
·
verified ·
1 Parent(s): ec6d93e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+ import numpy as np
4
+ from PIL import Image
5
+ import os
6
+
7
+ def save_image(im, filename="saved_image.png"):
8
+ """画像をファイルに保存する関数"""
9
+ if im is not None and "composite" in im:
10
+ # 合成画像(最終結果)を取得
11
+ composite = im["composite"]
12
+
13
+ # numpy配列の場合はPIL Imageに変換
14
+ if isinstance(composite, np.ndarray):
15
+ composite = Image.fromarray(composite)
16
+
17
+ # 画像を保存
18
+ composite.save(filename)
19
+ return f"画像が正常に {filename} として保存されました"
20
+ else:
21
+ return "保存する画像がないか、無効な画像形式です"
22
+
23
+ # Gradio Blocksアプリケーションを作成(テーマをOceanに設定)
24
+ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
25
+ gr.Markdown("# Image Editor Demo")
26
+
27
+ with gr.Row():
28
+ with gr.Column():
29
+ # 1:1の縦横比でImageEditorコンポーネントを作成
30
+ im = gr.ImageEditor(
31
+ type="numpy", # 画像はnumpy配列として処理される
32
+ crop_size="1:1", # 正方形のトリミング比率を設定
33
+ label="画像エディタ"
34
+ )
35
+
36
+ with gr.Column():
37
+ # プレビュー表示用のImageコンポーネントを作成
38
+ im_preview = gr.Image(label="プレビュー")
39
+
40
+ # 各種イベントを追跡するカウンター
41
+ with gr.Row():
42
+ n_upload = gr.Number(0, label="アップロードイベント数", step=1)
43
+ n_change = gr.Number(0, label="変更イベント数", step=1)
44
+ n_input = gr.Number(0, label="入力イベント数", step=1)
45
+
46
+ # ファイル名入力と保存ボタンを追加
47
+ with gr.Row():
48
+ filename_input = gr.Textbox(value="my_drawing.png", label="ファイル名")
49
+ save_btn = gr.Button("画像を保存", variant="primary")
50
+
51
+ # 保存操作のステータスメッセージ
52
+ save_status = gr.Textbox(label="保存ステータス", interactive=False)
53
+
54
+ # イベントハンドラーを定義:
55
+ # 画像がアップロードされたらアップロードカウンターを増加
56
+ im.upload(lambda x: x + 1, outputs=n_upload, inputs=n_upload)
57
+
58
+ # 画像が変更されたら変更カウンターを増加(ユーザー入力または関数更新による)
59
+ im.change(lambda x: x + 1, outputs=n_change, inputs=n_change)
60
+
61
+ # ユーザーが画像を変更したら入力カウンターを増加
62
+ im.input(lambda x: x + 1, outputs=n_input, inputs=n_input)
63
+
64
+ # 画像が変更されたらプレビューエリアに合成画像を表示
65
+ im.change(lambda im: im["composite"] if im and "composite" in im else None,
66
+ outputs=im_preview,
67
+ inputs=im,
68
+ show_progress="hidden")
69
+
70
+ # 保存ボタンクリックイベントを設定
71
+ save_btn.click(
72
+ fn=save_image,
73
+ inputs=[im, filename_input],
74
+ outputs=save_status
75
+ )
76
+
77
+ # このスクリプトが直接実行されたときにデモを起動
78
+ if __name__ == "__main__":
79
+ demo.launch()