複数画像対応に更新

#4
by Golf8 - opened
Files changed (1) hide show
  1. app.py +70 -28
app.py CHANGED
@@ -1,33 +1,75 @@
1
  import gradio as gr
 
 
 
2
 
3
- from infer import infer_image, infer_video
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- input_image = gr.Image(type='pil', label='Input Image')
6
- input_model_image = gr.Radio([('x2', 2), ('x4', 4), ('x8', 8)], type="value", value=4, label="Model Upscale/Enhance Type")
7
- submit_image_button = gr.Button('Submit')
8
- output_image = gr.Image(type="filepath", label="Output Image")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- tab_img = gr.Interface(
11
- fn=infer_image,
12
- inputs=[input_image, input_model_image],
13
- outputs=output_image,
14
- title="Real-ESRGAN Pytorch",
15
- description="Gradio UI for Real-ESRGAN Pytorch version. To use it, simply upload your image and choose the model. Read more at the links below. Please click submit only once <br>Credits: [Nick088](https://linktr.ee/Nick088), Xinntao, Tencent, Geeve George, ai-forever, daroche <br><p style='text-align: center'><a href='https://github.com/Nick088/Real-ESRGAN_Pytorch'>Github Repo</a></p>"
16
- )
17
 
18
- input_video = gr.Video(label='Input Video')
19
- input_model_video = gr.Radio([('x2', 2), ('x4', 4), ('x8', 8)], type="value", value=2, label="Model Upscale/Enhance Type")
20
- submit_video_button = gr.Button('Submit')
21
- output_video = gr.Video(label='Output Video')
22
-
23
- tab_vid = gr.Interface(
24
- fn=infer_video,
25
- inputs=[input_video, input_model_video],
26
- outputs=output_video,
27
- title="Real-ESRGAN Pytorch",
28
- description="Gradio UI for Real-ESRGAN Pytorch version. To use it, simply upload your video and choose the model. Read more at the links below. Please click submit only once <br>Credits: [Nick088](https://linktr.ee/Nick088), Xinntao, Tencent, Geeve George, ai-forever, daroche <br><p style='text-align: center'><a href='https://arxiv.org/abs/2107.10833'>Real-ESRGAN: Training Real-World Blind Super-Resolution with Pure Synthetic Data</a> | <a href='https://github.com/ai-forever/Real-ESRGAN'>Github Repo</a></p>"
29
- )
30
-
31
- demo = gr.TabbedInterface([tab_img, tab_vid], ["Image", "Video"])
32
-
33
- demo.launch(debug=True, show_error=True, share=True)
 
1
  import gradio as gr
2
+ from PIL import Image, ImageEnhance, ImageFilter
3
+ import io
4
+ import zipfile
5
 
6
+ def batch_upscale(images, scale, enhance):
7
+ if not images:
8
+ return None, None, "画像をアップロードしてください"
9
+
10
+ processed_images = []
11
+ results_info = []
12
+
13
+ for i, img in enumerate(images):
14
+ # 高品質アップスケール
15
+ width, height = img.size
16
+ new_width = int(width * scale)
17
+ new_height = int(height * scale)
18
+
19
+ upscaled = img.resize((new_width, new_height), Image.LANCZOS)
20
+
21
+ # 追加補正
22
+ if enhance:
23
+ upscaled = upscaled.filter(ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3))
24
+ enhancer = ImageEnhance.Contrast(upscaled)
25
+ upscaled = enhancer.enhance(1.15)
26
+ enhancer = ImageEnhance.Sharpness(upscaled)
27
+ upscaled = enhancer.enhance(1.35)
28
+
29
+ processed_images.append(upscaled)
30
+
31
+ # サイズ情報
32
+ buf = io.BytesIO()
33
+ upscaled.save(buf, format="PNG", quality=95)
34
+ size_kb = len(buf.getvalue()) / 1024
35
+ results_info.append(f"画像{i+1}: {size_kb:.1f} KB")
36
+
37
+ # ZIPファイル作成
38
+ zip_buffer = io.BytesIO()
39
+ with zipfile.ZipFile(zip_buffer, "w") as zip_file:
40
+ for i, img in enumerate(processed_images):
41
+ img_byte = io.BytesIO()
42
+ img.save(img_byte, format="PNG", quality=95)
43
+ zip_file.writestr(f"upscaled_{i+1}.png", img_byte.getvalue())
44
+
45
+ return processed_images, zip_buffer.getvalue(), "\n".join(results_info)
46
 
47
+ with gr.Blocks(title="Kazの高画質化アプリ") as demo:
48
+ gr.Markdown("# 🖼️ KazのAI画像高画質化アプリ\n**複数画像一括処理版**")
49
+
50
+ with gr.Row():
51
+ with gr.Column():
52
+ input_gallery = gr.Gallery(
53
+ label="📸 複数の画像をドラッグ&ドロップ(一括処理)",
54
+ height=500,
55
+ object_fit="contain"
56
+ )
57
+ scale = gr.Radio([2, 4, 8], value=4, label="🔍 拡大倍率(8xは時間がかかります)")
58
+ enhance = gr.Checkbox(label="シャープ・コントラスト強化", value=True)
59
+ btn = gr.Button("🚀 全画像を高画質化", variant="primary", size="large")
60
+
61
+ with gr.Column():
62
+ output_gallery = gr.Gallery(label="✨ 処理結果", height=500)
63
+ output_zip = gr.File(label="📦 全画像をZIPでダウンロード")
64
+
65
+ status = gr.Textbox(label="処理結果", lines=5)
66
+
67
+ btn.click(
68
+ fn=batch_upscale,
69
+ inputs=[input_gallery, scale, enhance],
70
+ outputs=[output_gallery, output_zip, status]
71
+ )
72
 
73
+ gr.Markdown("### Tips\n・複数画像を同時にアップロードできます\n・8x + 強化ONでかなり高画質になります\n・処理後、ZIPをダウンロードしてください")
 
 
 
 
 
 
74
 
75
+ demo.launch()