Merge pull request #3861 from PySimpleGUI/Dev-latest
Added missing make_square function... big ooops.
This commit is contained in:
commit
c4b31a2dfd
|
@ -15,6 +15,14 @@ import base64
|
||||||
Copyright 2020 PySimpleGUI.org
|
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):
|
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.
|
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)
|
scale = min(new_height / cur_height, new_width / cur_width)
|
||||||
img = img.resize((int(cur_width * scale), int(cur_height * scale)), PIL.Image.ANTIALIAS)
|
img = img.resize((int(cur_width * scale), int(cur_height * scale)), PIL.Image.ANTIALIAS)
|
||||||
if fill:
|
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:
|
with io.BytesIO() as bio:
|
||||||
img.save(bio, format="PNG")
|
img.save(bio, format="PNG")
|
||||||
del img
|
del img
|
||||||
|
@ -61,7 +70,7 @@ def main():
|
||||||
|
|
||||||
image = sg._random_error_icon()
|
image = sg._random_error_icon()
|
||||||
size = (60,60)
|
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-')],
|
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-')],
|
[sg.Image(data=image, k='-IMAGE-')],
|
||||||
|
@ -85,7 +94,7 @@ def main():
|
||||||
image = event_window[event].ImageData
|
image = event_window[event].ImageData
|
||||||
|
|
||||||
# Resize image and update the window
|
# 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['-IMAGE-'].update(data=image)
|
||||||
window['-BUTTON IMAGE-'].update(image_data=image)
|
window['-BUTTON IMAGE-'].update(image_data=image)
|
||||||
window['-SIZE-'].update(size)
|
window['-SIZE-'].update(size)
|
||||||
|
|
Loading…
Reference in New Issue