2

I want to render a string like this ‘3/4’ into a maths fraction.

I can do it by rendering an HTML page and using Mathjax, but that seems a little excessive. Any thoughts much appreciated.

stevo78
  • 21
  • 2
  • Welcome to SO! We cannot write a full guide to such a broad question as a serious answer here. You should show your attempts at (starting to) solve the problems and ask specific questions about where you are stuck – Ofer Sadan Nov 03 '19 at 07:08

1 Answers1

3

enter image description here

#!/usr/bin/env python3

from PIL import Image, ImageDraw, ImageFont

text1 = u"Unicode maybe? \u00be \u2153 \u2157"
text2 = u"Maths? \u222c \u221e \u220f \u221a"

im  =  Image.new ( "RGB", (400,100), 0 )
draw  =  ImageDraw.Draw ( im )
unifont = ImageFont.truetype("/System/Library/Fonts//Menlo.ttc", 32)
draw.text((10,10), text1, font=unifont, fill=(255,255,255))
draw.text((10,55), text2, font=unifont, fill=(255,255,255))

# Save result
im.save("text.png")

Just be sure to use a Unicode font - such as DejaVu.ttf I used /System/Library/Fonts//Menlo.ttc because I am on a Mac.

I looked up the codes here.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks, but I mean I want it to look good like mathJax and I need to concatenate it with another PNG image. Also I want it to take a raw string and convert that automatically,, like mathsJax does – stevo78 Nov 03 '19 at 09:44
  • Aha, a bit more than the question implies then :-) Maybe something like this... https://stackoverflow.com/a/37755133/2836621 – Mark Setchell Nov 03 '19 at 10:03