Change watermark position

Brightcells 7 lat temu
rodzic
commit
5411626ebe
1 zmienionych plików z 10 dodań i 3 usunięć
  1. 10 3
      utils/watermark_utils.py

+ 10 - 3
utils/watermark_utils.py

@@ -17,7 +17,7 @@ def reduce_opacity(im, opacity):
17 17
     return im
18 18
 
19 19
 
20
-def watermark(im, mark, position, opacity=1, maxsize=(0, 0), possize=(0, 0)):
20
+def watermark(im, mark, position, opacity=1, maxsize=(0, 0), possize=(0, 0, None, None)):
21 21
     """ Add watermark to image """
22 22
     if opacity < 1:
23 23
         mark = reduce_opacity(mark, opacity)
@@ -39,7 +39,12 @@ def watermark(im, mark, position, opacity=1, maxsize=(0, 0), possize=(0, 0)):
39 39
         w, h = int(mark.size[0] * ratio), int(mark.size[1] * ratio)
40 40
         w, h = int(min(w, maxsize[0]) if maxsize[0] else w), int(min(h, maxsize[1]) if maxsize[1] else h)
41 41
         mark = mark.resize((w, h))
42
-        layer.paste(mark, ((im.size[0] - possize[0] or w) / 2, (im.size[1] - possize[1] or h) / 2))
42
+        possize += (None, ) * (4 - len(possize))
43
+        lx, ly, rx, ry = possize
44
+        if rx is None or ry is None:
45
+            layer.paste(mark, ((im.size[0] - (lx or w)) / 2, (im.size[1] - (ly or h)) / 2))
46
+        else:
47
+            layer.paste(mark, ((im.size[0] - rx - w), ry))
43 48
     else:
44 49
         layer.paste(mark, position)
45 50
     # Composite the watermark with the layer
@@ -49,7 +54,8 @@ def watermark(im, mark, position, opacity=1, maxsize=(0, 0), possize=(0, 0)):
49 54
 def watermark_wrap(im_path, mark_path, save_path=''):
50 55
     im, mark = Image.open(im_path), Image.open(mark_path)
51 56
     # new_im = watermark(im, mark, (50, 50), 0.5)
52
-    new_im = watermark(im, mark, position='scale', opacity=1.0, maxsize=(400, 505.2), possize=(400, 400))
57
+    # new_im = watermark(im, mark, position='scale', opacity=1.0, maxsize=(400, 505.2), possize=(400, 400))
58
+    new_im = watermark(im, mark, position='scale', opacity=1.0, maxsize=(200, 247.1875), possize=(0, 0, 15, 15))
53 59
     new_im.save(save_path or im_path)
54 60
 
55 61
 
@@ -57,6 +63,7 @@ def watermark_test():
57 63
     im, mark = Image.open('original_CGzC_10a50000c8811190.jpg'), Image.open('paiai_water_mark.png')
58 64
     watermark(im, mark, position='tile', opacity=0.5, maxsize=(40, 49.4375)).show()
59 65
     watermark(im, mark, position='scale', opacity=1.0, maxsize=(400, 494.375), possize=(400, 400)).show()
66
+    watermark(im, mark, position='scale', opacity=1.0, maxsize=(200, 247.1875), possize=(0, 0, 10, 10)).show()
60 67
     watermark(im, mark, position=(50, 50), opacity=0.5, maxsize=(40, 49.4375)).show()
61 68
 
62 69