From db717ecbea7b79fc7fa2f13cc3ddf4c03e4dc52c Mon Sep 17 00:00:00 2001 From: Vadim Date: Sun, 18 Jun 2023 22:13:21 +0200 Subject: [PATCH] - crop youtube thumbnail to 1:1 apec ratio --- src/modules/Audio/youtube.py | 8 ++++---- src/modules/Image/image_helper.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 src/modules/Image/image_helper.py diff --git a/src/modules/Audio/youtube.py b/src/modules/Audio/youtube.py index a4ca385..3ece6ad 100644 --- a/src/modules/Audio/youtube.py +++ b/src/modules/Audio/youtube.py @@ -7,7 +7,7 @@ import yt_dlp from PIL import Image from src.modules.console_colors import ULTRASINGER_HEAD - +from src.modules.Image.image_helper import crop_image_to_square def get_youtube_title(url: str) -> tuple[str, str]: """Get the title of the YouTube video""" @@ -62,9 +62,9 @@ def download_and_convert_thumbnail(ydl_opts, url: str, clear_filename: str, outp response = ydl.urlopen(thumbnail_url) image_data = response.read() image = Image.open(io.BytesIO(image_data)) - image.save( - os.path.join(output_path, clear_filename + " [CO].jpg"), "JPEG" - ) + image_path = os.path.join(output_path, clear_filename + " [CO].jpg") + image.save(image_path, "JPEG") + crop_image_to_square(image_path) def download_youtube_video(url: str, clear_filename: str, output_path: str) -> None: diff --git a/src/modules/Image/image_helper.py b/src/modules/Image/image_helper.py new file mode 100644 index 0000000..2dd02d0 --- /dev/null +++ b/src/modules/Image/image_helper.py @@ -0,0 +1,18 @@ +from PIL import Image + + +def crop_image_to_square(image_path): + image = Image.open(image_path) + width, height = image.size + size = min(width, height) + + # Calculate the coordinates for the crop + left = (width - size) // 2 + top = (height - size) // 2 + right = left + size + bottom = top + size + + cropped_image = image.crop((left, top, right, bottom)) + + # Override the original image with the cropped version + cropped_image.save(image_path) \ No newline at end of file