Skip to content

Instantly share code, notes, and snippets.

@orjanv
Last active November 14, 2020 19:25
Show Gist options
  • Select an option

  • Save orjanv/e513fb2f1bf6ff96b71b87519d595a79 to your computer and use it in GitHub Desktop.

Select an option

Save orjanv/e513fb2f1bf6ff96b71b87519d595a79 to your computer and use it in GitHub Desktop.
Create good looking instagram image based on a wordpress post
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
from bs4 import BeautifulSoup
import requests
from datetime import datetime, date
from PIL import Image, ImageDraw, ImageFont, ImageOps
import textwrap
import sys
# get post url as a parameter
url = sys.argv[1]
# Trekk ut all informasjon, tittel, url, dato og bilde
response = requests.get(url)
webpage = BeautifulSoup(response.text, "html.parser")
meta_title = webpage.findAll(attrs={"property":"og:title"})
og_title = meta_title[0]['content']
meta_time = webpage.findAll(attrs={"property":"article:published_time"})
published_time = meta_time[0]['content']
# First convert your string to a datetime object using strptime, then convert this object to the required string format with strftime:
test_date = datetime.strftime(datetime.strptime(published_time,'%Y-%m-%dT%H:%M:%S%z'),'%-d. %B %Y')
meta_image = webpage.findAll(attrs={"property":"og:image"})
imageurl = meta_image[0]['content']
bigimageurl = imageurl.rsplit("-",1)[0]
bigimageurl = bigimageurl+".jpg"
# Last ned bilde og lag kvadratisk versjon på 1080x1080
with open('moon-temp-image.jpg', 'wb') as handle:
response = requests.get(bigimageurl, stream=True)
if not response.ok:
print(response)
for block in response.iter_content(1024):
if not block:
break
handle.write(block)
im = Image.open('moon-temp-image.jpg')
width, height = im.size
left = (width - 1080)/2
top = (height - 1080)/2
right = (width + 1080)/2
bottom = (height + 1080)/2
im1 = im.crop((left, top, right, bottom))
# legg på informasjon om tittel og dato og blog.hoyd.net/new post-tekst
draw = ImageDraw.Draw(im1, 'RGBA')
font_title = ImageFont.truetype('fonts/Roboto-Medium.ttf', size=60, encoding="utf-8")
font_head = ImageFont.truetype('fonts/Roboto-Bold.ttf', size=70, encoding="utf-8")
font_date = ImageFont.truetype('fonts/Roboto-Regular.ttf', size=40, encoding="utf-8")
# draw text on base
color_title = 'rgb(255, 255, 255)'
color_head = 'rgb(255, 255, 255)'
color_date = 'rgb(255, 255, 255)'
# Draw boxes on base
draw.rectangle([0, 720, 1080, 1020], fill = (103,144,73,170)) # transparent box at bottom
draw.rectangle([0, 720, 20, 1020], fill ="white") # left bar at bottom
draw.rectangle([620, 60, 1080, 160], fill = (103,144,73,170)) # transparent box at top right
draw.rectangle([1060, 60, 1080, 160], fill ="white") # left bar at top
# Draw text multiple places
test = "\n".join(textwrap.wrap(og_title, width=35))
draw.text((80, 750), test.upper(), fill=color_title, font=font_title)
draw.text((80, 940), '%s - read @ blog.hoyd.net' % (test_date.lower()), fill=color_date, font=font_date)
draw.text((660, 70), "NEW POST", fill=color_head, font=font_head)
# save the edited image
test_filename = '%s.jpg' % (og_title)
im1.save(test_filename, optimize=True, quality=80)
# poste bilde på instagram og legg ved tekst (tittel og standardtekst) og link in bio-tekst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment