in src/spg/utils/imageUtils.py [0:0]
def add_text_to_image_centre(buffer, text, font_size=None, font_path=None, text_color=(0, 0, 0),
x_pos_override=None, y_pos_override=None):
""" Adds the given text to the centre of the image, if not font size provided we estimate 80 % of the average height
and width to try and fill as much of the image as possible.
:param buffer: ImageBuf we want to add the text too
:param text: The text we want to display on the image
:param font_size: The size of the font
:param font_path: The filepath to the font we want to use
:param text_color: The color of the text we want to apply
:param x_pos_override: Overrides the position of the text in the x
:param y_pos_override: Overrides the position of the text in the y
:return: ImageBuf
"""
if not font_path:
font_path = ResourceLoader.regular_font()
if not font_size:
if buffer.roi.width == buffer.roi.height:
average = (buffer.roi.width + buffer.roi.height) / 2
elif buffer.roi.width > buffer.roi.height:
average = buffer.roi.height / 2
else:
average = buffer.roi.width / 2
font_size = int(average * 0.8)
if not os.path.exists(font_path):
raise IOError("Font Path Not Found: " + font_path)
size = ImageBufAlgo.text_size(text, fontsize=font_size, fontname=font_path)
if size.defined:
if not x_pos_override:
x_pos_override = buffer.roi.xbegin + buffer.roi.width / 2 - (size.xbegin + size.width / 2)
if not y_pos_override:
y_pos_override = buffer.roi.ybegin + buffer.roi.height / 2 - (size.ybegin + size.height / 2)
if not ImageBufAlgo.render_text(
buffer, int(x_pos_override), int(y_pos_override), text, fontname=font_path, fontsize=font_size,
textcolor=text_color):
raise ValueError("error: " + buffer.geterror())
return buffer