Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Last active July 27, 2021 20:59
Show Gist options
  • Save Hermann-SW/e867acb653eabfcbba35a226d6de55dc to your computer and use it in GitHub Desktop.
Save Hermann-SW/e867acb653eabfcbba35a226d6de55dc to your computer and use it in GitHub Desktop.
#!/bin/bash
fb="fb0"
vir_siz="/sys/class/graphics/$fb/virtual_size"
size=`stty size < /dev/tty`
echo -ne "\033[6n" < /dev/tty
read -s -d\[ garbage < /dev/tty
read -s -d R pos < /dev/tty
python3 <(cat <<EOF
'''draw image in Raspbian console'''
import sys
from PIL import Image
def to_bgr(img):
'''.convert('BGR') does not work for RGB workaround'''
rgb_bytes = img.convert('RGB').tobytes()
return Image.frombytes('RGB', img.size, rgb_bytes, 'raw', 'BGR')
def con_show(fhandle):
'''refresh framebuffer, with image drawn below current command line'''
drows, dcols = '$size'.rstrip().split()
drows, dcols = int(drows), int(dcols)
rpos = int('$pos'.split(';')[0])
right, bottom = open('$vir_siz', 'r').read().rstrip().split(',')
right, bottom = int(right), int(bottom)
fheight = int(int(bottom) / int(drows))
img = Image.open(fhandle)
irows = int((img.size[1] - 1) / fheight + 1)
if irows > drows - 2:
irows = drows - 2
if rpos + irows > drows:
irows = drows - rpos
if irows < 1:
print('\r \r', end='')
sys.exit()
iheight = irows * fheight
for prime in [13, 11, 7, 5, 3, 2]:
if dcols % prime != 0:
fill = 'abcdefghijklm'[:prime]
break
print('\r\033[5m', end='')
print((fill * (int(irows * dcols / prime) + 1))[:irows * dcols], end='')
print('\033[0m')
sys.stdout.flush()
img0 = Image.new('RGBA', [right, iheight])
img0.paste(to_bgr(img))
fbuf = open('/dev/$fb', 'wb')
fbuf.seek((rpos - 1) * fheight * right * 4)
fbuf.write(img0.tobytes())
FROM_STDIN = len(sys.argv) == 1 or sys.argv[1] == '-'
con_show(sys.stdin.buffer if FROM_STDIN else open(sys.argv[1], 'rb'))
EOF
) \
$1
@Hermann-SW
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment