31 lines
800 B
Python
31 lines
800 B
Python
|
# 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)
|