Skip to content

Commit

Permalink
use default font for image_text
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski committed Feb 29, 2024
1 parent e987969 commit 7ba6d7d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
6 changes: 3 additions & 3 deletions pyllusion/image/image_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def image_text(
size="auto",
color="black",
background="white",
font="arial.ttf",
font="default",
blur=0,
image=None,
):
Expand Down Expand Up @@ -46,7 +46,7 @@ def image_text(
font : str
The name of the font to be used. Note that the font is what controls features like
bold / italic. For instance, 'arialbd.ttf', 'ariblk.ttf' or 'ariali.ttf' can be
used for bold, black and italic, respectively.
used for bold, black and italic, respectively. If "default", will use ``PIL.ImageFont.load_default()``
blur : int
The degree of blur filter for the image returned.
image : Image
Expand All @@ -66,7 +66,7 @@ def image_text(
>>> image = pyllusion.image_text(image=image, size=20, x=0.5, text="Bold and blurred", font="arialbd.ttf", blur=0.005)
>>> image
>>> pyllusion.image_text(text="3D", width=1600, height=900, font="arial.ttf", blur=0.01)
>>> image
>>> pyllusion.image_text(text="BIG")
"""
# Get image
if image is None:
Expand Down
18 changes: 12 additions & 6 deletions pyllusion/image/utilities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import PIL.ImageColor
import PIL.ImageFont
import functools

from .rescale import rescale

Expand Down Expand Up @@ -72,7 +73,7 @@ def _coord_circle(image, diameter=0.1, x=0, y=0, unit="grid", method="pil"):


def _coord_text(
image, text="hello", size="auto", x=0, y=0, font="arial.ttf", unit="grid",
image, text="hello", size="auto", x=0, y=0, font="default", unit="grid",
method="pil"
):
"""Get text coordinates
Expand All @@ -98,6 +99,11 @@ def _coord_text(
elif method == "psychopy":
y = int(rescale(y, to=[0, height], scale=[-1, 1]))

if isinstance(font, str) and font == "default":
get_font = functools.partial(PIL.ImageFont.load_default)
else:
get_font = functools.partial(PIL.ImageFont.truetype, font=font)

if size == "auto":
# Initialize values
size, top_left_x, top_left_y, right_x, bottom_y = 0, width, height, 0, 0
Expand All @@ -109,8 +115,8 @@ def _coord_text(
and bottom_y < 0.99 * height
):
try: # In case size is too small
loaded_font = PIL.ImageFont.truetype(font, size)
text_width, text_height = loaded_font.getbbox(text)[2:4]
font = get_font(size=size)
text_width, text_height = font.getbbox(text)[2:4]
top_left_x = x - (text_width / 2)
top_left_y = y - (text_height / 2)
right_x = top_left_x + text_width
Expand All @@ -119,15 +125,15 @@ def _coord_text(
pass
size += 0.5 # Increment text size
else:
loaded_font = PIL.ImageFont.truetype(font, size)
font = get_font(size=size)
# text_width, text_height = loaded_font.getsize(text)
text_width, text_height = loaded_font.getbbox(text)[2:4]
text_width, text_height = font.getbbox(text)[2:4]
top_left_x = x - (text_width / 2)
top_left_y = y - (text_height / 2)

coord = top_left_x, top_left_y

return coord, loaded_font, x, y
return coord, font, x, y


def _coord_line(
Expand Down

0 comments on commit 7ba6d7d

Please sign in to comment.