label_image.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import sys
  2. import os
  3. from PIL import Image, ImageDraw, ImageFont
  4. import tempfile
  5. class Label():
  6. pixels_x = 155
  7. pixels_y = 106
  8. bg_color = (255, 255, 255)
  9. def __init__(self, skip_create = False):
  10. me_path = os.path.dirname(__file__)
  11. self.bold_font_fname = os.path.join(me_path, 'OpenSans-Bold.ttf')
  12. self.regular_font_fname = os.path.join(me_path, 'OpenSans-Regular.ttf')
  13. if not skip_create:
  14. self.create()
  15. def create(self):
  16. self.img = Image.new('RGB', (self.pixels_x, self.pixels_y), color=self.bg_color)
  17. def draw_text(self, text : str, pos_x, pos_y, color=(0,0,0), size=10, font_file=None, centered_x=False, centered_y=False, scale_to_fit=False):
  18. d = ImageDraw.Draw(self.img)
  19. if font_file is None or font_file == 'regular':
  20. font_file = self.regular_font_fname
  21. elif font_file == 'bold':
  22. font_file = self.bold_font_fname
  23. font_fits = False
  24. orig_size = size
  25. orig_pos_x = pos_x
  26. orig_pos_y = pos_y
  27. while not font_fits:
  28. fnt = ImageFont.truetype(font_file, size)
  29. w, h = d.textsize(text, font=fnt)
  30. if centered_x:
  31. pos_x = orig_pos_x - w / 2
  32. else:
  33. pos_x = orig_pos_x
  34. if centered_y:
  35. pos_y = orig_pos_y - h / 2
  36. else:
  37. pos_y = orig_pos_y
  38. if not scale_to_fit:
  39. font_fits = True
  40. break
  41. if pos_x >= 0 and pos_x + w <= self.pixels_x:
  42. font_fits = True
  43. else:
  44. size = size - 1
  45. if size != orig_size:
  46. print('Rescaled font to size:', size)
  47. d.text((pos_x, pos_y), text, font=fnt, fill=color)
  48. def save(self, fname = None):
  49. """
  50. Save Label to file. If no fname is supplied, a tempfile is created and its filename is returend
  51. """
  52. if self.img is None:
  53. raise Exception('Must create image first')
  54. if fname is None or fname == '':
  55. fobj = tempfile.mkstemp(suffix='.png', prefix='label_', text=False)
  56. with os.fdopen(fobj[0], mode="w+b") as file:
  57. self.img.save(file, format='PNG')
  58. fname = fobj[1]
  59. else:
  60. self.img.save(fname)
  61. return fname
  62. def get_pillow_image(self):
  63. return self.img
  64. class MiceToiletLabel(Label):
  65. pixels_x = 155
  66. pixels_y = 106
  67. def __init__(self):
  68. super().__init__(skip_create=True)
  69. self.create()
  70. def put_text(self, heading, line1=None, line2=None):
  71. self.draw_text(heading, self.pixels_x/2, 20, size=25, font_file='bold', centered_x=True, centered_y=True, scale_to_fit=True)
  72. if line1:
  73. self.draw_text(line1, self.pixels_x/2, 55, size=20, centered_x=True, centered_y=True, scale_to_fit=True)
  74. if line2:
  75. self.draw_text(line2, self.pixels_x/2, 85, size=20, centered_x=True, centered_y=True, scale_to_fit=True)