hong-xl commited on
Commit
a2f6548
1 Parent(s): 0987ddf

update application file

Browse files
Files changed (1) hide show
  1. app.py +21 -5
app.py CHANGED
@@ -1,10 +1,26 @@
1
  import gradio as gr
 
 
2
 
3
- # def greet(name):
4
- # return "Hello " + name + "!!"
5
 
6
- def greet(img):
7
- return img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # Create Gradio input and output components
10
  image_input = gr.inputs.Image(type='filepath', label="Input Image")
@@ -17,6 +33,6 @@ semantic_segment_device = gr.inputs.Radio(choices=['cuda', 'cpu'], default='cuda
17
  controlnet_device = gr.inputs.Radio(choices=['cuda', 'cpu'], default='cpu', label='ControlNet Device')
18
 
19
  # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
20
- iface = gr.Interface(fn=greet, inputs=[image_input], outputs=gr.outputs.HTML())
21
 
22
  iface.launch()
 
1
  import gradio as gr
2
+ from PIL import Image, ImageDraw, ImageFont
3
+ import io
4
 
 
 
5
 
6
+ # 定义一个函数,它接受一张图片和一些文本,返回一个 HTML 页面
7
+ def create_html_page(input_image, text):
8
+ # 将输入图片转换为 Pillow Image 对象
9
+ image = Image.open(io.BytesIO(input_image))
10
+ # 创建一个新的 Pillow Image 对象,大小为原始图片大小加上文本高度
11
+ new_image = Image.new('RGB', (image.width, image.height + 50), color = 'white')
12
+ new_image.paste(image, (0, 0))
13
+ draw = ImageDraw.Draw(new_image)
14
+ # 定义字体、字号和颜色
15
+ font = ImageFont.truetype("arial.ttf", 36)
16
+ color = (0, 0, 0)
17
+ # 在图片下方绘制文本
18
+ draw.text((0, image.height), text, font=font, fill=color)
19
+ # 将带有图片和文本的 HTML 页面返回
20
+ output_html = '<img src="data:image/png;base64,{}">'.format(
21
+ io.BytesIO(input_image).getvalue().encode('base64').decode())
22
+ output_html += '<p>{}</p>'.format(text)
23
+ return output_html
24
 
25
  # Create Gradio input and output components
26
  image_input = gr.inputs.Image(type='filepath', label="Input Image")
 
33
  controlnet_device = gr.inputs.Radio(choices=['cuda', 'cpu'], default='cpu', label='ControlNet Device')
34
 
35
  # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
36
+ iface = gr.Interface(fn=create_html_page, inputs=[image_input], outputs=["html"])
37
 
38
  iface.launch()