File size: 3,332 Bytes
3a8ba72
babe760
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a8ba72
 
 
 
 
 
 
 
babe760
3a8ba72
 
babe760
 
 
3a8ba72
 
 
 
 
 
 
 
 
 
 
 
 
 
babe760
 
 
 
 
 
 
 
 
 
 
 
 
3a8ba72
 
 
 
babe760
3a8ba72
 
 
 
 
 
 
 
babe760
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from PIL import Image



# class WatermarkApp:
#     def __init__(self):
#         pass
    
#     def process_image(self, image):
#         watermark = Image.open('asset/chaojihui-v2.png')
#         # 获取水印的等比例缩放尺寸
#         wm_width, wm_height = watermark.size
#         import random
#         # target_size = random.randint(100, 300)
#         target_size = 100
#         alpha = 0.3

#         if wm_width > wm_height:
#             new_wm_width = target_size
#             new_wm_height = int((wm_height / wm_width) * target_size)
#         else:
#             new_wm_height = target_size
#             new_wm_width = int((wm_width / wm_height) * target_size)

#         wm = watermark.resize((new_wm_width, new_wm_height), Image.Resampling.LANCZOS)

#         # 处理透明度
#         if alpha < 1.0:
#             wm = self.apply_transparency(wm, alpha)

#         # 确保原图是RGBA模式
#         if image.mode != 'RGBA':
#             image = image.convert('RGBA')

#         # 在整个图片上铺满水印
#         for y in range(0, image.height, new_wm_height):
#             for x in range(0, image.width, new_wm_width):
#                 image.alpha_composite(wm, dest=(x, y))
#         return image

#     def apply_transparency(self, watermark, alpha):
#         """应用透明度"""
#         matrix = watermark.split()[-1].point(lambda x: x * alpha)
#         watermark.putalpha(matrix)
#         return watermark



class WatermarkApp:
    def __init__(self):
        pass
    
    def process_image(self, image):
        watermark = Image.open('asset/chaojihui-v2.png')
        # 获取水印的等比例缩放尺寸
        wm_width, wm_height = watermark.size
        import random
        # target_size = random.randint(100, 300)
        target_size = 100
        alpha = 0.3

        if wm_width > wm_height:
            new_wm_width = target_size
            new_wm_height = int((wm_height / wm_width) * target_size)
        else:
            new_wm_height = target_size
            new_wm_width = int((wm_width / wm_height) * target_size)

        wm = watermark.resize((new_wm_width, new_wm_height), Image.Resampling.LANCZOS)

        # 处理透明度
        if alpha < 1.0:
            wm = self.apply_transparency(wm, alpha)
            
        double_image = Image.new('RGBA', (image.width * 2, image.height * 2), (0, 0, 0, 0))
        for y in range(0, double_image.height, new_wm_height):
            for x in range(0, double_image.width, new_wm_width):
                double_image.alpha_composite(wm, dest=(x, y))
                
        angle = random.uniform(-180, 180)
        double_image = double_image.rotate(angle)
        
        print(double_image)
        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)
        print(double_image)
        
        # 确保原图是RGBA模式
        if image.mode != 'RGBA':
            image = image.convert('RGBA')

        image.alpha_composite(double_image, dest=(0, 0))
        return image

    def apply_transparency(self, watermark, alpha):
        """应用透明度"""
        matrix = watermark.split()[-1].point(lambda x: x * alpha)
        watermark.putalpha(matrix)
        return watermark