Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jantic
GitHub Repository: jantic/deoldify
Path: blob/master/fastai/sixel.py
781 views
1
from .core import *
2
3
libsixel = try_import('libsixel')
4
5
def _sixel_encode(data, width, height):
6
s = io.BytesIO()
7
output = libsixel.sixel_output_new(lambda data, s: s.write(data), s)
8
dither = libsixel.sixel_dither_new(256)
9
w,h = int(width),int(height)
10
libsixel.sixel_dither_initialize(dither, data, w, h, libsixel.SIXEL_PIXELFORMAT_RGBA8888)
11
libsixel.sixel_encode(data, w, h, 1, dither, output)
12
return s.getvalue().decode('ascii')
13
14
def plot_sixel(fig=None):
15
if not libsixel:
16
warn("You could see this plot with `libsixel`. See https://github.com/saitoha/libsixel")
17
return
18
if fig is None: fig = plt.gcf()
19
fig.canvas.draw()
20
dpi = fig.get_dpi()
21
res = _sixel_encode(fig.canvas.buffer_rgba(), fig.get_figwidth()* dpi, fig.get_figheight() * dpi)
22
print(res)
23
24
25