From 284df38cb948ef409158f3da18e1bd2d6810990a Mon Sep 17 00:00:00 2001 From: aaa3334 Date: Mon, 2 Dec 2024 13:04:00 +1030 Subject: [PATCH 1/4] Update VideoClip.py Adding in a new optional parameter bg_radius. This parameter allows users to round the edges of the background text box so it is not sharp cornered. --- moviepy/video/VideoClip.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py index fd43a81a2..dce433b4d 100644 --- a/moviepy/video/VideoClip.py +++ b/moviepy/video/VideoClip.py @@ -1405,6 +1405,12 @@ class TextClip(ImageClip): a RGB (or RGBA if transparent = ``True``) ``tuple``, a color name, or an hexadecimal notation. + bg_radius + A paramater to round the edges of the text background. Defaults to 0 if there + is no background. It will have no effect if there is no bg_colour added. + The higher the value, the more rounded the corners will become. + + stroke_color Color of the stroke (=contour line) of the text. If ``None``, there will be no stroke. @@ -1451,6 +1457,7 @@ def __init__( margin=(None, None), color="black", bg_color=None, + bg_radius=0, stroke_color=None, stroke_width=0, method="label", @@ -1719,9 +1726,19 @@ def find_optimum_font_size( if bg_color is None and transparent: 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) - draw = ImageDraw.Draw(img) + if bg_radius is None: + bg_radius = 0 + + if bg_radius != 0: + + img = Image.new(img_mode, (img_width, img_height), color=(0,0,0,0)) + pil_font = ImageFont.truetype(font, font_size) + draw = ImageDraw.Draw(img) + draw.rounded_rectangle([0, 0, img_width, img_height], radius=bg_radius, fill=bg_color) + else: + img = Image.new(img_mode, (img_width, img_height), color=bg_color) + pil_font = ImageFont.truetype(font, font_size) + draw = ImageDraw.Draw(img) # Dont need allow break here, because we already breaked in caption text_width, text_height = find_text_size( From 6fe3f942744ad892f075c3a8f366040303702a27 Mon Sep 17 00:00:00 2001 From: Pierre-Lin Bonnemaison Date: Fri, 10 Jan 2025 16:22:44 +0100 Subject: [PATCH 2/4] Move new parameter bg_radius at the end to avoid breaking API --- moviepy/video/VideoClip.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py index dce433b4d..89e563ccb 100644 --- a/moviepy/video/VideoClip.py +++ b/moviepy/video/VideoClip.py @@ -1405,12 +1405,6 @@ class TextClip(ImageClip): a RGB (or RGBA if transparent = ``True``) ``tuple``, a color name, or an hexadecimal notation. - bg_radius - A paramater to round the edges of the text background. Defaults to 0 if there - is no background. It will have no effect if there is no bg_colour added. - The higher the value, the more rounded the corners will become. - - stroke_color Color of the stroke (=contour line) of the text. If ``None``, there will be no stroke. @@ -1444,6 +1438,11 @@ class TextClip(ImageClip): duration Duration of the clip + + bg_radius + A paramater to round the edges of the text background. Defaults to 0 if there + is no background. It will have no effect if there is no bg_colour added. + The higher the value, the more rounded the corners will become. """ @convert_path_to_string("filename") @@ -1457,7 +1456,6 @@ def __init__( margin=(None, None), color="black", bg_color=None, - bg_radius=0, stroke_color=None, stroke_width=0, method="label", @@ -1467,6 +1465,7 @@ def __init__( interline=4, transparent=True, duration=None, + bg_radius=0, # TODO : Move this with other bg_param on next breaking release ): def break_text( width, text, font, font_size, stroke_width, align, spacing From aaf1f033f1b3f00c396cdb591ac12ef67a551955 Mon Sep 17 00:00:00 2001 From: Pierre-Lin Bonnemaison Date: Fri, 10 Jan 2025 16:34:59 +0100 Subject: [PATCH 3/4] Update VideoClip.py --- moviepy/video/VideoClip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py index 89e563ccb..3ff428917 100644 --- a/moviepy/video/VideoClip.py +++ b/moviepy/video/VideoClip.py @@ -1465,7 +1465,7 @@ def __init__( interline=4, transparent=True, duration=None, - bg_radius=0, # TODO : Move this with other bg_param on next breaking release + bg_radius=0, # TODO : Move this with other bg_param on next breaking release ): def break_text( width, text, font, font_size, stroke_width, align, spacing From 99f8f3cd49a604bcee9fccb6e4c7ed61091f0772 Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Fri, 10 Jan 2025 16:52:05 +0100 Subject: [PATCH 4/4] fix linting --- moviepy/video/VideoClip.py | 9 +++++---- moviepy/video/fx/Scroll.py | 1 - moviepy/video/io/ffmpeg_reader.py | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py index 3ff428917..d8a3a96f5 100644 --- a/moviepy/video/VideoClip.py +++ b/moviepy/video/VideoClip.py @@ -1727,13 +1727,14 @@ def find_optimum_font_size( if bg_radius is None: bg_radius = 0 - + if bg_radius != 0: - - img = Image.new(img_mode, (img_width, img_height), color=(0,0,0,0)) + img = Image.new(img_mode, (img_width, img_height), color=(0, 0, 0, 0)) pil_font = ImageFont.truetype(font, font_size) draw = ImageDraw.Draw(img) - draw.rounded_rectangle([0, 0, img_width, img_height], radius=bg_radius, fill=bg_color) + draw.rounded_rectangle( + [0, 0, img_width, img_height], radius=bg_radius, fill=bg_color + ) else: img = Image.new(img_mode, (img_width, img_height), color=bg_color) pil_font = ImageFont.truetype(font, font_size) diff --git a/moviepy/video/fx/Scroll.py b/moviepy/video/fx/Scroll.py index 1d469d112..d5c9d60a8 100644 --- a/moviepy/video/fx/Scroll.py +++ b/moviepy/video/fx/Scroll.py @@ -30,7 +30,6 @@ def __init__( y_start=0, apply_to="mask", ): - self.w = w self.h = h self.x_speed = x_speed diff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.py index b22aa2b6d..16700ee15 100644 --- a/moviepy/video/io/ffmpeg_reader.py +++ b/moviepy/video/io/ffmpeg_reader.py @@ -459,12 +459,12 @@ def parse(self): # for default streams, set their numbers globally, so it's # easy to get without iterating all if self._current_stream["default"]: - self.result[f"default_{stream_type_lower}_input_number"] = ( - input_number - ) - self.result[f"default_{stream_type_lower}_stream_number"] = ( - stream_number - ) + self.result[ + f"default_{stream_type_lower}_input_number" + ] = input_number + self.result[ + f"default_{stream_type_lower}_stream_number" + ] = stream_number # exit chapter if self._current_chapter: