import PySimpleGUI as sg from PIL import Image, ImageDraw, ImageFilter, ImageFont, ImageOps from io import BytesIO # Globals 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. """ image = original font1 = ImageFont.truetype('tahomabd.ttf', 50) font2 = ImageFont.truetype('tahomabd.ttf', 38) draw = ImageDraw.Draw(image) draw.text((20,520), title, (255,255,255), font=font1) draw.text((20,588), subtitle, (255,255,255), font=font2) return image 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)) return thumbnail def build_layout(title, subtitle, thumbnail): control_col = sg.Column([ [ sg.Frame('Title', layout = [[sg.Input(title, key = '-TITLE-')]] )], [ sg.Frame('subtitle', layout = [[sg.Input(subtitle, key = '-SUBTITLE-')]] )], [ sg.Button('Save image', key= '-SAVE-')], ]) bio = BytesIO() thumbnail.save(bio, format = 'PNG') image_col = sg.Column([[sg.Image(data=bio.getvalue(), key = '-IMAGE-')]]) layout = [[control_col,image_col]] return layout def main(): title = 'Pastor Shane Wallis' subtitle = 'Bible Stduy Wednesday June 1st, 2022' 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) window = sg.Window('Harvest Christian Assembly Message Title', layout) while True: event, values = window.read(timeout=50) if event == sg.WIN_CLOSED: break if event == "-SAVE-": save_path = sg.popup_get_file('Save', save_as = True, no_window = True) + '.png' image.save(save_path, 'PNG') window.close() if __name__ == "__main__": main()