刘宇轩 commited on
Commit
babe760
·
1 Parent(s): bc342c5
Files changed (2) hide show
  1. asset/chaojihui45-v2.png +0 -0
  2. watermark.py +68 -7
asset/chaojihui45-v2.png ADDED
watermark.py CHANGED
@@ -1,4 +1,53 @@
 
 
1
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
 
4
 
@@ -7,10 +56,12 @@ class WatermarkApp:
7
  pass
8
 
9
  def process_image(self, image):
10
- watermark = Image.open('asset/zhushe.png')
11
  # 获取水印的等比例缩放尺寸
12
  wm_width, wm_height = watermark.size
13
- target_size = 600
 
 
14
  alpha = 0.3
15
 
16
  if wm_width > wm_height:
@@ -25,15 +76,24 @@ class WatermarkApp:
25
  # 处理透明度
26
  if alpha < 1.0:
27
  wm = self.apply_transparency(wm, alpha)
28
-
 
 
 
 
 
 
 
 
 
 
 
 
29
  # 确保原图是RGBA模式
30
  if image.mode != 'RGBA':
31
  image = image.convert('RGBA')
32
 
33
- # 在整个图片上铺满水印
34
- for y in range(0, image.height, new_wm_height):
35
- for x in range(0, image.width, new_wm_width):
36
- image.alpha_composite(wm, dest=(x, y))
37
  return image
38
 
39
  def apply_transparency(self, watermark, alpha):
@@ -42,3 +102,4 @@ class WatermarkApp:
42
  watermark.putalpha(matrix)
43
  return watermark
44
 
 
 
1
+ import os
2
+ import oss2
3
  from PIL import Image
4
+ import gradio as gr
5
+ import numpy as np
6
+ from io import BytesIO
7
+
8
+
9
+
10
+ # class WatermarkApp:
11
+ # def __init__(self):
12
+ # pass
13
+
14
+ # def process_image(self, image):
15
+ # watermark = Image.open('asset/chaojihui-v2.png')
16
+ # # 获取水印的等比例缩放尺寸
17
+ # wm_width, wm_height = watermark.size
18
+ # import random
19
+ # # target_size = random.randint(100, 300)
20
+ # target_size = 100
21
+ # alpha = 0.3
22
+
23
+ # if wm_width > wm_height:
24
+ # new_wm_width = target_size
25
+ # new_wm_height = int((wm_height / wm_width) * target_size)
26
+ # else:
27
+ # new_wm_height = target_size
28
+ # new_wm_width = int((wm_width / wm_height) * target_size)
29
+
30
+ # wm = watermark.resize((new_wm_width, new_wm_height), Image.Resampling.LANCZOS)
31
+
32
+ # # 处理透明度
33
+ # if alpha < 1.0:
34
+ # wm = self.apply_transparency(wm, alpha)
35
+
36
+ # # 确保原图是RGBA模式
37
+ # if image.mode != 'RGBA':
38
+ # image = image.convert('RGBA')
39
+
40
+ # # 在整个图片上铺满水印
41
+ # for y in range(0, image.height, new_wm_height):
42
+ # for x in range(0, image.width, new_wm_width):
43
+ # image.alpha_composite(wm, dest=(x, y))
44
+ # return image
45
+
46
+ # def apply_transparency(self, watermark, alpha):
47
+ # """应用透明度"""
48
+ # matrix = watermark.split()[-1].point(lambda x: x * alpha)
49
+ # watermark.putalpha(matrix)
50
+ # return watermark
51
 
52
 
53
 
 
56
  pass
57
 
58
  def process_image(self, image):
59
+ watermark = Image.open('asset/chaojihui-v2.png')
60
  # 获取水印的等比例缩放尺寸
61
  wm_width, wm_height = watermark.size
62
+ import random
63
+ # target_size = random.randint(100, 300)
64
+ target_size = 100
65
  alpha = 0.3
66
 
67
  if wm_width > wm_height:
 
76
  # 处理透明度
77
  if alpha < 1.0:
78
  wm = self.apply_transparency(wm, alpha)
79
+
80
+ double_image = Image.new('RGBA', (image.width * 2, image.height * 2), (0, 0, 0, 0))
81
+ for y in range(0, double_image.height, new_wm_height):
82
+ for x in range(0, double_image.width, new_wm_width):
83
+ double_image.alpha_composite(wm, dest=(x, y))
84
+
85
+ angle = random.uniform(-180, 180)
86
+ double_image = double_image.rotate(angle)
87
+
88
+ print(double_image)
89
+ double_image = double_image.crop((image.width//2, image.height//2, (image.width*3)//2, (image.height*3)//2)).resize(image.size, Image.Resampling.LANCZOS)
90
+ print(double_image)
91
+
92
  # 确保原图是RGBA模式
93
  if image.mode != 'RGBA':
94
  image = image.convert('RGBA')
95
 
96
+ image.alpha_composite(double_image, dest=(0, 0))
 
 
 
97
  return image
98
 
99
  def apply_transparency(self, watermark, alpha):
 
102
  watermark.putalpha(matrix)
103
  return watermark
104
 
105
+