library: add autopsy.py

This commit is contained in:
Connor Olding 2022-06-07 06:44:56 +02:00
parent 4e1c374077
commit 7e3694c205

30
library/autopsy.py Normal file
View file

@ -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)