- default_storage.save(photo_path, photo)
+ if default_storage.exists(m_photo_path):
+ default_storage.delete(m_photo_path)
+ default_storage.save(m_photo_path, photo)
+
+ p_photo_path = 'photo/{photo_id}_p{extension}'.format(photo_id=photo_id, extension=extension)
+ watermark_wrap(
+ os.path.join(settings.MEDIA_ROOT, m_photo_path).replace('\\', '/'),
+ settings.WATERMARK_LOGO,
+ os.path.join(settings.MEDIA_ROOT, p_photo_path).replace('\\', '/')
+ )
photo, created = PhotosInfo.objects.get_or_create(
lensman_id=lensman_id,
session_id=session_id,
photo_id=photo_id,
- photo_name=photo_name,
- photo_path=photo_path
+ p_photo_path=p_photo_path,
+ m_photo_path=m_photo_path,
)
return JsonResponse({
@@ -113,22 +120,22 @@ def session_detail(request, session):
def photo_standard(request, photo):
photo = PhotosInfo.objects.get(photo_id=photo)
- return render(request, 'photo/photo_detail.html', {'photo_url': photo.photo_url})
+ return render(request, 'photo/photo_detail.html', {'photo_url': photo.p_photo_url})
def photo_medium(request, photo):
photo = PhotosInfo.objects.get(photo_id=photo)
- return render(request, 'photo/photo_detail.html', {'photo_url': photo.photo_url})
+ return render(request, 'photo/photo_detail.html', {'photo_url': photo.m_photo_url})
def photo_large(request, photo):
photo = PhotosInfo.objects.get(photo_id=photo)
- return render(request, 'photo/photo_detail.html', {'photo_url': photo.photo_url})
+ return render(request, 'photo/photo_detail.html', {'photo_url': photo.l_photo_url})
def photo_raw(request, photo):
photo = PhotosInfo.objects.get(photo_id=photo)
- return render(request, 'photo/photo_detail.html', {'photo_url': photo.photo_url})
+ return render(request, 'photo/photo_detail.html', {'photo_url': photo.r_photo_url})
class PhotoInfoViewSet(viewsets.ModelViewSet):
@@ -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')
|