Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Social images #787

Merged
merged 17 commits into from
Mar 18, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Showcase both options
rneiss committed Mar 7, 2022
commit 1c42fbe2a6d39fb1a8b500647f1cbe9d64534ac4
3 changes: 2 additions & 1 deletion reader/views.py
Original file line number Diff line number Diff line change
@@ -1500,6 +1500,7 @@ def social_image_api(request, tref):
lang = request.GET.get("lang", "en")
version = request.GET.get("v", None)
platform = request.GET.get("platform", "twitter")
colored = request.GET.get("colored", None)
ref = Ref(tref)
ref_str = ref.normal() if lang == "en" else ref.he_normal()

@@ -1513,7 +1514,7 @@ def social_image_api(request, tref):

text = en if lang == "en" else he
text = ' '.join(text)
res = make_img_http_response(text, tf["primary_category"], ref_str, lang, platform)
res = make_img_http_response(text, tf["primary_category"], ref_str, lang, platform, colored)

return res

54 changes: 33 additions & 21 deletions sefaria/image_generator.py
Original file line number Diff line number Diff line change
@@ -73,9 +73,9 @@ def cleanup_and_format_text(text, language):
return text


def generate_image(text="", category="System", ref_str="", lang="he", platform="twitter"):
text_color = '#666666' # dark-grey
bg_color = (251, 251, 249) # lightest-grey
def generate_image(text="", category="System", ref_str="", lang="he", platform="twitter", colored=False):
text_color = '#fff' if colored else '#666666'# dark-grey
bg_color = palette[category] if colored else (251, 251, 249) # lightest-grey

font = ImageFont.truetype(font='static/fonts/Amiri-Taamey-Frank-merged.ttf', size=platforms[platform]["font_size"])
width = platforms[platform]["width"]
@@ -87,14 +87,14 @@ def generate_image(text="", category="System", ref_str="", lang="he", platform="

if lang == "en":
align = "left"
watermark_url = "static/img/logo.png"
logo_url = f"static/img/{'logo-white' if colored else 'logo' }.png"
spacing = 0
ref_font = ImageFont.truetype(font='static/fonts/Roboto-Regular.ttf', size=platforms[platform]["ref_font_size"])
cat_border_pos = (0, 0, 0, img.size[1])

else:
align = "right"
watermark_url = "static/img/logo-hebrew.png"
logo_url = f"static/img/{'logo-hebrew-white' if colored else 'logo-hebrew' }.png"
spacing = platforms[platform]["he_spacing"]
ref_font = ImageFont.truetype(font='static/fonts/Heebo-Regular.ttf', size=platforms[platform]["ref_font_size"])
cat_border_pos = (img.size[0], 0, img.size[0], img.size[1])
@@ -104,32 +104,44 @@ def generate_image(text="", category="System", ref_str="", lang="he", platform="
text = get_display(text) # Applies BIDI algorithm to text so that letters aren't reversed in PIL.

draw = ImageDraw.Draw(im=img)
draw.text(xy=(img.size[0] / 2, img.size[1] / 2), text=text, font=font, spacing=spacing, align=align, fill=text_color, anchor='mm')
draw.text(xy=(img.size[0] / 2, img.size[1] / 2), text=text, font=font, spacing=spacing, align=align,
fill=text_color, anchor='mm')

#category line
draw.line(cat_border_pos, fill=palette[category], width=int(width*.02))
if not colored:

#category line
draw.line(cat_border_pos, fill=palette[category], width=int(width*.02))

#header white
draw.line((0, int(height*.05), img.size[0], int(height*.05)), fill=(255, 255, 255), width=int(height*.1))
draw.line((0, int(height*.1), img.size[0], int(height*.1)), fill="#CCCCCC", width=int(height*.0025))

#write ref
draw.text(xy=(img.size[0] / 2, img.size[1]-padding_y/2), text=get_display(ref_str.upper()), font=ref_font, spacing=spacing, align=align, fill=text_color, anchor='mm')
#header white
draw.line((0, int(height*.05), img.size[0], int(height*.05)), fill=(255, 255, 255), width=int(height*.1))
draw.line((0, int(height*.1), img.size[0], int(height*.1)), fill="#CCCCCC", width=int(height*.0025))

#add sefaria logo
logo = Image.open(watermark_url)
logo.thumbnail((width, int(height*.06)))
logo_padded = Image.new('RGBA', (width, height))
logo_padded.paste(logo, (int(width/2-logo.size[0]/2), int(height*.05-logo.size[1]/2)))
#write ref
draw.text(xy=(img.size[0] / 2, img.size[1]-padding_y/2), text=get_display(ref_str.upper()), font=ref_font, spacing=spacing, align=align, fill=text_color, anchor='mm')

#add sefaria logo
logo = Image.open(logo_url)
logo.thumbnail((width, int(height*.06)))
logo_padded = Image.new('RGBA', (width, height))
logo_padded.paste(logo, (int(width/2-logo.size[0]/2), int(height*.05-logo.size[1]/2)))

img = Image.alpha_composite(img, logo_padded)

else:
watermark = Image.open(logo_url)
watermark.thumbnail((200, 200))
watermark_padded = Image.new('RGBA', (width, height))
watermark_pos = int(img.size[0]-(padding_x/2) - watermark.size[0]) if lang == "en" else int(padding_x/2)
watermark_padded.paste(watermark, (watermark_pos, int(height-watermark.size[1]- (padding_y/4))))
img = Image.alpha_composite(img, watermark_padded)

img = Image.alpha_composite(img, logo_padded)

buf = io.BytesIO()
img.save(buf, format='png')
return(buf.getvalue())

def make_img_http_response(text, category, ref_str, lang, platform):
img = generate_image(text, category, ref_str, lang, platform)
def make_img_http_response(text, category, ref_str, lang, platform, colored):
img = generate_image(text, category, ref_str, lang, platform, colored)
res = HttpResponse(img, content_type="image/png")
return res