MCPcopy Index your code
hub / github.com/PySimpleGUI/PySimpleGUI / convert_to_bytes

Function convert_to_bytes

DemoPrograms/Demo_Emoji_Toolbar_PIL.py:70–104  ·  view source on GitHub ↗

Will convert into bytes and optionally resize an image that is a file or a base64 bytes object. Turns into PNG format in the process so that can be displayed by tkinter :param file_or_bytes: either a string filename or a bytes base64 image object :type file_or_bytes: (Union[str, b

(file_or_bytes, resize=None, fill=False)

Source from the content-addressed store, hash-verified

68
69
70def convert_to_bytes(file_or_bytes, resize=None, fill=False):
71 """
72 Will convert into bytes and optionally resize an image that is a file or a base64 bytes object.
73 Turns into PNG format in the process so that can be displayed by tkinter
74 :param file_or_bytes: either a string filename or a bytes base64 image object
75 :type file_or_bytes: (Union[str, bytes])
76 :param resize: optional new size
77 :type resize: (Tuple[int, int] or None)
78 :param fill: If True then the image is filled/padded so that the image is not distorted
79 :type fill: (bool)
80 :return: (bytes) a byte-string object
81 :rtype: (bytes)
82 """
83
84 if isinstance(file_or_bytes, str):
85 img = PIL.Image.open(file_or_bytes)
86 else:
87 try:
88 img = PIL.Image.open(io.BytesIO(base64.b64decode(file_or_bytes)))
89 except Exception as e:
90 dataBytesIO = io.BytesIO(file_or_bytes)
91 img = PIL.Image.open(dataBytesIO)
92
93 cur_width, cur_height = img.size
94 if resize:
95 new_width, new_height = resize
96 scale = min(new_height / cur_height, new_width / cur_width)
97 img = img.resize((int(cur_width * scale), int(cur_height * scale)), PIL.Image.LANCZOS)
98 if fill:
99 if resize is not None:
100 img = make_square(img, resize[0])
101 with io.BytesIO() as bio:
102 img.save(bio, format="PNG")
103 del img
104 return bio.getvalue()
105
106
107'''

Callers 1

Calls 2

saveMethod · 0.80
make_squareFunction · 0.70

Tested by

no test coverage detected