-    default_storage.save(photo_path, photo)
90
+    if default_storage.exists(m_photo_path):
91
+        default_storage.delete(m_photo_path)
92
+    default_storage.save(m_photo_path, photo)
93
+
94
+    p_photo_path = 'photo/{photo_id}_p{extension}'.format(photo_id=photo_id, extension=extension)
95
+    watermark_wrap(
96
+        os.path.join(settings.MEDIA_ROOT, m_photo_path).replace('\\', '/'),
97
+        settings.WATERMARK_LOGO,
98
+        os.path.join(settings.MEDIA_ROOT, p_photo_path).replace('\\', '/')
99
+    )
93 100
 
94 101
     photo, created = PhotosInfo.objects.get_or_create(
95 102
         lensman_id=lensman_id,
96 103
         session_id=session_id,
97 104
         photo_id=photo_id,
98
-        photo_name=photo_name,
99
-        photo_path=photo_path
105
+        p_photo_path=p_photo_path,
106
+        m_photo_path=m_photo_path,
100 107
     )
101 108
 
102 109
     return JsonResponse({
@@ -113,22 +120,22 @@ def session_detail(request, session):
113 120
 
114 121
 def photo_standard(request, photo):
115 122
     photo = PhotosInfo.objects.get(photo_id=photo)
116
-    return render(request, 'photo/photo_detail.html', {'photo_url': photo.photo_url})
123
+    return render(request, 'photo/photo_detail.html', {'photo_url': photo.p_photo_url})
117 124
 
118 125
 
119 126
 def photo_medium(request, photo):
120 127
     photo = PhotosInfo.objects.get(photo_id=photo)
121
-    return render(request, 'photo/photo_detail.html', {'photo_url': photo.photo_url})
128
+    return render(request, 'photo/photo_detail.html', {'photo_url': photo.m_photo_url})
122 129
 
123 130
 
124 131
 def photo_large(request, photo):
125 132
     photo = PhotosInfo.objects.get(photo_id=photo)
126
-    return render(request, 'photo/photo_detail.html', {'photo_url': photo.photo_url})
133
+    return render(request, 'photo/photo_detail.html', {'photo_url': photo.l_photo_url})
127 134
 
128 135
 
129 136
 def photo_raw(request, photo):
130 137
     photo = PhotosInfo.objects.get(photo_id=photo)
131
-    return render(request, 'photo/photo_detail.html', {'photo_url': photo.photo_url})
138
+    return render(request, 'photo/photo_detail.html', {'photo_url': photo.r_photo_url})
132 139
 
133 140
 
134 141
 class PhotoInfoViewSet(viewsets.ModelViewSet):

BIN
utils/original_CGzC_10a50000c8811190.jpg


BIN
utils/original_CGzC_10a50000c8811190副本.jpg


BIN
utils/paiai_96_96.png


+ 62 - 0
utils/watermark_utils.py

@@ -0,0 +1,62 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+try:
4
+    import Image
5
+    import ImageEnhance
6
+except ImportError:
7
+    from PIL import Image, ImageEnhance
8
+
9
+
10
+def reduce_opacity(im, opacity):
11
+    """Returns an image with reduced opacity."""
12
+    assert 0 <= opacity <= 1
13
+    im = im.convert('RGBA') if im.mode != 'RGBA' else im.copy()
14
+    alpha = im.split()[3]
15
+    alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
16
+    im.putalpha(alpha)
17
+    return im
18
+
19
+
20
+def watermark(im, mark, position, opacity=1):
21
+    """Adds a watermark to an image."""
22
+    if opacity < 1:
23
+        mark = reduce_opacity(mark, opacity)
24
+    if im.mode != 'RGBA':
25
+        im = im.convert('RGBA')
26
+    # create a transparent layer the size of the image and draw the
27
+    # watermark in that layer.
28
+    layer = Image.new('RGBA', im.size, (0, 0, 0, 0))
29
+    if position == 'tile':
30
+        for y in range(0, im.size[1], mark.size[1]):
31
+            for x in range(0, im.size[0], mark.size[0]):
32
+                layer.paste(mark, (x, y))
33
+    elif position == 'scale':
34
+        # scale, but preserve the aspect ratio
35
+        ratio = min(
36
+            float(im.size[0]) / mark.size[0], float(im.size[1]) / mark.size[1])
37
+        w = int(mark.size[0] * ratio)
38
+        h = int(mark.size[1] * ratio)
39
+        mark = mark.resize((w, h))
40
+        layer.paste(mark, ((im.size[0] - w) / 2, (im.size[1] - h) / 2))
41
+    else:
42
+        layer.paste(mark, position)
43
+    # composite the watermark with the layer
44
+    return Image.composite(layer, im, layer)
45
+
46
+
47
+def watermark_wrap(im_path, mark_path, save_path=''):
48
+    im, mark = Image.open(im_path), Image.open(mark_path)
49
+    new_im = watermark(im, mark, (50, 50), 0.5)
50
+    new_im.save(save_path or im_path)
51
+
52
+
53
+def watermark_test():
54
+    im, mark = Image.open('original_CGzC_10a50000c8811190.jpg'), Image.open('paiai_96_96.png')
55
+    watermark(im, mark, 'tile', 0.5).show()
56
+    watermark(im, mark, 'scale', 1.0).show()
57
+    watermark(im, mark, (50, 50), 0.5).show()
58
+
59
+
60
+if __name__ == '__main__':
61
+    # watermark_test()
62
+    watermark_wrap('original_CGzC_10a50000c8811190.jpg', 'paiai_96_96.png')

adminSystem - Gogs: Go Git Service

Няма описание

.eslintrc 215B

    { "root": true, "extends": "@ljharb", "env": { "es6": true }, "rules": { "id-length": [2, { "max": 30 }], "max-statements": [2, 16], "no-magic-numbers": 0, "operator-linebreak": [2, "before"] } }