Created
June 26, 2020 14:17
-
-
Save kjaymiller/9157c578b330e67e4092f0ea8fde0547 to your computer and use it in GitHub Desktop.
Processing Images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import ImageDraw, ImageFont, Image | |
import episode_dl | |
import re | |
feed = '<Podcast Feed>' | |
pod = episode_dl.get_episodes(feed) | |
# Custom Code for splitting the text so that it fits on the image | |
# Play around with the base_n | |
for i, episode in enumerate(pod[::-1]): | |
index = i + 2 | |
raw_title = episode.title.replace('/','') | |
t = re.split(' with guest', raw_title, maxsplit=1, flags=re.I) | |
base_n = 25 | |
if len(t) == 1 and len(raw_title) > base_n: | |
n = base_n | |
while (raw_title[n] not in [' ', '.', '?']): | |
n -= 1 | |
else: | |
indices = [0, n + 1, len(raw_title)] # the + 1 ensures a clean word | |
t = [raw_title[i:j] for i,j in zip(indices, indices[1:]+[None])] | |
title = ('\n').join(t) | |
else: | |
title = ('\nwith guest').join(t) | |
# for twitter image | |
with Image.open('<IMAGE>') as im: | |
font = ImageFont.truetype('Roboto-Bold.ttf', 45) | |
font2 = ImageFont.truetype('Roboto-Regular.ttf', 35) | |
d = ImageDraw.Draw(im) | |
d.text( | |
(im.width/2 - 25, im.height/4), | |
f'Episode {index}:', | |
font=font, | |
fill=(0,0,0), | |
) | |
d.text( | |
(im.width/2, im.height/4 + 65), | |
title, | |
font=font2, | |
fill=(0,0,0), | |
) | |
clean_text = raw_title.replace('.', '') | |
im.save(f'twitter/{index}-{title}.png', 'PNG') | |
# for linked-in images | |
with Image.open('<linkedin-image>') as im: | |
font = ImageFont.truetype('Roboto-Bold.ttf', 55) | |
font2 = ImageFont.truetype('Roboto-Regular.ttf', 40) | |
d = ImageDraw.Draw(im) | |
d.text( | |
(im.width/2 - 25, im.height/4), | |
f'Episode {index}:', | |
font=font, | |
fill=(0,0,0), | |
) | |
d.text( | |
(im.width/2, im.height/4 + 65), | |
title, | |
font=font2, | |
fill=(0,0,0), | |
) | |
clean_text = raw_title.replace('.', '') | |
im.save(f'linkedin/{index}-{title}-linkedin.png', 'PNG') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment