Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
|
| 3 |
import io
|
| 4 |
|
| 5 |
-
# ํ
์คํธ๋ฅผ ์ด๋ฏธ์ง๋ก ๋ณํํ๋ ํจ์
|
| 6 |
def text_to_image(text, mode="Color", brightness=1.0, tone=1.0, filter_strength=1.0, output_format="JPG"):
|
| 7 |
-
#
|
| 8 |
img = Image.new("RGB" if mode == "Color" else "L", (800, 400), "white")
|
| 9 |
draw = ImageDraw.Draw(img)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
try:
|
| 13 |
-
font = ImageFont.truetype("arial.ttf", 24) # Arial ํฐํธ
|
| 14 |
-
except:
|
| 15 |
-
font = ImageFont.load_default() # ๊ธฐ๋ณธ ํฐํธ ๋์ฒด
|
| 16 |
-
|
| 17 |
-
# ํ
์คํธ๋ฅผ ์ด๋ฏธ์ง ์ค์์ ์ถ๋ ฅ
|
| 18 |
-
text_width, text_height = draw.textsize(text, font=font)
|
| 19 |
-
text_x = (img.width - text_width) // 2
|
| 20 |
-
text_y = (img.height - text_height) // 2
|
| 21 |
-
draw.text((text_x, text_y), text, fill="black" if mode == "Color" else "gray", font=font)
|
| 22 |
|
| 23 |
# ๋ฐ๊ธฐ ์กฐ์
|
| 24 |
enhancer = ImageEnhance.Brightness(img)
|
| 25 |
img = enhancer.enhance(brightness)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
enhancer = ImageEnhance.Contrast(img)
|
| 29 |
img = enhancer.enhance(tone)
|
| 30 |
|
| 31 |
-
# ํํฐ
|
| 32 |
if filter_strength < 1.0:
|
| 33 |
img = img.filter(ImageFilter.GaussianBlur(radius=(1 - filter_strength) * 5))
|
| 34 |
|
| 35 |
-
#
|
| 36 |
buffer = io.BytesIO()
|
| 37 |
if output_format == "JPG":
|
| 38 |
img.save(buffer, format="JPEG")
|
|
@@ -41,10 +30,9 @@ def text_to_image(text, mode="Color", brightness=1.0, tone=1.0, filter_strength=
|
|
| 41 |
elif output_format == "PDF":
|
| 42 |
img.save(buffer, format="PDF")
|
| 43 |
buffer.seek(0)
|
| 44 |
-
|
| 45 |
return buffer
|
| 46 |
|
| 47 |
-
# Gradio ์ธํฐํ์ด์ค
|
| 48 |
iface = gr.Interface(
|
| 49 |
fn=text_to_image,
|
| 50 |
inputs=[
|
|
@@ -55,11 +43,10 @@ iface = gr.Interface(
|
|
| 55 |
gr.Slider(0.1, 1.0, step=0.1, label="ํํฐ ๊ฐ๋", value=1.0),
|
| 56 |
gr.Radio(["JPG", "PNG", "PDF"], label="์ถ๋ ฅ ํฌ๋งท", value="JPG"),
|
| 57 |
],
|
| 58 |
-
outputs=gr.File(label="๋ค์ด๋ก๋
|
| 59 |
title="ํ
์คํธ๋ฅผ ์ด๋ฏธ์ง๋ก ๋ณํ",
|
| 60 |
-
description="
|
| 61 |
)
|
| 62 |
|
| 63 |
-
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
| 64 |
if __name__ == "__main__":
|
| 65 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
|
| 3 |
import io
|
| 4 |
|
|
|
|
| 5 |
def text_to_image(text, mode="Color", brightness=1.0, tone=1.0, filter_strength=1.0, output_format="JPG"):
|
| 6 |
+
# ์ด๋ฏธ์ง ์์ฑ
|
| 7 |
img = Image.new("RGB" if mode == "Color" else "L", (800, 400), "white")
|
| 8 |
draw = ImageDraw.Draw(img)
|
| 9 |
+
font = ImageFont.load_default()
|
| 10 |
+
draw.text((10, 10), text, fill="black" if mode == "Color" else "gray", font=font)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# ๋ฐ๊ธฐ ์กฐ์
|
| 13 |
enhancer = ImageEnhance.Brightness(img)
|
| 14 |
img = enhancer.enhance(brightness)
|
| 15 |
|
| 16 |
+
# ํํฐ (ํค ์กฐ์ ) ์ถ๊ฐ
|
| 17 |
enhancer = ImageEnhance.Contrast(img)
|
| 18 |
img = enhancer.enhance(tone)
|
| 19 |
|
| 20 |
+
# ํํฐ ๊ฐ๋ (์: ํ๋ฆผ ํจ๊ณผ)
|
| 21 |
if filter_strength < 1.0:
|
| 22 |
img = img.filter(ImageFilter.GaussianBlur(radius=(1 - filter_strength) * 5))
|
| 23 |
|
| 24 |
+
# ํ์ผ ์ ์ฅ
|
| 25 |
buffer = io.BytesIO()
|
| 26 |
if output_format == "JPG":
|
| 27 |
img.save(buffer, format="JPEG")
|
|
|
|
| 30 |
elif output_format == "PDF":
|
| 31 |
img.save(buffer, format="PDF")
|
| 32 |
buffer.seek(0)
|
|
|
|
| 33 |
return buffer
|
| 34 |
|
| 35 |
+
# Gradio ์ธํฐํ์ด์ค
|
| 36 |
iface = gr.Interface(
|
| 37 |
fn=text_to_image,
|
| 38 |
inputs=[
|
|
|
|
| 43 |
gr.Slider(0.1, 1.0, step=0.1, label="ํํฐ ๊ฐ๋", value=1.0),
|
| 44 |
gr.Radio(["JPG", "PNG", "PDF"], label="์ถ๋ ฅ ํฌ๋งท", value="JPG"),
|
| 45 |
],
|
| 46 |
+
outputs=gr.File(label="๋ค์ด๋ก๋"),
|
| 47 |
title="ํ
์คํธ๋ฅผ ์ด๋ฏธ์ง๋ก ๋ณํ",
|
| 48 |
+
description="ํ
์คํธ๋ฅผ ์
๋ ฅํ๊ณ ์ํ๋ ์ค์ ์ ํตํด ์ด๋ฏธ์ง๋ฅผ ์์ฑํฉ๋๋ค."
|
| 49 |
)
|
| 50 |
|
|
|
|
| 51 |
if __name__ == "__main__":
|
| 52 |
iface.launch()
|