hcatitles/hcatitles.py

118 lines
4.1 KiB
Python
Raw Normal View History

2022-05-18 23:19:58 -06:00
import PySimpleGUI as sg
2022-05-30 12:07:50 -06:00
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont, ImageOps
2022-05-18 22:30:18 -06:00
from io import BytesIO
2022-05-27 15:37:07 -06:00
# Globals
2022-05-29 12:26:02 -06:00
def update_image(original, title, subtitle):
"""Render new title and title preview
renders text on full sized slide
make half sized copy for preview and fill top/bottom with white.
2022-05-27 15:37:07 -06:00
"""
2022-05-30 11:52:33 -06:00
# render text
image = Image.new(mode="RGBA", size=(1280,720))
2022-05-29 12:26:02 -06:00
font1 = ImageFont.truetype('tahomabd.ttf', 50)
font2 = ImageFont.truetype('tahomabd.ttf', 38)
draw = ImageDraw.Draw(image)
2022-05-30 10:20:46 -06:00
draw.text((20,520), title, font=font1, fill='white',
stroke_width=4, stroke_fill='black', spacing=4)
draw.text((20,588), subtitle, font=font2, fill='white',
stroke_width=4, stroke_fill='black', spacing=4)
2022-05-30 11:45:54 -06:00
2022-05-30 11:52:33 -06:00
# render drop dhadow
2022-05-30 11:45:54 -06:00
dropshadow = Image.new(mode="RGBA", size=(1280,720))
font1 = ImageFont.truetype('tahomabd.ttf', 50)
font2 = ImageFont.truetype('tahomabd.ttf', 38)
draw = ImageDraw.Draw(dropshadow)
draw.text((25,525), title, font=font1, fill='black',
stroke_width=4, stroke_fill='black', spacing=4)
draw.text((25,593), subtitle, font=font2, fill='black',
stroke_width=4, stroke_fill='black', spacing=4)
2022-05-30 11:52:33 -06:00
# apply blur
for i in range(6):
dropshadow = dropshadow.filter(ImageFilter.BLUR)
2022-05-30 12:07:50 -06:00
# make dropdhadow 10% transparent
alpha = dropshadow.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(.9)
dropshadow.putalpha(alpha)
2022-05-30 11:52:33 -06:00
2022-05-30 11:45:54 -06:00
background = Image.alpha_composite(original, dropshadow)
background = Image.alpha_composite(background, image)
return background
2022-05-18 22:30:18 -06:00
2022-05-29 12:26:02 -06:00
def update_thumbnail(image):
"""genreate thumbnail from original image"""
"""454-720 scales to 227-360"""
scaled = image.resize((720, 360))
# white = (255, 255, 255, 255)
# ImageDraw.floodfill(scaled, (5, 5), value=white)
# ImageDraw.floodfill(scaled, (5, 355), value=white)
thumbnail = scaled.crop((0, 255, 719, 359))
2022-05-29 12:26:02 -06:00
return thumbnail
2022-05-18 22:30:18 -06:00
def build_layout(title, subtitle, thumbnail):
2022-05-27 15:37:07 -06:00
control_col = sg.Column([
[
sg.Frame('Title',
2022-05-30 11:08:57 -06:00
layout = [[sg.Input(title, key = '-TITLE-',
enable_events=True)]]
2022-05-27 15:37:07 -06:00
)],
[
2022-05-29 13:24:17 -06:00
sg.Frame('subtitle',
2022-05-30 11:08:57 -06:00
layout = [[sg.Input(subtitle, key = '-SUBTITLE-',
enable_events=True)]]
2022-05-27 15:37:07 -06:00
)],
[
sg.Button('Save image', key= '-SAVE-')],
])
2022-05-27 15:37:07 -06:00
bio = BytesIO()
2022-05-29 12:26:02 -06:00
thumbnail.save(bio, format = 'PNG')
2022-05-27 15:37:07 -06:00
image_col = sg.Column([[sg.Image(data=bio.getvalue(), key = '-IMAGE-')]])
layout = [[control_col,image_col]]
return layout
2022-05-18 22:30:18 -06:00
2022-05-27 15:37:07 -06:00
def main():
title = 'Pastor Shane Wallis'
subtitle = 'Bible Stduy Wednesday June 1st, 2022'
2022-05-27 15:37:07 -06:00
image_path = 'blank_title.png'
original = Image.open(image_path)
image = update_image(original, title, subtitle)
thumbnail = update_thumbnail(image)
layout = build_layout(title, subtitle, thumbnail)
2022-05-27 15:37:07 -06:00
window = sg.Window('Harvest Christian Assembly Message Title', layout)
2022-05-18 22:30:18 -06:00
# image contains the final image
2022-05-27 15:37:07 -06:00
while True:
event, values = window.read(timeout=50)
if event == sg.WIN_CLOSED:
break
2022-05-18 22:30:18 -06:00
if event == "-TITLE-" or event == '-SUBTITLE-':
if values['-TITLE-'] != title or values['-SUBTITLE-'] != subtitle:
title = values['-TITLE-']
subtitle = values['-SUBTITLE-']
original = Image.open(image_path)
image = update_image(original, title, subtitle)
thumbnail = update_thumbnail(image)
bio = BytesIO()
thumbnail.save(bio, format = 'PNG')
window['-IMAGE-'].update(data = bio.getvalue())
2022-05-27 15:37:07 -06:00
if event == "-SAVE-":
save_path = sg.popup_get_file('Save',
save_as = True,
no_window = True) + '.png'
image.save(save_path, 'PNG')
2022-05-18 22:30:18 -06:00
2022-05-27 15:37:07 -06:00
window.close()
2022-05-18 22:30:18 -06:00
2022-05-27 15:37:07 -06:00
if __name__ == "__main__":
main()