__init__.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. def loadFontSize(text, draw, image, fontpath):
  4. from PIL import ImageFont
  5. fontsize = 1 # starting font size
  6. img_fraction = (1,1) # fraction of screen covered
  7. font = ImageFont.truetype(fontpath, fontsize)
  8. fsize = (0,0)
  9. imgsize = (0.9,0.9)
  10. longline = getLongLine(text.splitlines())
  11. while True:
  12. font_last = font
  13. fontsize += 1
  14. font = ImageFont.truetype(fontpath, fontsize)
  15. fsize = draw.textsize(text, font=font)
  16. imgsize = (img_fraction[0]*image.size[0], img_fraction[1]*image.size[1])
  17. if (fsize[0] > imgsize[0] or fsize[1] > imgsize[1]):
  18. break
  19. print("Fontsize:", fontsize, fsize, imgsize)
  20. return font_last
  21. def wrapText(text):
  22. import textwrap
  23. splittext = []
  24. longline = ""
  25. iter_width = 500
  26. line_width_factor = 5
  27. while len(splittext)*line_width_factor < iter_width:
  28. iter_width -= 1
  29. splittext = textwrap.wrap(text, width=iter_width)
  30. longline = getLongLine(splittext)
  31. print("Wrap:", iter_width, len(splittext))
  32. return "\n".join(splittext), longline
  33. def getLongLine(split):
  34. longline = ""
  35. for l in split:
  36. if len(l) > len(longline):
  37. longline = l
  38. return longline
  39. def getQuote():
  40. try:
  41. import requests
  42. # r = requests.get('https://type.fit/api/quotes')
  43. r = requests.get('http://api.quotable.io/random?maxLength=200')
  44. r = r.json()
  45. text = r["content"] + " - " + r["author"]
  46. except Exception as ex:
  47. text = str(ex)
  48. return text