Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _build_texts(self, image):
"""Draw texts on a PIL image (PIL is used instead of OpenCV
because it is able to draw any fonts without ext).
:param image: PIL.Image instance
:type image: object
"""
offset_generator = self._iter_texts_rects()
draw = ImageDraw.Draw(image)
for text, font_name, color, align in self._texts:
text_x, text_y, max_width, max_height = next(offset_generator)
if not text: # Empty string: go to next text position
continue
# Use PIL to draw text because better support for fonts than OpenCV
font = fonts.get_pil_font(text, font_name, max_width, max_height)
_, text_height = font.getsize(text)
(text_width, _baseline), (offset_x, offset_y) = font.font.getsize(text)
if align == self.CENTER:
text_x += (max_width - text_width) // 2
elif align == self.RIGHT:
text_x += (max_width - text_width)
draw.text((text_x - offset_x // 2,
text_y + (max_height - text_height) // 2 - offset_y // 2),
text, color, font=font)
def build_overlay(self, size, text, alpha):
"""Return a PIL image with the given text that can be used
as an overlay for the camera.
"""
image = Image.new('RGBA', size)
draw = ImageDraw.Draw(image)
font = fonts.get_pil_font(text, fonts.CURRENT, 0.9 * size[0], 0.9 * size[1])
txt_width, txt_height = draw.textsize(text, font=font)
position = ((size[0] - txt_width) // 2, (size[1] - txt_height) // 2 - size[1] // 10)
draw.text(position, text, (255, 255, 255, alpha), font=font)
return image
:type layout_number: int
:param size: maximum size of the layout surface
:type size: tuple
:return: surface
:rtype: :py:class:`pygame.Surface`
"""
layout_image = get_pygame_image("layout{0}.png".format(layout_number),
size, color=text_color, bg_color=bg_color)
text = language.get_translated_text(str(layout_number))
if text:
rect = layout_image.get_rect()
rect = pygame.Rect(rect.x + rect.width * 0.3 / 2,
rect.y + rect.height * 0.76,
rect.width * 0.7, rect.height * 0.20)
text_font = fonts.get_pygame_font(text, fonts.CURRENT, rect.width, rect.height)
surface = text_font.render(text, True, bg_color)
layout_image.blit(surface, surface.get_rect(center=rect.center))
return layout_image
"""
if not self._print_number and not self._print_failure:
return # Dont show counter: no file in queue, no failure
smaller = self.surface.get_size()[1] if self.surface.get_size(
)[1] < self.surface.get_size()[0] else self.surface.get_size()[0]
side = int(smaller * 0.05) # 5% of the window
if side > 0:
if self._print_failure:
image = pictures.get_pygame_image('printer_failure.png', (side, side), color=self.text_color)
else:
image = pictures.get_pygame_image('printer.png', (side, side), color=self.text_color)
y = self.surface.get_rect().height - image.get_rect().height - 10
self.surface.blit(image, (10, y))
font = pygame.font.Font(fonts.CURRENT, side)
label = font.render(str(self._print_number), True, self.text_color)
self.surface.blit(label, (side + 20, y))
The ``align`` parameter can be one of:
* top-left
* top-center
* top-right
* center-left
* center
* center-right
* bottom-left
* bottom-center
* bottom-right
"""
surfaces = []
lines = text.splitlines()
font = fonts.get_pygame_font(max(lines, key=len), fonts.CURRENT,
rect.width, rect.height / len(lines))
for i, line in enumerate(lines):
surface = font.render(line, True, color)
if align.endswith('left'):
x = rect.left
elif align.endswith('center'):
x = rect.centerx - surface.get_rect().width / 2
elif align.endswith('right'):
x = rect.right - surface.get_rect().width / 2
else:
raise ValueError("Invalid horizontal alignment '{}'".format(align))
height = surface.get_rect().height
if align.startswith('top'):
y = rect.top + i * height