library: add autopsy.py
This commit is contained in:
parent
4e1c374077
commit
7e3694c205
1 changed files with 30 additions and 0 deletions
30
library/autopsy.py
Normal file
30
library/autopsy.py
Normal 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)
|
Loading…
Add table
Reference in a new issue