hcatitles/hcatitles.py

80 lines
2.5 KiB
Python
Raw Normal View History

2022-05-18 23:19:58 -06:00
import PySimpleGUI as sg
2022-05-27 15:37:07 -06:00
from PIL import Image, ImageDraw, 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-29 12:26:02 -06:00
image = original
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=3, stroke_fill='black', spacing=4)
2022-05-29 12:26:02 -06:00
return image
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-29 12:26:02 -06:00
layout = [[sg.Input(title, key = '-TITLE-')]]
2022-05-27 15:37:07 -06:00
)],
[
2022-05-29 13:24:17 -06:00
sg.Frame('subtitle',
layout = [[sg.Input(subtitle, key = '-SUBTITLE-')]]
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
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
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()