42 lines
1.3 KiB
Python
Executable File
42 lines
1.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# TODO: Consider removing when Kitty Graphics Addon is shipped.
|
|
"""
|
|
Minimal Kitty Graphics Protocol image sender.
|
|
Based on: https://sw.kovidgoyal.net/kitty/graphics-protocol/#a-minimal-example
|
|
|
|
Usage:
|
|
./send-png image.png # Original size
|
|
./send-png image.png 10 # Scale to 10 columns wide
|
|
./send-png image.png 10 5 # Scale to 10 columns x 5 rows
|
|
"""
|
|
import sys
|
|
from base64 import standard_b64encode
|
|
|
|
def serialize_image(path):
|
|
with open(path, 'rb') as f:
|
|
data = f.read()
|
|
return standard_b64encode(data).decode('ascii')
|
|
|
|
def write_chunked(data, columns=None, rows=None):
|
|
# a=T means transmit and display
|
|
# f=100 means PNG format
|
|
# c=columns, r=rows for sizing
|
|
params = 'a=T,f=100'
|
|
if columns:
|
|
params += f',c={columns}'
|
|
if rows:
|
|
params += f',r={rows}'
|
|
sys.stdout.write(f'\x1b_G{params};{data}\x1b\\')
|
|
sys.stdout.flush()
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) < 2:
|
|
print("Usage: ./send-png <image.png> [columns] [rows]", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
img_data = serialize_image(sys.argv[1])
|
|
columns = int(sys.argv[2]) if len(sys.argv) > 2 else None
|
|
rows = int(sys.argv[3]) if len(sys.argv) > 3 else None
|
|
write_chunked(img_data, columns, rows)
|
|
print() # newline after the sequence
|