Skip to content

Commit

Permalink
Add transparency to the watermark
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Jun 2, 2018
1 parent e0fd118 commit 083beef
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 10 deletions.
30 changes: 21 additions & 9 deletions memegen/domain/image.py
Expand Up @@ -132,19 +132,20 @@ def _generate(top, bottom, font_path, background, width, height,
outline = int(round(max(1, min(dimensions) / 300)))
t_outline = min(outline, top_font_size // 20)
b_outline = min(outline, bottom_font_size // 20)
_draw_outlined_text(draw, top_text_pos, top, top_font, t_outline)
_draw_outlined_text(draw, bottom_text_pos, bottom, bottom_font, b_outline)
_draw_outlined_text(image, top_text_pos, top, top_font, t_outline)
_draw_outlined_text(image, bottom_text_pos, bottom, bottom_font, b_outline)

# Pad image if a specific dimension is requested
if pad_image:
image = _add_blurred_background(image, background_image, width, height)

# Add watermark
if watermark:
draw = ImageDraw.Draw(image)
# draw = ImageDraw.Draw(image)
watermark_font = ImageFont.truetype(watermark_font_path, 11)
watermark_pos = (3, image.size[1] - 15)
_draw_outlined_text(draw, watermark_pos, watermark, watermark_font)
_draw_outlined_text(image, watermark_pos, watermark, watermark_font,
alpha=True)

return image

Expand Down Expand Up @@ -173,19 +174,30 @@ def _optimize_font_size(font, text, max_font_size, min_font_size,
return font_size, text


def _draw_outlined_text(draw_image, text_pos, text, font, width=1):
def _draw_outlined_text(image, text_pos, text, font, width=1, alpha=False):
"""Draw white text with black outline on an image."""
if alpha:
black_alpha = 170
white_alpha = 128
else:
black_alpha = 255
white_alpha = 240

overlay = ImageFile.new('RGBA', image.size)
draw = ImageDraw.ImageDraw(overlay, 'RGBA')

# Draw black text outlines
for x in range(-width, 1 + width):
for y in range(-width, 1 + width):
pos = (text_pos[0] + x, text_pos[1] + y)
draw_image.multiline_text(pos, text, (0, 0, 0),
font=font, align='center')
draw.multiline_text(pos, text, (0, 0, 0, black_alpha),
font=font, align='center')

# Draw inner white text
draw_image.multiline_text(text_pos, text, (255, 255, 255),
font=font, align='center')
draw.multiline_text(text_pos, text, (255, 255, 255, white_alpha),
font=font, align='center')

image.paste(overlay, None, overlay)


def _add_blurred_background(foreground, background, width, height):
Expand Down
2 changes: 1 addition & 1 deletion memegen/templates/examples.html
Expand Up @@ -8,7 +8,7 @@
{% for img in sample_images %}
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 col-xl-2">
<a href="{{ img.url }}&share=true">
<img class="meme-img img-preview" src="{{ img.url }}" />
<img class="meme-img" src="{{ img.url }}" />
</a>
</div>
{% endfor %}
Expand Down
16 changes: 16 additions & 0 deletions pytest.ini
@@ -0,0 +1,16 @@
[pytest]

addopts =
--strict

--verbose --verbose
-r sxX

--cov=memegen
--cov-report=html
--cov-report=term-missing:skip-covered
--no-cov-on-fail

cache_dir = .cache

markers =
Binary file modified tests/examples/font-impact.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/font-notosans.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/size-both.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/size-height.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/size-large.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/size-width.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/text-long.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/text-nominal.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/text-short-on-tall.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/text-short.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/text-subscripts.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/watermark-pad-h.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/watermark-pad-v.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/examples/watermark.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 083beef

Please sign in to comment.