Skip to content

Commit 09329f6

Browse files
committed
Added offset option in cli
1 parent 11e4e54 commit 09329f6

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,4 @@ dmypy.json
9999
.pyre/
100100
.idea
101101
data/
102+
nimmy.log

NiimPrintX/cli/command.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ def niimbot_cli(ctx, verbose):
5252
show_default=True,
5353
help="Print quantity",
5454
)
55+
@click.option(
56+
"--vo",
57+
"vertical_offset",
58+
default=0,
59+
show_default=True,
60+
help="Vertical offset in pixels",
61+
)
62+
@click.option(
63+
"--ho",
64+
"horizontal_offset",
65+
default=0,
66+
show_default=True,
67+
help="Horizontal offset in pixels",
68+
)
5569
@click.option(
5670
"-r",
5771
"--rotate",
@@ -67,7 +81,7 @@ def niimbot_cli(ctx, verbose):
6781
required=True,
6882
help="Image path",
6983
)
70-
def print_command(model, density, rotate, image, quantity):
84+
def print_command(model, density, rotate, image, quantity, vertical_offset, horizontal_offset):
7185
logger.info(f"Niimbot Printing Start")
7286

7387
if model in ("b1", "b18", "b21"):
@@ -84,19 +98,20 @@ def print_command(model, density, rotate, image, quantity):
8498
# PIL library rotates counterclockwise, so we need to multiply by -1
8599
image = image.rotate(-int(rotate), expand=True)
86100
assert image.width <= max_width_px, f"Image width too big for {model.upper()}"
87-
asyncio.run(_print(model, density, image, quantity))
101+
asyncio.run(_print(model, density, image, quantity, vertical_offset, horizontal_offset))
88102
except Exception as e:
89103
logger.info(f"{e}")
90104

91105

92-
async def _print(model, density, image, quantity):
106+
async def _print(model, density, image, quantity, vertical_offset, horizontal_offset):
93107
try:
94108
print_info("Starting print job")
95109
device = await find_device(model)
96110
printer = PrinterClient(device)
97111
if await printer.connect():
98112
print(f"Connected to {device.name}")
99-
await printer.print_image(image, density=density, quantity=quantity)
113+
await printer.print_image(image, density=density, quantity=quantity, vertical_offset=vertical_offset,
114+
horizontal_offset=horizontal_offset)
100115
print_success("Print job completed")
101116
await printer.disconnect()
102117
except Exception as e:
@@ -134,7 +149,7 @@ async def _info(model):
134149
except Exception as e:
135150
logger.debug(f"{e}")
136151
print_error(e)
137-
#await printer.disconnect()
152+
# await printer.disconnect()
138153

139154

140155
cli = click.CommandCollection(sources=[niimbot_cli])

NiimPrintX/nimmy/printer.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
from .logger_config import get_logger
99
from .packet import NiimbotPacket, packet_to_int
1010

11-
1211
from devtools import debug
1312

1413
logger = get_logger()
1514

15+
1616
class InfoEnum(enum.IntEnum):
1717
DENSITY = 1
1818
PRINTSPEED = 2
@@ -125,15 +125,16 @@ def notification_handler(self, sender, data):
125125
self.notification_data = data
126126
self.notification_event.set()
127127

128-
async def print_image(self, image: Image, density: int = 3, quantity: int = 1):
128+
async def print_image(self, image: Image, density: int = 3, quantity: int = 1, vertical_offset= 0,
129+
horizontal_offset = 0):
129130
await self.set_label_density(density)
130131
await self.set_label_type(1)
131132
await self.start_print()
132133
await self.start_page_print()
133134
await self.set_dimension(image.height, image.width)
134135
await self.set_quantity(quantity)
135136

136-
for pkt in self._encode_image(image):
137+
for pkt in self._encode_image(image, vertical_offset, horizontal_offset):
137138
# Send each line and wait for a response or status check
138139
await self.write_raw(pkt)
139140
# Adding a short delay or status check here can help manage buffer issues
@@ -150,8 +151,19 @@ async def print_image(self, image: Image, density: int = 3, quantity: int = 1):
150151

151152
await self.end_print()
152153

153-
def _encode_image(self, image: Image):
154+
def _encode_image(self, image: Image, vertical_offset=0, horizontal_offset=0):
155+
# Convert the image to monochrome
154156
img = ImageOps.invert(image.convert("L")).convert("1")
157+
158+
# Apply horizontal offset
159+
if horizontal_offset > 0:
160+
img = ImageOps.expand(img, border=(horizontal_offset, 0, 0, 0), fill=1)
161+
else:
162+
img = img.crop((-horizontal_offset, 0, img.width, img.height))
163+
164+
# Add vertical padding for vertical offset
165+
img = ImageOps.expand(img, border=(0, vertical_offset, 0, 0), fill=1)
166+
155167
for y in range(img.height):
156168
line_data = [img.getpixel((x, y)) for x in range(img.width)]
157169
line_data = "".join("0" if pix == 0 else "1" for pix in line_data)

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,17 @@ python -m NiimPrintX.ui
120120
```
121121

122122
## Contributing
123-
Contributions are welcome! Please fork the repository and submit a pull request with your improvements.
123+
Contributions are welcome! Please fork the repository and submit a pull request with your improvements.
124+
125+
## Credits
126+
* Icons made by [Dave Gandy](https://www.flaticon.com/authors/dave-gandy) from [www.flaticon.com](https://www.flaticon.com/)
127+
* Icons made by [Pixel perfect](https://www.flaticon.com/authors/pixel-perfect) from [www.flaticon.com](https://www.flaticon.com/)
128+
* Icons made by [Freepik](https://www.freepik.com) from [www.flaticon.com](https://www.flaticon.com/)
129+
* Icons made by [rddrt](https://www.flaticon.com/authors/rddrt) from [www.flaticon.com](https://www.flaticon.com/)
130+
* Icons made by [Icongeek26](https://www.flaticon.com/authors/icongeek26) from [www.flaticon.com](https://www.flaticon.com/)
131+
* Icons made by [SyafriStudio](https://www.flaticon.com/authors/syafristudio) from [www.flaticon.com](https://www.flaticon.com/)
132+
* Icons made by [Wahyu Adam](https://www.flaticon.com/authors/wahyu-adam) from [www.flaticon.com](https://www.flaticon.com/)
133+
* Icons made by [meaicon](https://www.flaticon.com/authors/meaicon) from [www.flaticon.com](https://www.flaticon.com/)
134+
* Icons made by [IconKanan](https://www.flaticon.com/authors/iconkanan) from [www.flaticon.com](https://www.flaticon.com/)
135+
* Icons made by [kornkun](https://www.flaticon.com/authors/kornkun) from [www.flaticon.com](https://www.flaticon.com/)
136+
* Icons made by [Rifaldi Ridha Aisy](https://www.flaticon.com/authors/rifaldi-ridha-aisy) from [www.flaticon.com](https://www.flaticon.com/)

bin/process_png.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
import shutil
3+
from PIL import Image
4+
import subprocess
5+
import click
6+
import glob
7+
8+
9+
@click.command()
10+
@click.argument('image_directory', type=click.Path(exists=True))
11+
def process_images(image_directory):
12+
# Create subdirectories
13+
original_dir = os.path.join(image_directory, 'original')
14+
resized_dir = os.path.join(image_directory, '50x50')
15+
16+
os.makedirs(original_dir, exist_ok=True)
17+
os.makedirs(resized_dir, exist_ok=True)
18+
19+
# Copy files to subdirectories
20+
for filename in glob.glob(os.path.join(image_directory, '*.png')):
21+
shutil.copy(filename, original_dir)
22+
shutil.copy(filename, resized_dir)
23+
24+
# Run mogrify commands
25+
subprocess.run(['mogrify', '-resize', '50x50', os.path.join(resized_dir, '*.png')])
26+
subprocess.run(['mogrify', '-format', 'png', '-alpha', 'on', os.path.join(resized_dir, '*.png')])
27+
subprocess.run(['mogrify', '-fill', 'black', '-colorize', '100', os.path.join(resized_dir, '*.png')])
28+
29+
# Process images with PIL
30+
for image_path in glob.glob(os.path.join(resized_dir, '*.png')):
31+
with Image.open(image_path).convert("RGBA").resize((50, 50), Image.Resampling.LANCZOS) as image:
32+
image.save(image_path)
33+
34+
35+
if __name__ == '__main__':
36+
process_images()

0 commit comments

Comments
 (0)