Print image on socket Termal printer (Python)
Question:
I’m trying to create a python script to print an image on my network connected thermal printer, through a socket connection.
I tried this script but instead of printing the image it prints an infinite string of characters:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("192.168.1.56", 9100))
with open("immagine.jpg", "rb") as f:
sock.sendall(f.read())
sock.close()
Are there any other methods to do this?
Maybe some escape command that I need to send with the image or something like that.
I also found this script (https://gist.github.com/Carko/1507260d13eaa3e7cea6cecb713caca0) but it should be adapted for sending via socket, does it make sense to try to adapt it or not?
Answers:
If someone were to happen here, the solution was this Github here:
https://gist.github.com/Carko/1507260d13eaa3e7cea6cecb713caca0
Adapted like this:
import socket
import serial
from PIL import Image
import PIL.ImageOps
import struct
im = Image.open("YOUR/IMAGE/PATH")
# if image is not 1-bit, convert it
if im.mode != '1':
im = im.convert('1')
# if image width is not a multiple of 8 pixels, fix that
if im.size[0] % 8:
im2 = Image.new('1', (im.size[0] + 8 - im.size[0] % 8,
im.size[1]), 'white')
im2.paste(im, (0, 0))
im = im2
# Invert image, via greyscale for compatibility
# (no, I don't know why I need to do this)
im = PIL.ImageOps.invert(im.convert('L'))
# ... and now convert back to single bit
im = im.convert('1')
job = [b'x1brn',
(b''.join((bytearray(b'x1dx76x30x00'),
struct.pack('2B', int(im.size[0] / 8 % 256),
int(im.size[0] / 8 / 256)),
struct.pack('2B', int(im.size[1] % 256),
int(im.size[1] / 256)),
im.tobytes()))),
b'x1brn',
b'x1brn',
b'x1brn',
b'x1brn',
b'x1bx69'
]
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.connect(("YOUR IP","YOUR PORT"))
for b in job:
soc.sendall(b)
soc.close()
I’m trying to create a python script to print an image on my network connected thermal printer, through a socket connection.
I tried this script but instead of printing the image it prints an infinite string of characters:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("192.168.1.56", 9100))
with open("immagine.jpg", "rb") as f:
sock.sendall(f.read())
sock.close()
Are there any other methods to do this?
Maybe some escape command that I need to send with the image or something like that.
I also found this script (https://gist.github.com/Carko/1507260d13eaa3e7cea6cecb713caca0) but it should be adapted for sending via socket, does it make sense to try to adapt it or not?
If someone were to happen here, the solution was this Github here:
https://gist.github.com/Carko/1507260d13eaa3e7cea6cecb713caca0
Adapted like this:
import socket
import serial
from PIL import Image
import PIL.ImageOps
import struct
im = Image.open("YOUR/IMAGE/PATH")
# if image is not 1-bit, convert it
if im.mode != '1':
im = im.convert('1')
# if image width is not a multiple of 8 pixels, fix that
if im.size[0] % 8:
im2 = Image.new('1', (im.size[0] + 8 - im.size[0] % 8,
im.size[1]), 'white')
im2.paste(im, (0, 0))
im = im2
# Invert image, via greyscale for compatibility
# (no, I don't know why I need to do this)
im = PIL.ImageOps.invert(im.convert('L'))
# ... and now convert back to single bit
im = im.convert('1')
job = [b'x1brn',
(b''.join((bytearray(b'x1dx76x30x00'),
struct.pack('2B', int(im.size[0] / 8 % 256),
int(im.size[0] / 8 / 256)),
struct.pack('2B', int(im.size[1] % 256),
int(im.size[1] / 256)),
im.tobytes()))),
b'x1brn',
b'x1brn',
b'x1brn',
b'x1brn',
b'x1bx69'
]
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.connect(("YOUR IP","YOUR PORT"))
for b in job:
soc.sendall(b)
soc.close()