Spaces:
Runtime error
Runtime error
Commit
·
2926e61
1
Parent(s):
93a5abe
app update
Browse files- .gitignore +1 -0
- app.py +50 -16
.gitignore
CHANGED
|
@@ -57,6 +57,7 @@
|
|
| 57 |
!requirements.txt
|
| 58 |
!.pre-commit-config.yaml
|
| 59 |
|
|
|
|
| 60 |
test.py
|
| 61 |
test*.py
|
| 62 |
|
|
|
|
| 57 |
!requirements.txt
|
| 58 |
!.pre-commit-config.yaml
|
| 59 |
|
| 60 |
+
app copy.py
|
| 61 |
test.py
|
| 62 |
test*.py
|
| 63 |
|
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
# Watermarking Lab
|
| 2 |
# 创建人:曾逸夫
|
| 3 |
-
# 创建时间:2022-08-
|
|
|
|
| 4 |
|
|
|
|
| 5 |
import sys
|
| 6 |
|
| 7 |
import gradio as gr
|
|
@@ -10,26 +12,53 @@ from PIL import Image, ImageDraw, ImageFont
|
|
| 10 |
from util.fonts_opt import is_fonts
|
| 11 |
|
| 12 |
ROOT_PATH = sys.path[0] # 根目录
|
| 13 |
-
DESCRIPTION = '''# Watermarking Lab
|
| 14 |
|
| 15 |
|
| 16 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
text_size = int(text_size)
|
| 18 |
-
draw = ImageDraw.Draw(
|
| 19 |
|
| 20 |
font = ImageFont.truetype(f"./fonts/{text_font}.ttf", text_size)
|
| 21 |
textwidth, textheight = draw.textsize(text, font)
|
| 22 |
|
| 23 |
-
width, height =
|
|
|
|
|
|
|
| 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 |
-
|
| 32 |
-
return img
|
| 33 |
|
| 34 |
|
| 35 |
def main():
|
|
@@ -42,16 +71,19 @@ def main():
|
|
| 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"
|
|
|
|
|
|
|
| 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 |
|
|
@@ -60,15 +92,17 @@ def main():
|
|
| 60 |
output_img = gr.Image(type="pil", label="水印图片")
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
-
example_list = [[
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 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)
|
|
|
|
| 1 |
# Watermarking Lab
|
| 2 |
# 创建人:曾逸夫
|
| 3 |
+
# 创建时间:2022-08-10
|
| 4 |
+
# 功能描述:单一图片加水印 透明度 字体颜色
|
| 5 |
|
| 6 |
+
import math
|
| 7 |
import sys
|
| 8 |
|
| 9 |
import gradio as gr
|
|
|
|
| 12 |
from util.fonts_opt import is_fonts
|
| 13 |
|
| 14 |
ROOT_PATH = sys.path[0] # 根目录
|
| 15 |
+
DESCRIPTION = '''# Watermarking Lab'''
|
| 16 |
|
| 17 |
|
| 18 |
+
def Hex_to_RGB(hex):
|
| 19 |
+
r = int(hex[1:3], 16)
|
| 20 |
+
g = int(hex[3:5], 16)
|
| 21 |
+
b = int(hex[5:7], 16)
|
| 22 |
+
|
| 23 |
+
return r, g, b
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def watermarking(img, text, text_size, text_font, wm_location, wm_trans, wm_textColor):
|
| 27 |
+
# 参考:https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html?highlight=text#example-draw-partial-opacity-text
|
| 28 |
+
|
| 29 |
+
img_base = img.convert("RGBA")
|
| 30 |
+
txt = Image.new('RGBA', img_base.size, (255, 255, 255, 0))
|
| 31 |
+
|
| 32 |
text_size = int(text_size)
|
| 33 |
+
draw = ImageDraw.Draw(txt)
|
| 34 |
|
| 35 |
font = ImageFont.truetype(f"./fonts/{text_font}.ttf", text_size)
|
| 36 |
textwidth, textheight = draw.textsize(text, font)
|
| 37 |
|
| 38 |
+
width, height = img_base.size # 图片尺寸
|
| 39 |
+
|
| 40 |
+
# ---------- 水印位置 ----------
|
| 41 |
if wm_location == "center":
|
| 42 |
x = width / 2 - textwidth / 2
|
| 43 |
y = height / 2 - textheight / 2
|
| 44 |
elif wm_location == "bottom right":
|
| 45 |
x = width - textwidth - text_size
|
| 46 |
y = height - textheight - text_size
|
| 47 |
+
elif wm_location == "bottom left":
|
| 48 |
+
x = text_size
|
| 49 |
+
y = height - textheight - text_size
|
| 50 |
+
elif wm_location == "top right":
|
| 51 |
+
x = width - textwidth - text_size
|
| 52 |
+
y = text_size
|
| 53 |
+
elif wm_location == "top left":
|
| 54 |
+
x = text_size
|
| 55 |
+
y = text_size
|
| 56 |
+
|
| 57 |
+
r, g, b = Hex_to_RGB(wm_textColor)
|
| 58 |
+
draw.text((x, y), text, fill=(r, g, b, math.ceil(float(wm_trans) * 255)), font=font)
|
| 59 |
+
img_combined = Image.alpha_composite(img_base, txt)
|
| 60 |
|
| 61 |
+
return img_combined
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
def main():
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
input_img = gr.Image(image_mode="RGB", source="upload", type="pil", label="原始图片")
|
| 73 |
with gr.Row():
|
| 74 |
+
wm_location = gr.Radio(choices=["center", "bottom right", "bottom left", "top right", "top left"],
|
| 75 |
+
value="center",
|
| 76 |
+
label="位置")
|
| 77 |
with gr.Row():
|
| 78 |
wm_text = gr.Textbox(value="水印内容", label="水印内容")
|
| 79 |
with gr.Row():
|
| 80 |
+
wm_textFont = gr.Dropdown(choices=["SimSun", "TimesNewRoman", "malgun"], value="SimSun", label="字体")
|
|
|
|
|
|
|
| 81 |
with gr.Row():
|
| 82 |
wm_textSize = gr.Number(value=50, label="文字大小")
|
| 83 |
+
with gr.Row():
|
| 84 |
+
wm_textTrans = gr.Slider(minimum=0, maximum=1, value=1, step=0.1, label="水印透明度")
|
| 85 |
+
with gr.Row():
|
| 86 |
+
wm_textColor = gr.ColorPicker(label="水印颜色")
|
| 87 |
with gr.Row():
|
| 88 |
btn_01 = gr.Button(value='加水印', variant="primary")
|
| 89 |
|
|
|
|
| 92 |
output_img = gr.Image(type="pil", label="水印图片")
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
+
example_list = [[
|
| 96 |
+
"./img_examples/bus.jpg", "Watermarking Text", 50, "TimesNewRoman", "bottom right", "0.9", "#FFFFFF"],
|
| 97 |
+
["./img_examples/zidane.jpg", "水印文字", 50, "SimSun", "center", "0.5", "#2488C6"]]
|
| 98 |
+
gr.Examples(example_list,
|
| 99 |
+
[input_img, wm_text, wm_textSize, wm_textFont, wm_location, wm_textTrans, wm_textColor],
|
| 100 |
output_img,
|
| 101 |
watermarking,
|
| 102 |
cache_examples=False)
|
| 103 |
|
| 104 |
btn_01.click(fn=watermarking,
|
| 105 |
+
inputs=[input_img, wm_text, wm_textSize, wm_textFont, wm_location, wm_textTrans, wm_textColor],
|
| 106 |
outputs=[output_img])
|
| 107 |
|
| 108 |
gyd.launch(inbrowser=True)
|