johann-foerster commited on
Commit
b47a274
·
1 Parent(s): 99a9568

implement text (watermark) + logo feature

Browse files
resources/SourceSansPro-Regular.ttf ADDED
Binary file (119 kB). View file
 
resources/qm_logo.png ADDED
webui.py CHANGED
@@ -1,20 +1,19 @@
1
  import gradio as gr
2
  from gradio.themes.utils.colors import Color
3
- import random
4
- import time
5
- import shared
6
- import argparse
7
- import modules.path
8
- import fooocus_version
9
- import modules.html
10
  import modules.async_worker as worker
 
 
11
 
12
  from modules.sdxl_styles import style_keys, aspect_ratios
13
 
 
 
 
14
  def generate(*args):
15
  yield gr.update(interactive=False), \
16
  gr.update(visible=True, value=modules.html.make_progress_html(1, "0/30")), \
17
- gr.update(visible=True, show_share_button=False, show_download_button=False), \
18
  gr.update(visible=False)
19
 
20
  while worker.is_working:
@@ -44,23 +43,45 @@ def generate(*args):
44
  return
45
 
46
  def toggle_greet_visibility(is_visible):
47
- yield gr.update(visible=is_visible), gr.update(visible=is_visible)
48
 
49
  def debounce():
50
  time.sleep(0.5)
51
 
52
- def make_overlay(generated_image_raw, toggle_greet, greet):
53
- return gr.update(visible=False), gr.update(visible=True, value=generated_image_raw)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- qm_color=Color(name="qm", c50="#effaed",c100="#def5db",c200="#64b445",c300="#c6eec0",c400="#b9ebb3",c500="#64b445",c600="#55993b",c700="#467e30",c800="#325a23",c900="#233f18",c950="#192d11")
56
- shared.gradio_root = gr.Blocks(title='QualityMinds AI Christmas Card Maker', css=modules.html.css, theme=gr.themes.Default(primary_hue=qm_color))
 
 
 
57
  with shared.gradio_root:
58
  gr.Markdown(
59
  """
60
  # QualityMinds KI Weihnachtskarten-Generator
61
- * Beschreibe das Motiv der Weihnachtskarte in einem Prompt (Englisch), wähle einen Stil und generiere!
62
- * Zur Übersetzung einer Beschreibung ins Englische kann beispielsweise [deepl.com](https://www.deepl.com/translator#de/Schneekugeln) helfen.
63
- * Das Tool basiert auf [Stable Diffusion XL](https://stability.ai/stable-diffusion) v1.0 und der Oberfläche [Fooocus](https://github.com/lllyasviel/Fooocus), der Code ist OpenSource auf [Github](https://github.com/QualityMinds/AI-Christmas-Cards) verfügbar.
64
  """)
65
  with gr.Row(elem_classes='type_row'):
66
  with gr.Column(scale=2):
@@ -82,22 +103,30 @@ with shared.gradio_root:
82
  progress_html = gr.HTML(visible=False, elem_id='progress-bar', elem_classes='progress-bar')
83
 
84
  with gr.Row(elem_classes='type_row'):
85
- with gr.Column(scale=2):
 
 
 
 
 
 
86
  toggle_greet = gr.Checkbox(label="Persönliche Weihnachtsgrüße hinzufügen", elem_id="toggle-greet-checkbox",
87
  container=False, value=True, interactive=True)
88
- greet = gr.Textbox(value="", placeholder="", interactive=True,
89
- elem_classes='type_row', container=False, lines=2)
90
- greet_example_column = gr.Column(scale=1)
91
- with greet_example_column:
92
- gr.Markdown("<br>", elem_classes="input-label")
93
  gr.Markdown("##### Beispiele", elem_classes="input-label")
94
- greet_examples = gr.Examples(label="Beispiele", examples=["Frohe Weihnachten"], inputs=[greet],
95
  elem_id="greet-examples")
96
 
97
- generated_image_raw = gr.Image(visible=False, label="Weihnachtskarte", width=1280, value="resources/init.png", interactive=False )
98
- generated_image_overlayed = gr.Image(label="Weihnachtskarte", width=1280, value="resources/init.png" )
 
 
99
 
100
- toggle_greet.change(fn=toggle_greet_visibility, inputs=[toggle_greet], outputs=[greet, greet_example_column])
 
101
  greet.change(fn=debounce)\
102
  .then(fn=make_overlay, inputs=[generated_image_raw, toggle_greet, greet], outputs=[generated_image_raw, generated_image_overlayed])
103
  run_button.click(fn=generate, inputs=[prompt, style_selection], outputs=[run_button, progress_html, generated_image_raw, generated_image_overlayed])\
 
1
  import gradio as gr
2
  from gradio.themes.utils.colors import Color
3
+ import random, time, shared, argparse, modules.path, fooocus_version, modules.html
 
 
 
 
 
 
4
  import modules.async_worker as worker
5
+ from math import floor
6
+ from PIL import Image, ImageDraw, ImageFont
7
 
8
  from modules.sdxl_styles import style_keys, aspect_ratios
9
 
10
+ QM_LOGO=Image.open("resources/qm_logo.png")
11
+ QM_COLOR=Color(name="qm", c50="#effaed",c100="#def5db",c200="#64b445",c300="#c6eec0",c400="#b9ebb3",c500="#64b445",c600="#55993b",c700="#467e30",c800="#325a23",c900="#233f18",c950="#192d11")
12
+
13
  def generate(*args):
14
  yield gr.update(interactive=False), \
15
  gr.update(visible=True, value=modules.html.make_progress_html(1, "0/30")), \
16
+ gr.update(visible=True), \
17
  gr.update(visible=False)
18
 
19
  while worker.is_working:
 
43
  return
44
 
45
  def toggle_greet_visibility(is_visible):
46
+ return gr.update(visible=is_visible), gr.update(visible=is_visible)
47
 
48
  def debounce():
49
  time.sleep(0.5)
50
 
51
+ def pil_image_with_overlay(pil_image, toggle_greet, greet):
52
+ image = ImageDraw.Draw(pil_image, "RGBA")
53
+ HEIGHT, TEXT_PADDING, TEXTBOX_HEIGHT = 100, 32, 56
54
+ TEXTBOX_WIDTH = 1280-HEIGHT-4*TEXT_PADDING
55
+
56
+ image.rectangle(((0 if toggle_greet else 1280-HEIGHT-2*TEXT_PADDING, 768-HEIGHT), (1280, 768)), fill=(255, 255, 255, 222))
57
+ image._image.paste(QM_LOGO, (1135,675), QM_LOGO)
58
+ if (toggle_greet):
59
+ txt_image = Image.new('RGBA', (4*1280, 4*TEXTBOX_HEIGHT), (255,255,255,0))
60
+ ImageDraw.Draw(txt_image).text(xy=(0,0), text=greet, font=ImageFont.truetype('resources/SourceSansPro-Regular.ttf', floor(1.5*TEXTBOX_HEIGHT)), fill=(100, 180, 69))
61
+ image_box = txt_image.getbbox()
62
+ txt_image = txt_image.crop(image_box)
63
+ width, height = txt_image.size
64
+ if (height > 0 and width/height < TEXTBOX_WIDTH/TEXTBOX_HEIGHT):
65
+ txt_image = txt_image.resize((floor((TEXTBOX_HEIGHT/height)*width), TEXTBOX_HEIGHT))
66
+ else:
67
+ txt_image = txt_image.resize((TEXTBOX_WIDTH, floor((TEXTBOX_WIDTH/width)*height)))
68
+ _, height = txt_image.size
69
+ image._image.paste(txt_image, (TEXT_PADDING, 768 - floor((HEIGHT+height)/2)), txt_image)
70
+
71
+ return image._image
72
 
73
+ def make_overlay(image, toggle_greet, greet):
74
+ return gr.update(visible=False), \
75
+ gr.update(visible=True, value=pil_image_with_overlay(image, toggle_greet, greet))
76
+
77
+ shared.gradio_root = gr.Blocks(title='QualityMinds AI Christmas Card Maker', css=modules.html.css, theme=gr.themes.Default(primary_hue=QM_COLOR))
78
  with shared.gradio_root:
79
  gr.Markdown(
80
  """
81
  # QualityMinds KI Weihnachtskarten-Generator
82
+ * Beschreibe das Motiv der Weihnachtskarte in einem Prompt (Englisch), wähle ggf. Stil und los!
83
+ * Zur Übersetzung einer Beschreibung ins Englische eignet sich beispielsweise [deepl.com](https://www.deepl.com/translator#de/Schneekugeln) helfen.
84
+ * Werkzeug basiert auf [Stable Diffusion XL](https://stability.ai/stable-diffusion) v1.0 und der Oberfläche [Fooocus](https://github.com/lllyasviel/Fooocus), der Code ist OpenSource auf [Github](https://github.com/QualityMinds/AI-Christmas-Cards) verfügbar.
85
  """)
86
  with gr.Row(elem_classes='type_row'):
87
  with gr.Column(scale=2):
 
103
  progress_html = gr.HTML(visible=False, elem_id='progress-bar', elem_classes='progress-bar')
104
 
105
  with gr.Row(elem_classes='type_row'):
106
+ example_texts = [
107
+ "Ich wünsche dir von Herzen ein frohes Weihnachtsfest.",
108
+ "Frohe Weihnachten und viele glückliche Momente mit deinen Liebsten!",
109
+ "Zauberhafte Weihnachten für dich!!",
110
+ "Möge die Magie von Weihnachten dein Herz erleuchten und deine Wünsche erfüllen."
111
+ ]
112
+ with gr.Column(scale=1):
113
  toggle_greet = gr.Checkbox(label="Persönliche Weihnachtsgrüße hinzufügen", elem_id="toggle-greet-checkbox",
114
  container=False, value=True, interactive=True)
115
+ greet = gr.Textbox(value=example_texts[0], placeholder="", interactive=True,
116
+ elem_classes='type_row', container=False, lines=2, max_lines=2)
117
+ greet_examples_column = gr.Column(scale=2)
118
+ with greet_examples_column:
 
119
  gr.Markdown("##### Beispiele", elem_classes="input-label")
120
+ greet_examples = gr.Examples(label="Beispiele", examples=example_texts, inputs=[greet],
121
  elem_id="greet-examples")
122
 
123
+ generated_image_raw = gr.Image(visible=False, type='pil', label="Erstelle Weihnachtskarte...", width=1280,
124
+ value="resources/init.png", interactive=False,
125
+ show_share_button=False, show_download_button=False )
126
+ generated_image_overlayed = gr.Image(label="Weihnachtskarte", type='pil', width=1280, value=pil_image_with_overlay(Image.open("resources/init.png"), toggle_greet.value, greet.value))
127
 
128
+ toggle_greet.change(fn=toggle_greet_visibility, inputs=[toggle_greet], outputs=[greet, greet_examples_column])\
129
+ .then(fn=make_overlay, inputs=[generated_image_raw, toggle_greet, greet], outputs=[generated_image_raw, generated_image_overlayed])
130
  greet.change(fn=debounce)\
131
  .then(fn=make_overlay, inputs=[generated_image_raw, toggle_greet, greet], outputs=[generated_image_raw, generated_image_overlayed])
132
  run_button.click(fn=generate, inputs=[prompt, style_selection], outputs=[run_button, progress_html, generated_image_raw, generated_image_overlayed])\