Merge pull request #3187 from PySimpleGUI/Dev-latest
changed convert_to_bytes function to use a context manager for better…
This commit is contained in:
commit
1678576eb3
|
@ -20,6 +20,7 @@ import base64
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def convert_to_bytes(file_or_bytes, resize=None):
|
def convert_to_bytes(file_or_bytes, resize=None):
|
||||||
'''
|
'''
|
||||||
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.
|
||||||
|
@ -34,17 +35,22 @@ def convert_to_bytes(file_or_bytes, resize=None):
|
||||||
if isinstance(file_or_bytes, str):
|
if isinstance(file_or_bytes, str):
|
||||||
img = PIL.Image.open(file_or_bytes)
|
img = PIL.Image.open(file_or_bytes)
|
||||||
else:
|
else:
|
||||||
img = PIL.Image.open(io.BytesIO(base64.b64decode(file_or_bytes)))
|
try:
|
||||||
|
img = PIL.Image.open(io.BytesIO(base64.b64decode(file_or_bytes)))
|
||||||
|
except Exception as e:
|
||||||
|
dataBytesIO = io.BytesIO(file_or_bytes)
|
||||||
|
img = PIL.Image.open(dataBytesIO)
|
||||||
|
|
||||||
cur_width, cur_height = img.size
|
cur_width, cur_height = img.size
|
||||||
if resize:
|
if resize:
|
||||||
new_width, new_height = resize
|
new_width, new_height = resize
|
||||||
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)
|
||||||
bio = io.BytesIO()
|
with io.BytesIO() as bio:
|
||||||
img.save(bio, format="PNG")
|
img.save(bio, format="PNG")
|
||||||
del img
|
del img
|
||||||
return bio.getvalue()
|
return bio.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------- Define Layout ---------------------------------
|
# --------------------------------- Define Layout ---------------------------------
|
||||||
|
|
|
@ -891,10 +891,10 @@ def convert_to_bytes(file_or_bytes, resize=None):
|
||||||
new_width, new_height = resize
|
new_width, new_height = resize
|
||||||
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)
|
||||||
bio = io.BytesIO()
|
with io.BytesIO() as bio:
|
||||||
img.save(bio, format="PNG")
|
img.save(bio, format="PNG")
|
||||||
del img
|
del img
|
||||||
return bio.getvalue()
|
return bio.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue