Skip to content

Commit

Permalink
Merge pull request #2344 from OsaAjani/issue-2338
Browse files Browse the repository at this point in the history
Add support for default pillow font
  • Loading branch information
OsaAjani authored Jan 23, 2025
2 parents 030c147 + caba2d9 commit 52329a1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add codecs to .mov files
- Add background radius to text clips
- Support pillow 11
- Add support for Pillow default font on textclip
- Add support for ffmpeg v7

### Changed <!-- for changes in existing functionality -->
Expand Down
55 changes: 33 additions & 22 deletions moviepy/video/VideoClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,8 @@ class TextClip(ImageClip):
----------
font
Path to the font to use. Must be an OpenType font.
Path to the font to use. Must be an OpenType font. If set to None
(default) will use Pillow default font
text
A string of the text to write. Can be replaced by argument
Expand Down Expand Up @@ -1549,7 +1550,7 @@ class TextClip(ImageClip):
@convert_path_to_string("filename")
def __init__(
self,
font,
font=None,
text=None,
filename=None,
font_size=None,
Expand All @@ -1567,12 +1568,13 @@ def __init__(
transparent=True,
duration=None,
):
try:
_ = ImageFont.truetype(font)
except Exception as e:
raise ValueError(
"Invalid font {}, pillow failed to use it with error {}".format(font, e)
)
if font is not None:
try:
_ = ImageFont.truetype(font)
except Exception as e:
raise ValueError(
"Invalid font {}, pillow failed to use it with error {}".format(font, e)
)

if filename:
with open(filename, "r") as file:
Expand Down Expand Up @@ -1620,30 +1622,30 @@ def __init__(
allow_break=True,
)

if img_height is None:
img_height = self.__find_text_size(
# Add line breaks whenever needed
text = "\n".join(
self.__break_text(
width=img_width,
text=text,
font=font,
font_size=font_size,
stroke_width=stroke_width,
align=text_align,
spacing=interline,
max_width=img_width,
allow_break=True,
)[1]
)
)

# Add line breaks whenever needed
text = "\n".join(
self.__break_text(
width=img_width,
if img_height is None:
img_height = self.__find_text_size(
text=text,
font=font,
font_size=font_size,
stroke_width=stroke_width,
align=text_align,
spacing=interline,
)
)
max_width=img_width,
allow_break=True,
)[1]

elif method == "label":
if font_size is None and img_width is None:
Expand Down Expand Up @@ -1693,7 +1695,10 @@ def __init__(
bg_color = (0, 0, 0, 0)

img = Image.new(img_mode, (img_width, img_height), color=bg_color)
pil_font = ImageFont.truetype(font, font_size)
if font:
pil_font = ImageFont.truetype(font, font_size)
else:
pil_font = ImageFont.load_default(font_size)
draw = ImageDraw.Draw(img)

# Dont need allow break here, because we already breaked in caption
Expand Down Expand Up @@ -1760,7 +1765,10 @@ def __break_text(
) -> List[str]:
"""Break text to never overflow a width"""
img = Image.new("RGB", (1, 1))
font_pil = ImageFont.truetype(font, font_size)
if font:
font_pil = ImageFont.truetype(font, font_size)
else:
font_pil = ImageFont.load_default(font_size)
draw = ImageDraw.Draw(img)

lines = []
Expand Down Expand Up @@ -1843,7 +1851,10 @@ def __find_text_size(
``real_font_size + (stroke_width * 2) + (lines - 1) * height``
"""
img = Image.new("RGB", (1, 1))
font_pil = ImageFont.truetype(font, font_size)
if font:
font_pil = ImageFont.truetype(font, font_size)
else:
font_pil = ImageFont.load_default(font_size)
ascent, descent = font_pil.getmetrics()
real_font_size = ascent + descent
draw = ImageDraw.Draw(img)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_TextClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,12 @@ def test_label_autosizing(util):
assert not np.allclose(last_three_columns, [0, 0, 0], rtol=0.01)


def test_no_font(util):
# Try make a clip with default font
clip = TextClip(text="Hello world !", font_size=20, color="white")
clip.show(1)
assert clip.size[0] > 10


if __name__ == "__main__":
pytest.main()

0 comments on commit 52329a1

Please sign in to comment.