There is a Python interface in libsixel, so let's use it.
abridgement.
You can convert the byte string to Sixel data, but it seems that you have to use the C API to do that, so you can easily realize it by discarding the runtime efficiency.
from libsixel.encoder import Encoder as SixelEncoder
from libsixel import SIXEL_OPTFLAG_WIDTH, SIXEL_OPTFLAG_HEIGHT
from tempfile import NamedTemporaryFile
def sixeldraw(width=None,height=None):
with NamedTemporaryFile(prefix="sixel-") as fd:
pl.savefig(fd,format="png")
fd.flush()
encoder = SixelEncoder()
if width : encoder.setopt(SIXEL_OPTFLAG_WIDTH, width)
if height: encoder.setopt(SIXEL_OPTFLAG_HEIGHT,height)
encoder.encode(fd.name)
if __name__=="__main__":
import matplotlib
matplotlib.use("Agg")
import pylab as pl
import numpy as np
x = np.linspace(0,1)
y = x**2
pl.plot(x,y)
sixeldraw(640)
Yes. Create a file temporarily and save the graph image. Output the image file to standard output in Sixel format using libsixel's python interface. If you use a terminal that supports Sixel, you can get an image.
It is a cut-out implementation.
Recommended Posts