chore: move print to printclass
This commit is contained in:
parent
69a058834e
commit
934cfba919
1 changed files with 51 additions and 38 deletions
|
@ -25,32 +25,43 @@ def handle_image():
|
||||||
return f"Error loading image: {e}", 500
|
return f"Error loading image: {e}", 500
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print_image(img, resize=True)
|
print_image = PrintImage(img)
|
||||||
|
print_image.print()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"Error printing image: {e}", 500
|
return f"Error printing image: {e}", 500
|
||||||
|
|
||||||
return "ok", 200
|
return "ok", 200
|
||||||
|
|
||||||
|
|
||||||
def resize_image(img: Image):
|
class PrintImage:
|
||||||
|
image: Image
|
||||||
|
|
||||||
|
def __init__(self, image: Image, offset=True, resize=True):
|
||||||
|
self.image = image
|
||||||
|
if resize:
|
||||||
|
self._resize()
|
||||||
|
if offset:
|
||||||
|
self._offset()
|
||||||
|
|
||||||
|
def _resize(self):
|
||||||
|
img = self.image
|
||||||
|
|
||||||
if img.height > img.width:
|
if img.height > img.width:
|
||||||
img = img.transpose(Image.ROTATE_90)
|
img = img.transpose(Image.ROTATE_90)
|
||||||
|
|
||||||
if 323 / img.width * img.height <= 240:
|
if 323 / img.width * img.height <= 240:
|
||||||
img = img.resize(size=(323, int(323 / img.width * img.height)))
|
img = img.resize(size=(323, int(323 / img.width * img.height)))
|
||||||
else:
|
else:
|
||||||
img = img.resize(size=(int(240 / img.height * img.width), 240))
|
img = img.resize(size=(int(240 / img.height * img.width), 240))
|
||||||
return img
|
|
||||||
|
|
||||||
|
self.image = img
|
||||||
|
|
||||||
def print_image(img: Image, resize=False, offset=True):
|
def _offset(self):
|
||||||
img = img.convert("1")
|
|
||||||
if resize:
|
|
||||||
img = resize_image(img)
|
|
||||||
if offset:
|
|
||||||
imgborder = Image.new("1", (384, 240), color=1)
|
imgborder = Image.new("1", (384, 240), color=1)
|
||||||
imgborder.paste(img, (61, 0))
|
imgborder.paste(self.image, (61, 0))
|
||||||
img = imgborder
|
self.image = imgborder
|
||||||
|
|
||||||
|
def print(self):
|
||||||
|
img = self.image.convert("1")
|
||||||
|
|
||||||
buf = []
|
buf = []
|
||||||
buf += [0x1B, 0x40]
|
buf += [0x1B, 0x40]
|
||||||
|
@ -74,7 +85,9 @@ def print_image(img: Image, resize=False, offset=True):
|
||||||
|
|
||||||
# -- send
|
# -- send
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
|
sock = socket.socket(
|
||||||
|
socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM
|
||||||
|
)
|
||||||
sock.bind((socket.BDADDR_ANY, 1))
|
sock.bind((socket.BDADDR_ANY, 1))
|
||||||
sock.connect((DEVICE, 1))
|
sock.connect((DEVICE, 1))
|
||||||
sock.sendall(bytes(buf))
|
sock.sendall(bytes(buf))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue