Sangwon78 commited on
Commit
4741617
ยท
verified ยท
1 Parent(s): 3017648

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -23
app.py CHANGED
@@ -1,38 +1,27 @@
1
  import gradio as gr
2
- from PIL import Image, ImageDraw, ImageFont, ImageEnhance, ImageFilter
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
- # ๊ธฐ๋ณธ ์ด๋ฏธ์ง€ ์ƒ์„ฑ (๊ฐ€๋กœ 800px, ์„ธ๋กœ 400px)
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()