Zengyf-CVer commited on
Commit
e9acbb1
1 Parent(s): aa56ecb

app update

Browse files
Files changed (2) hide show
  1. .gitignore +0 -1
  2. app.py +78 -0
.gitignore CHANGED
@@ -57,7 +57,6 @@
57
  !requirements.txt
58
  !.pre-commit-config.yaml
59
 
60
- app.py
61
  test.py
62
  test*.py
63
 
 
57
  !requirements.txt
58
  !.pre-commit-config.yaml
59
 
 
60
  test.py
61
  test*.py
62
 
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Watermarking Lab
2
+ # 创建人:曾逸夫
3
+ # 创建时间:2022-08-08
4
+
5
+ import sys
6
+
7
+ import gradio as gr
8
+ from PIL import Image, ImageDraw, ImageFont
9
+
10
+ from util.fonts_opt import is_fonts
11
+
12
+ ROOT_PATH = sys.path[0] # 根目录
13
+ DESCRIPTION = '''# Watermarking Lab v0.1'''
14
+
15
+
16
+ def watermarking(img, text, text_size, text_font, wm_location):
17
+ text_size = int(text_size)
18
+ draw = ImageDraw.Draw(img)
19
+
20
+ font = ImageFont.truetype(f"./fonts/{text_font}.ttf", text_size)
21
+ textwidth, textheight = draw.textsize(text, font)
22
+
23
+ width, height = img.size
24
+ if wm_location == "center":
25
+ x = width / 2 - textwidth / 2
26
+ y = height / 2 - textheight / 2
27
+ elif wm_location == "bottom right":
28
+ x = width - textwidth - text_size
29
+ y = height - textheight - text_size
30
+
31
+ draw.text((x, y), text, font=font)
32
+ return img
33
+
34
+
35
+ def main():
36
+ is_fonts(f"{ROOT_PATH}/fonts") # 检查字体文件
37
+
38
+ with gr.Blocks(css='style.css') as gyd:
39
+ gr.Markdown(DESCRIPTION)
40
+ with gr.Row():
41
+ with gr.Column():
42
+ with gr.Row():
43
+ input_img = gr.Image(image_mode="RGB", source="upload", type="pil", label="原始图片")
44
+ with gr.Row():
45
+ wm_location = gr.Radio(choices=["center", "bottom right"], value="中间")
46
+ with gr.Row():
47
+ wm_text = gr.Textbox(value="水印内容", label="水印内容")
48
+ with gr.Row():
49
+ wm_textFont = gr.Dropdown(choices=["SimSun", "TimesNewRoman", "malgun"],
50
+ value="TimesNewRoman",
51
+ label="字体")
52
+ with gr.Row():
53
+ wm_textSize = gr.Number(value=50, label="文字大小")
54
+
55
+ with gr.Row():
56
+ btn_01 = gr.Button(value='加水印', variant="primary")
57
+
58
+ with gr.Column():
59
+ with gr.Row():
60
+ output_img = gr.Image(type="pil", label="水印图片")
61
+
62
+ with gr.Row():
63
+ example_list = [["./img_examples/bus.jpg", "Watermarking Text", 50, "TimesNewRoman", "bottom right"],
64
+ ["./img_examples/zidane.jpg", "水印文字", 50, "SimSun", "center"]]
65
+ gr.Examples(example_list, [input_img, wm_text, wm_textSize, wm_textFont, wm_location],
66
+ output_img,
67
+ watermarking,
68
+ cache_examples=False)
69
+
70
+ btn_01.click(fn=watermarking,
71
+ inputs=[input_img, wm_text, wm_textSize, wm_textFont, wm_location],
72
+ outputs=[output_img])
73
+
74
+ gyd.launch(inbrowser=True)
75
+
76
+
77
+ if __name__ == '__main__':
78
+ main()