34 from base64
import standard_b64encode, standard_b64decode
36 from math
import floor, ceil, sqrt
40 """ PNG-compress the string in a square RBG image padded with '\n', return the b64 encoded bytes """ 41 string_bytes = string.encode(
'utf-8')
42 length = len(string_bytes)
43 width = floor(sqrt(length/3.0))
44 height = ceil((length/3.0) / width)
45 bytes_needed = int(width * height * 3)
46 string_padded = string_bytes + (b
'\n' * (bytes_needed - length))
47 i = Image.frombytes(
'RGB', (int(width), int(height)), string_padded)
50 encoded = standard_b64encode(buff.getvalue()).
decode()
54 """ b64 decode the string, then PNG-decompress """ 55 decoded = standard_b64decode(string)
56 buff = BytesIO(decoded)
60 except NotImplementedError:
61 return i.tobytes().
decode(
'utf-8')