Skip to content

Commit 09b5eb8

Browse files
committed
Allow printing images
1 parent b8137b0 commit 09b5eb8

File tree

5 files changed

+98
-4
lines changed

5 files changed

+98
-4
lines changed

Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
install:
2+
python3 setup.py install
3+
4+
build: clean
5+
python3 -m build
6+
twine check dist/*
7+
8+
clean:
9+
rm -rf build dist phomemo_printer.egginfo
10+
11+
bump-version:
12+
current_version=$(cat phomemo_printer/version.py | cut -f ' ' -d 2 | tr -d \")
13+
major=$(echo "${current_version}" | cut -d '.' -f 1)
14+
minor=$(echo "${current_version}" | cut -d '.' -f 2)
15+
patch=$(echo "${current_version}" | cut -d '.' -f 3)
16+
new_patch=$((${patcH} + 1))
17+
new_version="${major}.${minor}.${new_patch}"
18+
sed -i "s/${current_version}/${new_version}/"
19+
20+
release: build
21+
python3 -m twine upload dist/*

phomemo_printer/ESCPOS_printer.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .pixel_sans.charset import charset
33
from .ESCPOS_constants import *
44
import socket
5+
from PIL import Image
56

67

78
class Printer:
@@ -137,3 +138,70 @@ def print_charset(self):
137138
]
138139
)
139140
self.print_text(char_string_split)
141+
142+
# from https://github.com/vivier/phomemo-tools
143+
def print_image(self, image_path):
144+
"""
145+
Print an image
146+
147+
Args:
148+
image_path (str): Path to an image file to print
149+
"""
150+
image = Image.open(image_path)
151+
if image.width > image.height:
152+
image = image.transpose(Image.ROTATE_90)
153+
154+
# width 384 dots
155+
IMAGE_WIDTH_BYTES = 70
156+
IMAGE_WIDTH_BITS = IMAGE_WIDTH_BYTES * 8
157+
image = image.resize(
158+
size=(IMAGE_WIDTH_BITS, int(image.height * IMAGE_WIDTH_BITS / image.width))
159+
)
160+
161+
# black&white printer: dithering
162+
image = image.convert(mode="1")
163+
164+
self._print_bytes(HEADER)
165+
for start_index in range(0, image.height, 256):
166+
end_index = (
167+
start_index + 256 if image.height - 256 > start_index else image.height
168+
)
169+
line_height = end_index - start_index
170+
171+
BLOCK_MARKER = (
172+
GSV0
173+
+ bytes([IMAGE_WIDTH_BYTES])
174+
+ b"\x00"
175+
+ bytes([line_height - 1])
176+
+ b"\x00"
177+
)
178+
self._print_bytes(BLOCK_MARKER)
179+
180+
image_lines = []
181+
for image_line_index in range(line_height):
182+
image_line = b""
183+
for byte_start in range(int(image.width / 8)):
184+
byte = 0
185+
for bit in range(8):
186+
if (
187+
image.getpixel(
188+
(byte_start * 8 + bit, image_line_index + start_index)
189+
)
190+
== 0
191+
):
192+
byte |= 1 << (7 - bit)
193+
# 0x0a breaks the rendering
194+
# 0x0a alone is processed like LineFeed by the printe
195+
if byte == 0x0A:
196+
byte = 0x14
197+
# self._print_bytes(byte.to_bytes(1, 'little'))
198+
image_line += byte.to_bytes(1, "little")
199+
200+
image_lines.append(image_line)
201+
202+
for l in image_lines:
203+
self._print_bytes(l)
204+
205+
self._print_bytes(PRINT_FEED)
206+
self._print_bytes(PRINT_FEED)
207+
self._print_bytes(FOOTER)

phomemo_printer/command_line.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ def main(args):
88

99
if args.print_charset:
1010
printer.print_charset()
11-
else:
12-
text = args.text
13-
printer.print_text(text)
11+
elif args.text:
12+
printer.print_text(args.text)
13+
elif args.image:
14+
printer.print_image(args.image)
1415

1516
printer.close()
1617

@@ -32,6 +33,9 @@ def cli():
3233
action="store_true",
3334
default=False,
3435
)
36+
group.add_argument(
37+
"-i", "--image", type=str, help="Image file to print", required=False
38+
)
3539
parser.add_argument(
3640
"-a",
3741
"--bluetooth_address",

phomemo_printer/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.0.3"
1+
__version__ = "1.0.4"

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
argparse
2+
Pillow

0 commit comments

Comments
 (0)