jamendo_obs/lib/png_renderer.py
2022-11-11 06:23:00 +01:00

44 lines
No EOL
2.1 KiB
Python

from lib.renderer import Renderer
from PIL import Image, ImageDraw, ImageFont, ImageFile
class PNG_Renderer(Renderer):
def __init__(self, config):
self._config = config
ImageFile.LOAD_TRUNCATED_IMAGES = True
def render(self, cover, artist, album, title):
title_font = ImageFont.truetype(font=self._config['TitleFont'], size=int(self._config['TitleFontSize']))
title_y = 0
title_height = title_font.getbbox('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')[3]
text_width = title_font.getbbox(title)[2]
artist_font = ImageFont.truetype(font=self._config['ArtistFont'], size=int(self._config['ArtistFontSize']))
artist_y = title_y + title_height + 5
artist_height = artist_font.getbbox('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')[3]
if artist_font.getbbox(artist)[2] > text_width: text_width = artist_font.getbbox(artist)[2]
album_font = ImageFont.truetype(font=self._config['AlbumFont'], size=int(self._config['AlbumFontSize']))
album_y = artist_y + artist_height + 2
album_height = album_font.getbbox('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')[3]
if album_font.getbbox(album)[2] > text_width: text_width = album_font.getbbox(album)[2]
image_height = album_y + album_height + 10
image_width = text_width + image_height + 40
image = Image.new('RGBA', (image_width, image_height), color='#' + self._config['BackgroundColor'])
draw = ImageDraw.Draw(image)
text_x = 0
if self._config['Cover'] == '1':
cover = Image.open(cover)
cover.thumbnail((image_height, image_height), Image.ANTIALIAS)
image.paste(cover)
text_x = image_height + 20
draw.text((text_x,title_y), title, font=title_font, fill='#' + self._config['TitleFontColor'])
draw.text((text_x,artist_y), artist, font=artist_font, fill='#' + self._config['TitleFontColor'])
draw.text((text_x,album_y), album, font=album_font, fill='#' + self._config['TitleFontColor'])
image.save(self._config['OutputPath'])