Spaces:
Sleeping
Sleeping
new
Browse files- app.py +121 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import datetime
|
| 4 |
+
from zoneinfo import ZoneInfo
|
| 5 |
+
import cv2
|
| 6 |
+
from PIL import Image, ImageDraw, ImageFilter, ImageColor
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def combin_image(input_images, back_color, margin_top, margin_between, margin_side):
|
| 10 |
+
|
| 11 |
+
dt = datetime.datetime.now(ZoneInfo("Asia/Tokyo"))
|
| 12 |
+
|
| 13 |
+
# フォルダ・ファイル名設定
|
| 14 |
+
work_fol = dt.strftime("%Y%m%d%H%M%S")
|
| 15 |
+
combin_file = dt.strftime("%H%M%S") + ".jpg"
|
| 16 |
+
|
| 17 |
+
# フォルダ作成
|
| 18 |
+
os.makedirs(work_fol, exist_ok=True)
|
| 19 |
+
|
| 20 |
+
back_color_rgb = tuple(back_color)
|
| 21 |
+
|
| 22 |
+
for i, img in enumerate(input_images):
|
| 23 |
+
|
| 24 |
+
# 対象画像読み込み
|
| 25 |
+
load_img = Image.open(img)
|
| 26 |
+
|
| 27 |
+
img_width, img_height = load_img.size
|
| 28 |
+
|
| 29 |
+
if i == 0:
|
| 30 |
+
|
| 31 |
+
# 上下マージン用画像生成
|
| 32 |
+
img_margin = Image.new("RGB", (img_width + margin_side * 2, int(margin_between/2) + margin_top), back_color_rgb)
|
| 33 |
+
|
| 34 |
+
img_margin.save(work_fol + "/" + "img_margin.jpg")
|
| 35 |
+
|
| 36 |
+
# 画像を生成して貼付
|
| 37 |
+
img_add_edge = Image.new("RGB", (img_width + margin_side * 2, img_height + margin_between), back_color_rgb)
|
| 38 |
+
|
| 39 |
+
img_add_edge.paste(load_img, (margin_side, int(margin_between/2)))
|
| 40 |
+
|
| 41 |
+
file_name = os.path.basename(img)
|
| 42 |
+
|
| 43 |
+
print(file_name)
|
| 44 |
+
|
| 45 |
+
img_add_edge.save(work_fol + "/" + file_name)
|
| 46 |
+
|
| 47 |
+
im_margin = cv2.imread(work_fol + "/" + "img_margin.jpg")
|
| 48 |
+
|
| 49 |
+
im_v = im_margin
|
| 50 |
+
|
| 51 |
+
for i, img in enumerate(input_images):
|
| 52 |
+
|
| 53 |
+
im = cv2.imread(work_fol + "/" + os.path.basename(img))
|
| 54 |
+
|
| 55 |
+
im_v = cv2.vconcat([im_v, im])
|
| 56 |
+
|
| 57 |
+
im_v = cv2.vconcat([im_v, im_margin])
|
| 58 |
+
|
| 59 |
+
cv2.imwrite(work_fol + "/" + combin_file, im_v)
|
| 60 |
+
|
| 61 |
+
return work_fol + "/" + combin_file
|
| 62 |
+
|
| 63 |
+
def get_rgb(color):
|
| 64 |
+
|
| 65 |
+
if color[0:4] != "rgba":
|
| 66 |
+
|
| 67 |
+
if color[0] != "#":
|
| 68 |
+
|
| 69 |
+
color = "#" + color
|
| 70 |
+
|
| 71 |
+
color_rgb = ImageColor.getrgb(color)
|
| 72 |
+
|
| 73 |
+
else:
|
| 74 |
+
|
| 75 |
+
r = round(float(color.split(",")[0].split("(")[1]))
|
| 76 |
+
g = round(float(color.split(",")[1]))
|
| 77 |
+
b = round(float(color.split(",")[2]))
|
| 78 |
+
|
| 79 |
+
color_rgb = (r, g, b)
|
| 80 |
+
|
| 81 |
+
return color_rgb
|
| 82 |
+
|
| 83 |
+
with gr.Blocks(title="Fliptoon Image Generator", theme=gr.themes.Citrus()) as demo:
|
| 84 |
+
|
| 85 |
+
title = "<center><strong><h2>Fliptoon用画像作成アプリ</h2></strong></center>"
|
| 86 |
+
message = "<center><h3>"
|
| 87 |
+
message += "・画像の横幅のサイズは揃えて下さい。<br>"
|
| 88 |
+
message += "・画像はファイル名の順番に結合します。ファイル名は01_XXXXXX、02_XXXXXX…などにして下さい。<br>"
|
| 89 |
+
message += "</h3></center>"
|
| 90 |
+
|
| 91 |
+
gr.Markdown(title + message)
|
| 92 |
+
|
| 93 |
+
gr.Markdown("") # 余白
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
with gr.Column():
|
| 97 |
+
|
| 98 |
+
files = gr.Files(label="対象画像", file_types=['image'])
|
| 99 |
+
back_color = gr.ColorPicker(label="背景色", value="ffffff", interactive=True)
|
| 100 |
+
|
| 101 |
+
margin_top = gr.Number(label="上下マージン", value=100)
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
|
| 105 |
+
margin_between = gr.Number(label="コマ間マージン", value=200)
|
| 106 |
+
margin_side = gr.Number(label="コマ左右マージン", value=200)
|
| 107 |
+
|
| 108 |
+
back_color_rgb = gr.JSON(visible=False)
|
| 109 |
+
btn = gr.Button(value="画像作成", variant="primary")
|
| 110 |
+
sys_msg = gr.Text(label="システムメッセージ", interactive=False)
|
| 111 |
+
|
| 112 |
+
with gr.Column():
|
| 113 |
+
|
| 114 |
+
out_image = gr.Image(label="結合画像", width=320)
|
| 115 |
+
|
| 116 |
+
btn.click(get_rgb, inputs=[back_color], outputs=[back_color_rgb]).success(
|
| 117 |
+
combin_image, inputs=[files, back_color_rgb, margin_top, margin_between, margin_side], outputs=[out_image]
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
demo.queue()
|
| 121 |
+
demo.launch(share=False, debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio==5.25.2
|