Skip to content

Instantly share code, notes, and snippets.

@thisismattmiller
Created May 22, 2017 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thisismattmiller/f7a4e92e7576cf42c8b44e773d23c87f to your computer and use it in GitHub Desktop.
Save thisismattmiller/f7a4e92e7576cf42c8b44e773d23c87f to your computer and use it in GitHub Desktop.
from PIL import Image, ImageDraw, ImageFont
import math, json, sys
count = 0
maxTextWidth = 500
font = ImageFont.truetype('NotoSans-Regular.ttf', 10)
height = 557017 + 9
width = maxTextWidth * 160
print(height,width)
canvas = Image.new('RGBA', (width,height), (255,255,255,255))
draw = ImageDraw.Draw(canvas)
currentCol = 0
currentHeight = 0
titlesPerCol = 0
for line in open('all.txt'):
line = line.strip()
if line == '':
continue
#if it is less then 95 chars then it is likely not longer than 500px
if len(line) <= 95:
fontWidth, fontHeight = 500, 10
else:
fontWidth, fontHeight = font.getsize(line)
if fontWidth > maxTextWidth:
newLine = line
while fontWidth > maxTextWidth:
newLine = newLine[:-2]
fontWidth, fontHeight = font.getsize(newLine + '...')
if newLine != line:
line = newLine+'...'
#force 10 px
fontHeight = 10
draw.text((currentCol, currentHeight), text=line, font=font, fill=(0, 0, 0,200))
titlesPerCol = titlesPerCol + 1
currentHeight = currentHeight + fontHeight
if (currentHeight + 10 >height):
currentHeight = 0
currentCol = currentCol + maxTextWidth
print("Fit",titlesPerCol,"lines into this col #", currentCol/maxTextWidth )
titlesPerCol = 0
count += 1
print(count, end="\r")
print("writing out file")
canvas.save('all.png', "PNG")
print(count, " total")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment