PySimpleGUI/DemoPrograms/Demo_Image_Resize_and_Base6...

89 lines
7.4 KiB
Python

import PySimpleGUI as sg
from PIL import Image
import os
import base64
"""
Demo Image Resize and Base64 Encode
A quick little utility that will resize an image and also Base64 Encode it.
Base64 is particualr good to use to make icons or other images that you include in your sourcecode.
Copyright 2021 PySimpleGUI
"""
def resize(input_file, output_file, size):
image = Image.open(input_file)
width, height = image.size
print(f"The original image size is {width} wide x {height} high")
new_width, new_height = size
scale = min(new_height / height, new_width / width)
resized_image = image.resize((int(width * scale), int(height * scale)), Image.ANTIALIAS)
# resized_image = image.resize(size)
width, height = resized_image.size
print(f"The resized image size is {width} wide x {height} high")
resized_image.save(output_file)
def convert_file_to_base64(filename):
try:
contents = open(filename, 'rb').read()
encoded = base64.b64encode(contents)
sg.clipboard_set(encoded)
if not sg.user_settings_get_entry('-autoclose-'):
sg.popup('Copied to your clipboard!', 'Keep program open until you have pasted the base64 bytestring', auto_close=True, auto_close_duration=4)
except Exception as error:
sg.popup_error('Cancelled - An error occurred', error)
def main():
layout = [ [sg.Text('Image Resizer')],
[sg.Frame('Input Image', [[sg.Input(key='-IN-', enable_events=True), sg.FileBrowse()],
[sg.T('Original size'), sg.T(k='-ORIG WIDTH-'), sg.T('X'), sg.T(k='-ORIG HEIGHT-')]])],
[sg.Frame('New Size', [[sg.In(50, s=4, k='-WIDTH-'), sg.T('X'), sg.In(50, s=4, k='-HEIGHT-')]])],
[sg.CBox('Encode to Base64 and leave on Clipboard', default=True,k='-BASE64-')],
[sg.CBox('Autoclose Immediately When Done', default=sg.user_settings_get_entry('-autoclose-', True if sg.running_windows() else False),k='-AUTOCLOSE-')],
[sg.Button('Resize', bind_return_key=True), sg.Button('Exit')],
[sg.T('Note - on some systems, autoclose cannot be used\nbecause the clipboard is cleared by tkinter')],]
window = sg.Window('Resize Image', layout, icon=image_resize_icon, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_VER_EXIT, enable_close_attempted_event=True)
while True:
event, values = window.read()
print(event, values)
if event in (sg.WIN_CLOSED, sg.WIN_CLOSE_ATTEMPTED_EVENT, 'Exit'):
sg.user_settings_set_entry('-autoclose-', values['-AUTOCLOSE-'])
break
sg.user_settings_set_entry('-autoclose-', values['-AUTOCLOSE-'])
infile = values['-IN-']
if event == '-IN-':
if os.path.isfile(infile):
image = Image.open(infile)
window['-ORIG WIDTH-'].update(image.size[0])
window['-ORIG HEIGHT-'].update(image.size[1])
if event == 'Resize':
if os.path.isfile(infile):
infilename = os.path.basename(infile)
infilenameonly, infileext = os.path.splitext(infilename)
try:
width, height = int(values['-WIDTH-']), int(values['-HEIGHT-'])
outfile = f'{infilenameonly}{width}x{height}{infileext}'
outfullfilename = os.path.join(os.path.dirname(infile), outfile)
resize(input_file=infile, output_file=outfullfilename, size=(width, height))
if values['-BASE64-']:
convert_file_to_base64(outfullfilename)
except Exception as e:
sg.popup_error_with_traceback('Error resizing or converting', 'Error encountered during the resize or Base64 encoding', e)
sg.popup_quick_message('DONE!', font='_ 40', background_color='red', text_color='white')
if sg.user_settings_get_entry('-autoclose-'):
break
elif event == 'Version':
sg.popup_scrolled(sg.get_versions(), non_blocking=True)
elif event == 'Edit Me':
sg.execute_editor(__file__)
window.close()
if __name__ == '__main__':
image_resize_icon = b'iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAJaklEQVR4nMWabYxU1RnHf885d+7s7uxrBWFFEClagqxYX1upWm3Q2i+KiaRNqwg0VWljtVjR2pT4oZFGa0pSSxt1gdjEBk0x0cZi1QpCGrUmwgKlVkWBZdcX2GWX2Z25L+fphzu7vOzMujO7q/8PM3fu3Oee53/Oc5/zP8+5wjjgwPpZp6RNakIc0aQqNQAi2mc9uvIu/PT0RXsOjXWbMhY3OdR6ztTQmG+I43IHc0GngjSCVilik4Y0BsmBdoPsN7BdDVtSzm09Zcmu/aP1oWIi76yema6vq/62ERY51SvTKdsoArFTYgeqigLJR9KSACKCNWCNoApB5LqAV42wrutI/6azf/pu/nMhoisxnd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