Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save earthbound19/9578435 to your computer and use it in GitHub Desktop.
Save earthbound19/9578435 to your computer and use it in GitHub Desktop.
Render a horizontal series of multiples of a number, each centered along a grid width, with a font of your choice.
#See "TO CHANGE THE OUTPUT IMAGE, CHANGE THESE VARIABLES," below.
#Render a series of string multiples of a number, each horizontally centered along a tile-length grid; using a font of our choice.
#Started 2014-03-15 (Sat Mar 15) 04.53.40 PM RAH
#Horked and extended from:
#http://stackoverflow.com/questions/15857117/python-pil-text-to-image-and-fonts
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import math
import os
#TO CHANGE THE OUTPUT IMAGE, CHANGE THESE VARIABLES:
hoopDiameter = 2.20
hoopHeight = .8
dotsPerInch = 300
fontSize = 20.3
numberOfTiles = 12
renderNumStep = 400
fontFileName = "UniversLTStd-BoldCn.otf"
#Prepare variables.
#We want the font to scale up if the dots per inch scale up, so use Percent Change formula + 1 to get a multiplier (with 72 DPI as the "change" reference in the equation).
fontSizeScalar = int(1+(dotsPerInch - 72) / 72)
fontSize = int(fontSize * fontSizeScalar)
font = ImageFont.truetype(fontFileName,fontSize)
renderNum = 0
ImageW = int(hoopDiameter * math.pi * dotsPerInch)
ImageTileWidth = ImageW / numberOfTiles
ImageH = int(hoopHeight * dotsPerInch)
img=Image.new("RGBA", (ImageW,ImageH),(255,255,255))
draw = ImageDraw.Draw(img)
#Create the image series at proper coordinates, each properly laid out, in memory.
#TO DO: Start with -2 to "cluge" two words at the scale start.
i = numberOfTiles + 1
for j in range (1, i):
textW, textH = draw.textsize(str(j * renderNumStep), font)
textW = (ImageTileWidth/2) - (textW/2) + renderNum
renderNum += ImageTileWidth
textH = (ImageH/2) - (textH/2)
draw.text((textW, textH),str(renderNumStep * j),(0,0,0),font)
#Add to a composite from the image we created in this iteration of the loop.
draw = ImageDraw.Draw(img)
#Save the resultant image.
img.save("scale.png")
#The next line is only for previewing during production; comment it out otherwise.
os.system("scale.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment