diff --git a/library/autopsy.py b/library/autopsy.py new file mode 100644 index 0000000..37a5f3f --- /dev/null +++ b/library/autopsy.py @@ -0,0 +1,30 @@ +# autopsy.py -- a terminal image. + +from base64 import b64encode +from io import BytesIO +from sys import stdout, stderr +import PIL.Image + + +def termshow(im, fp=None, out=stderr): + if isinstance(im, BytesIO): + data = im.getvalue() + else: + f = BytesIO() + im = PIL.Image.fromarray(im) + im.save(f, "png") + data = f.getvalue() + data64 = b64encode(data) + + # magic = f"\033]1337;File=size={len(data)};width={dims}px;height={dims}px;inline=1:{data64}\007" + magic = f"\033]1337;File=size={len(data)};inline=1:{data64.decode()}\007" + + if out is not None: + if out in (stdout, stderr): + stdout.flush() + stderr.flush() + print(magic, file=out) + + if fp is not None: + with open(fp, "wb") as f: + f.write(data)