Added missing make_square function... big ooops.

This commit is contained in:
PySimpleGUI 2021-02-01 15:08:13 -05:00
parent 3997cb9151
commit 5332b056fe
1 changed files with 13 additions and 4 deletions

View File

@ -15,6 +15,14 @@ import base64
Copyright 2020 PySimpleGUI.org
"""
def make_square(im, min_size=256, fill_color=(0, 0, 0, 0)):
x, y = im.size
size = max(min_size, x, y)
new_im = Image.new('RGBA', (size, size), fill_color)
new_im.paste(im, (int((size - x) / 2), int((size - y) / 2)))
return new_im
def convert_to_bytes(file_or_bytes, resize=None, fill=False):
'''
Will convert into bytes and optionally resize an image that is a file or a base64 bytes object.
@ -43,7 +51,8 @@ def convert_to_bytes(file_or_bytes, resize=None, fill=False):
scale = min(new_height / cur_height, new_width / cur_width)
img = img.resize((int(cur_width * scale), int(cur_height * scale)), PIL.Image.ANTIALIAS)
if fill:
img = make_square(img, THUMBNAIL_SIZE[0])
if resize is not None:
img = make_square(img, resize[0])
with io.BytesIO() as bio:
img.save(bio, format="PNG")
del img
@ -61,7 +70,7 @@ def main():
image = sg._random_error_icon()
size = (60,60)
image = convert_to_bytes(image, size)
image = convert_to_bytes(image, size, fill=False)
layout = [[sg.Button('+', size=(4,2)), sg.Button('-', size=(4,2)), sg.B('Next', size=(4,2)), sg.T(size, size=(10,1), k='-SIZE-')],
[sg.Image(data=image, k='-IMAGE-')],
@ -85,7 +94,7 @@ def main():
image = event_window[event].ImageData
# Resize image and update the window
image = convert_to_bytes(image, size)
image = convert_to_bytes(image, size, fill=True)
window['-IMAGE-'].update(data=image)
window['-BUTTON IMAGE-'].update(image_data=image)
window['-SIZE-'].update(size)
@ -93,4 +102,4 @@ def main():
if __name__ == '__main__':
main()
main()