cardgenerator.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import os
  2. import PIL
  3. from PIL import Image
  4. from PIL import ImageDraw
  5. from PIL import ImageFont
  6. import textwrap
  7. CHARS_PER_LINE = 15
  8. FONT_PATH = "LiberationSerif-Bold.ttf"
  9. BODY_SIZE = 50
  10. FOOTER_SIZE = 18
  11. class CardGenerator(object):
  12. def __init__(self, config):
  13. self.save_folder = config["save_folder"]
  14. self.color = config["color"]
  15. self.card_dimensions = config["card_dimensions_px"]
  16. self.footer_text = config["footer_text"]
  17. self.index = 0
  18. self.padding = config["card_padding_px"]
  19. super(CardGenerator, self).__init__()
  20. @property
  21. def background_color(self):
  22. if self.color == "white":
  23. return (255, 255, 255, 255)
  24. else:
  25. return (0, 0, 0, 255)
  26. @property
  27. def foreground_color(self):
  28. if self.color == "white":
  29. return (0, 0, 0, 255)
  30. else:
  31. return (255, 255, 255, 255)
  32. def makeCard(self, text):
  33. card = Image.new("RGBA", (self.card_dimensions[0], self.card_dimensions[1]), self.background_color)
  34. draw = ImageDraw.Draw(card)
  35. # draw words
  36. body_font = ImageFont.truetype(FONT_PATH, BODY_SIZE)
  37. offset = self.padding
  38. for line in textwrap.wrap(text, width=CHARS_PER_LINE):
  39. draw.text((self.padding, offset), line, font=body_font, fill=self.foreground_color)
  40. offset += body_font.getsize(line)[1]
  41. # draw footer
  42. footer_font = ImageFont.truetype(FONT_PATH, FOOTER_SIZE)
  43. footer_img = Image.open('input/logo.png','r')
  44. footer_img_offset = self.card_dimensions[1] - self.padding - footer_img.size[1] / 2 - footer_font.getsize(self.footer_text)[1] / 2
  45. card.paste(footer_img, (int(self.padding), int(footer_img_offset)), footer_img)
  46. footer_text_left_offset = self.padding + footer_img.size[0] + self.padding / 3
  47. footer_text_top_offset = self.card_dimensions[1] - self.padding - footer_font.getsize(line)[1]
  48. draw.text((footer_text_left_offset, footer_text_top_offset), self.footer_text, font=footer_font, fill=self.foreground_color)
  49. return card
  50. def writeCard(self, words):
  51. filename = u"{}.{}.png".format(self.index, self.color)
  52. self.index += 1
  53. card = self.makeCard(words)
  54. card.save(os.path.join(self.save_folder, filename))
  55. def makeBack(self):
  56. card = Image.new("RGBA", (self.card_dimensions[0], self.card_dimensions[1]), self.background_color)
  57. draw = ImageDraw.Draw(card)
  58. # draw words
  59. body_font = ImageFont.truetype(FONT_PATH, BODY_SIZE * 2)
  60. offset = self.padding
  61. for line in self.footer_text.split(" "):
  62. draw.text((self.padding, offset), line, font=body_font, fill=self.foreground_color)
  63. offset += body_font.getsize(line)[1] + 24
  64. return card
  65. def writeBack(self):
  66. card = self.makeBack()
  67. card.save(os.path.join(self.save_folder, "back.{}.png".format(self.color)))