ytdl-sub/ytdl_subscribe/plugins/convert_thumbnail.py
2022-04-24 00:30:53 +00:00

44 lines
1.5 KiB
Python

import os
from PIL.Image import Image
from PIL.Image import open as pil_open
from ytdl_subscribe.entries.entry import Entry
from ytdl_subscribe.plugins.plugin import Plugin
from ytdl_subscribe.plugins.plugin import PluginOptions
from ytdl_subscribe.validators.string_select_validator import StringSelectValidator
class ThumbnailTypes(StringSelectValidator):
"""Valid image types that thumbnails can be converted to"""
_select_values = {"jpg"}
class ConvertThumbnailOptions(PluginOptions):
_required_keys = {"to"}
def __init__(self, name, value):
super().__init__(name, value)
self.convert_to = self._validate_key(key="to", validator=ThumbnailTypes)
class ConvertThumbnailPlugin(Plugin[ConvertThumbnailOptions]):
plugin_options_type = ConvertThumbnailOptions
def post_process_entry(self, entry: Entry):
"""
Convert the source thumbnail to the desired type. Leave the original file name and
extension intact to let output_options deal with renaming it.
"""
if not os.path.isfile(entry.get_download_thumbnail_path()):
raise ValueError("Thumbnail not found")
image: Image = pil_open(entry.get_download_thumbnail_path()).convert("RGB")
# Pillow likes the formal 'jpeg' name and not 'jpg'
thumbnail_format = self.plugin_options.convert_to.value
if thumbnail_format == "jpg":
thumbnail_format = "jpeg"
image.save(fp=entry.get_download_thumbnail_path(), format=thumbnail_format)