Skip to content

Instantly share code, notes, and snippets.

@msizov
Created April 17, 2023 06:05
Show Gist options
  • Save msizov/f693a17586dce35facf96c5edd485be3 to your computer and use it in GitHub Desktop.
Save msizov/f693a17586dce35facf96c5edd485be3 to your computer and use it in GitHub Desktop.
import PIL
from PIL import Image
import sys
def print_img(f):
img = Image.open(f)
basewidth= 320
hsize = img.height * basewidth // img.width
print(img.size)
img=img.resize((basewidth, hsize))
print(img.size)
img_gray = PIL.ImageOps.invert(img.convert('L'))
img_color1 = img_gray.point( lambda p: 255 if p >= 64 and p < 128 else 0 ).convert('1').tobytes()
img_color2 = img_gray.point( lambda p: 255 if p >= 128 and p < 128+64 else 0 ).convert('1').tobytes()
img_color3 = img_gray.point( lambda p: 255 if p >= 128+64 else 0 ).convert('1').tobytes()
img_data = b''
#interleave colors
for y in range(img.height) :
img_data += img_color1[(img.width//8) * y:(img.width//8) * (y+1)]
img_data += img_color2[(img.width//8) * y:(img.width//8) * (y+1)]
img_data += img_color3[(img.width//8) * y:(img.width//8) * (y+1)]
#print(img_data)
w = img.width // 8
h = img.height * 3
wL = (w % 256).to_bytes(1,'big')
wH = (w // 256).to_bytes(1,'big')
hL = (h % 256).to_bytes(1,'big')
hH = (h // 256).to_bytes(1,'big')
data = "\x1ba1\x12#\x09\x1dv1p".encode('ascii') +wL + wH + hL + hH + img_data
open("/tmp/DEVTERM_PRINTER_IN", 'wb').write(data)
return data
print_img(sys.argv[1])
@LaneaLucy
Copy link

LaneaLucy commented Apr 26, 2024

Is this still working? And is it working on devterm cm4?

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