Skip to content

Instantly share code, notes, and snippets.

@sergei-mironov
Last active August 24, 2021 20:38
Show Gist options
  • Save sergei-mironov/785a9ee514e99a28d9b9c16489a9b6cf to your computer and use it in GitHub Desktop.
Save sergei-mironov/785a9ee514e99a28d9b9c16489a9b6cf to your computer and use it in GitHub Desktop.
KITTY.py
from os import makedirs, get_terminal_size
from subprocess import call
from typing import Optional
import numpy as np
import matplotlib.pyplot as plt
from os import environ
environ['TERMINAL_IMAGES_COLS_PER_INCH']= str(256 / (598 / 25.4))
environ['TERMINAL_IMAGES_ROWS_PER_INCH']= str(67 / (315 / 25.4))
environ['LC_NUMERIC']='C'
def show(path:str, col:Optional[float], row:Optional[float])->None:
""" Send image to Kitty and then open it in "Feh" image viewer """
ret=call(['upload-terminal-image.sh']+
(['-c',str(int(col))] if col else []) +
(['-r',str(int(row))] if row else []) +
[path])
assert ret==0, f"upload-terminal-image.sh returned {ret}"
ret=call(['feh',path])
assert ret==0, f"feh returned {ret}"
def run():
""" Trying to find the best-looking dimensions of the image """
x=np.arange(0,100)
y=np.sin(x/8)/3
plt.plot(x,y)
plt.grid(True)
path='_plot.png'
col,row=62,None
(width0,height0) = (6.4, 4.8)
for i in [1.0, 0.97, 0.93, 0.9, 0.87]:
# for i in [1.0, 1.03, 1.07, 1.1, 1.13]:
(w,h)=(width0*i, height0*i)
plt.title(f"col {col} row {row} W {w:.3f} inch H {h:.3f} inch")
plt.gcf().set_size_inches(w,h)
plt.savefig(path)
show(path, col, row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment