Skip to content

Instantly share code, notes, and snippets.

@noqqe
Created December 5, 2018 11:14
Show Gist options
  • Save noqqe/5e6ce0e196e8a8722e2c5c8f3855bc3c to your computer and use it in GitHub Desktop.
Save noqqe/5e6ce0e196e8a8722e2c5c8f3855bc3c to your computer and use it in GitHub Desktop.
instabehrliches.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import html
import feedparser
import datetime
import random
import urllib.request
from PIL import Image, ImageDraw, ImageFont
from unsplash.api import Api
from unsplash.auth import Auth
# unsplash auth
client_id = "xxx"
client_secret = "xxx"
redirect_uri = "xxx"
code = ""
# settings
feedurl = '../entbehrliches/public/index.xml'
unsplash_keywords = [ 'library', 'knowledge', 'learning', 'read', 'books' ]
title_font = ImageFont.truetype('Roboto-Bold.ttf', size=45)
text_font = ImageFont.truetype('Roboto-Light.ttf', size=40)
basewidth = 1080
def search_image(query, title):
log('Picking random image with %s as query' % query)
auth = Auth(client_id, client_secret, redirect_uri, code=code)
api = Api(auth)
photo = api.photo.random(query=query, count=1)[0]
url = photo.urls.raw
log('Downloading the image from %s to %s' % (url, fname))
urllib.request.urlretrieve(url, fname)
return fname
def open_image(fname):
log('Opening image %s' % fname)
image = Image.open(fname)
return image
def log(text):
d = datetime.datetime.now().strftime('%H:%M:%S ')
print(d + text)
def text_wrap(text, font, max_width):
log('Linewrapping description text')
lines = []
if font.getsize(text)[0] <= max_width:
lines.append(text)
else:
words = text.split(' ')
i = 0
while i < len(words):
line = ''
while i < len(words) and font.getsize(line + words[i])[0] <= max_width:
line = line + words[i] + " "
i += 1
if not line:
line = words[i]
i += 1
lines.append(line)
return lines
# Resize image to smaller size
def resize_image(image):
wpercent = (basewidth/float(image.size[0]))
hsize = int((float(image.size[1])*float(wpercent)))
log("Resizing image to %sx%s" % (basewidth, hsize))
image = image.resize((basewidth,hsize), Image.ANTIALIAS)
return image
def draw_title(image):
log('Drawing title')
# Draw title
text_size = title_font.getsize(title)
button_size = (text_size[0]+20, text_size[1]+20)
button_img = Image.new('RGBA', button_size, "gray")
button_draw = ImageDraw.Draw(button_img)
button_draw.text((10, 10), title, font=title_font)
image.paste(button_img, (100, 100))
return image
def draw_text(image):
log('Drawing description text')
# Draw text
w, _ = image.size
lines = text_wrap(text, text_font, w - 250)
line_height = text_font.getsize('hg')[1]
line_length = len(max(lines, key=len))
log('Placing text box with %s width and %s height' % (str(w-130), str(line_height + 20)))
button_size = (w - 230, len(lines) * line_height + 20)
button_img = Image.new('RGBA', button_size, "gray")
button_draw = ImageDraw.Draw(button_img)
x = 10
y = 10
for line in lines:
button_draw.text((x, y), line, fill="white", font=text_font)
y = y + line_height
image.paste(button_img, (100, 200))
return image
# Wrapper for single image
def generate_image(title, fname, text):
fname = search_image(random.choice(unsplash_keywords), title)
image = open_image(fname)
image = resize_image(image)
draw_title(image)
draw_text(image)
image.save(fname, quality=99)
articles = feedparser.parse(feedurl)
for article in articles['entries']:
title = article['title']
desc = article['summary']
desc = desc.split('\n')[0]
desc = desc.replace('\n','')
text = html.unescape(desc)
fname = 'out/' + title.replace(' ', '-').replace('---', '-').replace(')', '').replace('(', '') + '.jpg'
if not os.path.exists(fname):
log("Generating image for %s" % title)
generate_image(title,fname, text)
else:
log("Skipping image for %s as its already there" % title)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment