Skip to content

Video editing with Python

Add a custom dropped-in replacement for pillow internal _multiline_spacing

osaajani97316f37f6a8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1c2fd92c..d2e4394a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fix circular reference in ImageSequenceClip leading to memory leak
+- Fix TextClip broken with Pillow > 11.2
### Added
- Possibility to select audio track when reading a file (#2429)
diff --git a/moviepy/audio/io/AudioFileClip.py b/moviepy/audio/io/AudioFileClip.py
index 897a3807..f426fdee 100644
--- a/moviepy/audio/io/AudioFileClip.py
+++ b/moviepy/audio/io/AudioFileClip.py
@@ -60,8 +60,13 @@ class AudioFileClip(AudioClip):
@convert_path_to_string("filename")
def __init__(
- self, filename, decode_file=False, buffersize=200000,
- nbytes=2, fps=44100, audio_stream_index=0
+ self,
+ filename,
+ decode_file=False,
+ buffersize=200000,
+ nbytes=2,
+ fps=44100,
+ audio_stream_index=0,
):
AudioClip.__init__(self)
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index 5e8d4734..7fe55658 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -14,7 +14,7 @@ import numpy as np
import proglog
from imageio.v2 import imread as imread_v2
from imageio.v3 import imwrite
-from PIL import Image, ImageDraw, ImageFont, __version__ as PIL_version
+from PIL import Image, ImageDraw, ImageFont
from moviepy.video.io.ffplay_previewer import ffplay_preview_video
@@ -1711,7 +1711,7 @@ class TextClip(ImageClip):
try:
# Only Pillow >= 10.1.0, can set font size
pil_font = ImageFont.load_default(font_size)
- except TypeError as e:
+ except TypeError:
pil_font = ImageFont.load_default()
draw = ImageDraw.Draw(img)
@@ -1786,9 +1786,9 @@ class TextClip(ImageClip):
try:
# Only Pillow >= 10.1.0, can set font size
font_pil = ImageFont.load_default(font_size)
- except TypeError as e:
+ except TypeError:
font_pil = ImageFont.load_default()
-
+
draw = ImageDraw.Draw(img)
lines = []
@@ -1921,18 +1921,35 @@ class TextClip(ImageClip):
)
# For height calculate manually as textbbox is not realiable
- try:
- # this disappeared in more recent versions of Pillow
- line_height = draw._multiline_spacing(font_pil, spacing, stroke_width)
- line_breaks = text.count("\n")
- lines_height = line_breaks * line_height
- paddings = real_font_size + stroke_width * 2
- height = int(lines_height + paddings)
- except AttributeError:
- height = int(bottom - top)
+ line_height = self.__multiline_spacing(draw, font_pil, spacing, stroke_width)
+ line_breaks = text.count("\n")
+ lines_height = line_breaks * line_height
+ paddings = real_font_size + stroke_width * 2
+ height = int(lines_height + paddings)
return (int(right - left), height)
+ def __multiline_spacing(
+ self,
+ draw: ImageDraw.ImageDraw,
+ font: ImageFont.ImageFont | ImageFont.FreeTypeFont | ImageFont.TransposedFont,
+ spacing: float,
+ stroke_width: float,
+ ) -> float:
+ """Calculate the spacing between lines for multiline text.
+
+ This method is used to calculate the height of each line in a multiline
+ text block, taking into account the font metrics, spacing, and stroke width.
+
+ This is a dropped-in replacement for the deprecated
+ `ImageDraw._multiline_spacing` method in Pillow.
+ """
+ return (
+ draw.textbbox((0, 0), "A", font, stroke_width=stroke_width)[3]
+ + stroke_width
+ + spacing
+ )
+
def __find_optimum_font_size(
self,
text,
diff --git a/pyproject.toml b/pyproject.toml
index 789f333b..e601f5c1 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -33,7 +33,7 @@ dependencies = [
"numpy>=1.25.0",
"proglog<=1.0.0",
"python-dotenv>=0.10",
- "pillow>=9.2.0,<12.0",
+ "pillow>=9.2.0",
]
[project.optional-dependencies]