fix(label): Use buffer for image

This commit is contained in:
Andreas Mieke 2024-04-17 22:07:05 -04:00
parent b29acedffd
commit cf7003aa9a
Signed by: zenermerps
SSH key fingerprint: SHA256:Ne+hwc5QIgYlqCuLZ0LV3301Wo/p8UoGOrGC+T6S0t8
2 changed files with 7 additions and 3 deletions

3
.gitignore vendored
View file

@ -1,7 +1,8 @@
# Created by https://www.toptal.com/developers/gitignore/api/macos,python,visualstudiocode # Created by https://www.toptal.com/developers/gitignore/api/macos,python,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,python,visualstudiocode # Edit at https://www.toptal.com/developers/gitignore?templates=macos,python,visualstudiocode
test.png # File generated by test server # File generated by test server
test.png
### macOS ### ### macOS ###
# General # General

View file

@ -5,6 +5,7 @@ from django.core.validators import MaxValueValidator
# printer supportt # printer supportt
import socket import socket
import io
# InvenTree plugin libs # InvenTree plugin libs
from plugin import InvenTreePlugin from plugin import InvenTreePlugin
@ -56,14 +57,16 @@ class PhomemoLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin):
label_image = label_image.convert('L').point(fn, mode='1') label_image = label_image.convert('L').point(fn, mode='1')
##### Convert to phomemo ##### Convert to phomemo
img_byte_arr = io.BytesIO()
label_image.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
# Send the label to the printer # Send the label to the printer
try: try:
print_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print_socket.settimeout(5) print_socket.settimeout(5)
print_socket.connect((ip_address, port)) print_socket.connect((ip_address, port))
data = label_image.tobytes("PNG") print_socket.send(img_byte_arr)
print_socket.send(data)
print_socket.close() print_socket.close()
except Exception as error: except Exception as error:
raise ConnectionError('Error connecting to printer server: ' + str(error)) raise ConnectionError('Error connecting to printer server: ' + str(error))