code nl-5 ol-5">
+ # 有水印
if watermark:
if not photo.photo_watermark_path:
photo_watermark_path = 'photo/{}{}'.format(shortuuid.uuid(), ext)
@@ -80,6 +80,11 @@ def file_save(file_=None, file_path=None, prefix='img', ext='jpeg', watermark=Fa
os.path.join(settings.MEDIA_ROOT, photo_thumbnail2_path).replace('\\', '/'),
settings.THUMBNAIL_MAX_WIDTH2
)
+ watermark_wrap(
+ os.path.join(settings.MEDIA_ROOT, photo_thumbnail2_path).replace('\\', '/'),
+ settings.WATERMARK_LOGO,
+ os.path.join(settings.MEDIA_ROOT, photo_thumbnail2_path).replace('\\', '/')
+ )
photo.photo_w = photo_w
photo.photo_h = photo_h
photo.photo_thumbnail2_path = photo_thumbnail2_path
@@ -17,46 +17,49 @@ def reduce_opacity(im, opacity): |
||
| 17 | 17 |
return im |
| 18 | 18 |
|
| 19 | 19 |
|
| 20 |
-def watermark(im, mark, position, opacity=1): |
|
| 21 |
- """Adds a watermark to an image.""" |
|
| 20 |
+def watermark(im, mark, position, opacity=1, maxsize=(0, 0), possize=(0, 0)): |
|
| 21 |
+ """ Add watermark to image """ |
|
| 22 | 22 |
if opacity < 1: |
| 23 | 23 |
mark = reduce_opacity(mark, opacity) |
| 24 | 24 |
if im.mode != 'RGBA': |
| 25 | 25 |
im = im.convert('RGBA')
|
| 26 |
- # create a transparent layer the size of the image and draw the |
|
| 27 |
- # watermark in that layer. |
|
| 26 |
+ # Resize mark |
|
| 27 |
+ w, h = int(min(mark.size[0], maxsize[0]) if maxsize[0] else mark.size[0]), int(min(mark.size[1], maxsize[1]) if maxsize[1] else mark.size[1]) |
|
| 28 |
+ mark = mark.resize((w, h)) |
|
| 29 |
+ # Create a transparent layer the size of the image |
|
| 30 |
+ # Draw the watermark in that layer. |
|
| 28 | 31 |
layer = Image.new('RGBA', im.size, (0, 0, 0, 0))
|
| 29 | 32 |
if position == 'tile': |
| 30 | 33 |
for y in range(0, im.size[1], mark.size[1]): |
| 31 | 34 |
for x in range(0, im.size[0], mark.size[0]): |
| 32 | 35 |
layer.paste(mark, (x, y)) |
| 33 | 36 |
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) |
|
| 37 |
+ # Scale, but preserve the aspect ratio |
|
| 38 |
+ ratio = min(float(im.size[0]) / mark.size[0], float(im.size[1]) / mark.size[1]) |
|
| 39 |
+ w, h = int(mark.size[0] * ratio), int(mark.size[1] * ratio) |
|
| 40 |
+ w, h = int(min(w, maxsize[0]) if maxsize[0] else w), int(min(h, maxsize[1]) if maxsize[1] else h) |
|
| 39 | 41 |
mark = mark.resize((w, h)) |
| 40 |
- layer.paste(mark, ((im.size[0] - w) / 2, (im.size[1] - h) / 2)) |
|
| 42 |
+ layer.paste(mark, ((im.size[0] - possize[0] or w) / 2, (im.size[1] - possize[1] or h) / 2)) |
|
| 41 | 43 |
else: |
| 42 | 44 |
layer.paste(mark, position) |
| 43 |
- # composite the watermark with the layer |
|
| 45 |
+ # Composite the watermark with the layer |
|
| 44 | 46 |
return Image.composite(layer, im, layer) |
| 45 | 47 |
|
| 46 | 48 |
|
| 47 | 49 |
def watermark_wrap(im_path, mark_path, save_path=''): |
| 48 | 50 |
im, mark = Image.open(im_path), Image.open(mark_path) |
| 49 |
- new_im = watermark(im, mark, (50, 50), 0.5) |
|
| 51 |
+ # new_im = watermark(im, mark, (50, 50), 0.5) |
|
| 52 |
+ new_im = watermark(im, mark, position='scale', opacity=1.0, maxsize=(400, 494.375), possize=(400, 400)) |
|
| 50 | 53 |
new_im.save(save_path or im_path) |
| 51 | 54 |
|
| 52 | 55 |
|
| 53 | 56 |
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() |
|
| 57 |
+ im, mark = Image.open('original_CGzC_10a50000c8811190.jpg'), Image.open('paiai_water_mark.png')
|
|
| 58 |
+ watermark(im, mark, position='tile', opacity=0.5, maxsize=(40, 49.4375)).show() |
|
| 59 |
+ watermark(im, mark, position='scale', opacity=1.0, maxsize=(400, 494.375), possize=(400, 400)).show() |
|
| 60 |
+ watermark(im, mark, position=(50, 50), opacity=0.5, maxsize=(40, 49.4375)).show() |
|
| 58 | 61 |
|
| 59 | 62 |
|
| 60 | 63 |
if __name__ == '__main__': |
| 61 | 64 |
# watermark_test() |
| 62 |
- watermark_wrap('original_CGzC_10a50000c8811190.jpg', 'paiai_96_96.png')
|
|
| 65 |
+ watermark_wrap('original_CGzC_10a50000c8811190.jpg', 'paiai_water_mark.png')
|