Add rotation support, update readme
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 1.0 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 744 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 84 KiB |
+18
-13
@@ -10,7 +10,7 @@ from niimprint import BluetoothTransport, PrinterClient, SerialTransport
|
|||||||
@click.option(
|
@click.option(
|
||||||
"-m",
|
"-m",
|
||||||
"--model",
|
"--model",
|
||||||
type=click.Choice(["b21", "d11"], False),
|
type=click.Choice(["b1", "b21", "d11"], False),
|
||||||
default="b21",
|
default="b21",
|
||||||
show_default=True,
|
show_default=True,
|
||||||
help="Niimbot printer model",
|
help="Niimbot printer model",
|
||||||
@@ -36,6 +36,14 @@ from niimprint import BluetoothTransport, PrinterClient, SerialTransport
|
|||||||
show_default=True,
|
show_default=True,
|
||||||
help="Print density",
|
help="Print density",
|
||||||
)
|
)
|
||||||
|
@click.option(
|
||||||
|
"-r",
|
||||||
|
"--rotate",
|
||||||
|
type=click.Choice(["0", "90", "180", "270"]),
|
||||||
|
default="0",
|
||||||
|
show_default=True,
|
||||||
|
help="Image rotation (clockwise)",
|
||||||
|
)
|
||||||
@click.option(
|
@click.option(
|
||||||
"-i",
|
"-i",
|
||||||
"--image",
|
"--image",
|
||||||
@@ -43,10 +51,9 @@ from niimprint import BluetoothTransport, PrinterClient, SerialTransport
|
|||||||
required=True,
|
required=True,
|
||||||
help="Image path",
|
help="Image path",
|
||||||
)
|
)
|
||||||
def print_cmd(model, conn, addr, density, image):
|
def print_cmd(model, conn, addr, density, rotate, image):
|
||||||
assert model != "d11", "D11 support may be broken (test yourself)"
|
|
||||||
|
|
||||||
if conn == "bluetooth":
|
if conn == "bluetooth":
|
||||||
|
assert conn is not None, "--addr argument required for bluetooth connection"
|
||||||
addr = addr.upper()
|
addr = addr.upper()
|
||||||
assert re.fullmatch(r"([0-9A-F]{2}:){5}([0-9A-F]{2})", addr), "Bad MAC address"
|
assert re.fullmatch(r"([0-9A-F]{2}:){5}([0-9A-F]{2})", addr), "Bad MAC address"
|
||||||
transport = BluetoothTransport(addr)
|
transport = BluetoothTransport(addr)
|
||||||
@@ -54,18 +61,16 @@ def print_cmd(model, conn, addr, density, image):
|
|||||||
port = addr if addr is not None else "auto"
|
port = addr if addr is not None else "auto"
|
||||||
transport = SerialTransport(port=port)
|
transport = SerialTransport(port=port)
|
||||||
|
|
||||||
if model == "b21":
|
if model in ("b1", "b21"):
|
||||||
# This may be wrong, but B21 doesn't accept anything larger. It's just shy of
|
max_width_px = 384
|
||||||
# 50mm * 8 px/mm = 400px (for vertical space it's always 8 px/mm), so lgtm.
|
|
||||||
max_height_px = 384
|
|
||||||
if model == "d11":
|
if model == "d11":
|
||||||
max_height_px = 100 # I don't have D11 to test
|
max_width_px = 96
|
||||||
|
|
||||||
# Image is printed left-to-right. Generally, we expect image width to be larger
|
|
||||||
# than image height, because that's the usual sticker aspect ratio.
|
|
||||||
image = Image.open(image)
|
image = Image.open(image)
|
||||||
assert image.width > image.height, "Are you sure image rotation is right?"
|
if rotate != "0":
|
||||||
assert image.height <= max_height_px, f"Image height too big for {model}"
|
# PIL library rotates counter clockwise, so we need to multiply by -1
|
||||||
|
image = image.rotate(-int(rotate), expand=True)
|
||||||
|
assert image.width <= max_width_px, f"Image width too big for {model.upper()}"
|
||||||
|
|
||||||
printer = PrinterClient(transport)
|
printer = PrinterClient(transport)
|
||||||
printer.print_image(image, density=density)
|
printer.print_image(image, density=density)
|
||||||
|
|||||||
@@ -101,8 +101,6 @@ class PrinterClient:
|
|||||||
self._packetbuf = bytearray()
|
self._packetbuf = bytearray()
|
||||||
|
|
||||||
def print_image(self, image: Image, density: int = 3):
|
def print_image(self, image: Image, density: int = 3):
|
||||||
# Internally, we're slicing the image vertically, so it's convenient to rotate
|
|
||||||
image = image.transpose(Image.ROTATE_270)
|
|
||||||
self.set_label_density(density)
|
self.set_label_density(density)
|
||||||
self.set_label_type(1)
|
self.set_label_type(1)
|
||||||
self.start_print()
|
self.start_print()
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
- Tested on Niimbot B21 and Python 3.11. Niimbot D11 support may be broken!
|
- Tested on Niimbot B21 and Python 3.11. Niimbot D11 support may be broken!
|
||||||
- Added transport abstraction: switch between bluetooth and USB (serial)
|
- Added transport abstraction: switch between bluetooth and USB (serial)
|
||||||
|
- Disabled checksum calculation for image encoding (works fine without it so far)
|
||||||
- Switched to [click](https://click.palletsprojects.com/) CLI library instead of argparse
|
- Switched to [click](https://click.palletsprojects.com/) CLI library instead of argparse
|
||||||
- Integrated [pyproject.toml](https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/) and [poetry](https://python-poetry.org)
|
- Integrated [pyproject.toml](https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/) and [poetry](https://python-poetry.org)
|
||||||
- Integrated [pre-commit](https://pre-commit.com/) and [ruff](https://docs.astral.sh/ruff/), re-formatted all files
|
- Integrated [pre-commit](https://pre-commit.com/) and [ruff](https://docs.astral.sh/ruff/), re-formatted all files
|
||||||
@@ -21,40 +22,60 @@ $ python niimprint --help
|
|||||||
Usage: niimprint [OPTIONS]
|
Usage: niimprint [OPTIONS]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-m, --model [b21|d11] Niimbot printer model [default: b21]
|
-m, --model [b1|b21|d11] Niimbot printer model [default: b21]
|
||||||
-c, --conn [usb|bluetooth] Connection type [default: usb]
|
-c, --conn [usb|bluetooth] Connection type [default: usb]
|
||||||
-a, --addr TEXT Bluetooth MAC address OR serial device path
|
-a, --addr TEXT Bluetooth MAC address OR serial device path
|
||||||
-d, --density INTEGER RANGE Print density [default: 5; 1<=x<=5]
|
-d, --density INTEGER RANGE Print density [default: 5; 1<=x<=5]
|
||||||
|
-r, --rotate [0|90|180|270] Image rotation (clockwise) [default: 0]
|
||||||
-i, --image PATH Image path [required]
|
-i, --image PATH Image path [required]
|
||||||
--help Show this message and exit.
|
--help Show this message and exit.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Image orientation:
|
||||||
|
|
||||||
|
Generally, the image comes out of the printer with the same orientation you see it on your screen. You can have your input image rotated as you like, but adjust its orientation by passing `-r <...>` flag. See the image below for clarification.
|
||||||
|
|
||||||
|
[]()
|
||||||
|
|
||||||
|
<!-- Excalidraw link: https://excalidraw.com/#json=vYHMBohMn5GeB-5M6SNch,TsxRmh_WKUfzYjL183FGfg -->
|
||||||
|
|
||||||
|
### Image resolution:
|
||||||
|
|
||||||
|
As far as we've tested, Niimbot printers have **8 pixels per mm** (~203 dpi) resolution. The CLI prints the image you provided as-is, without any checks of the actual label size, so be careful. However the script will check if the image width is too big for selected printer. The maximum width in pixels is usually slightly less than specified maximum width in mm:
|
||||||
|
|
||||||
|
- **B21, B1**: max 384 pixels (almost equal to 50 mm * 8 px/mm = 400)
|
||||||
|
- **D11**: max 96 pixels (almost equal to 15 mm * 8 px/mm = 120)
|
||||||
|
|
||||||
|
### USB connection:
|
||||||
|
|
||||||
|
For USB connection, you can omit the `--addr` argument and let the script auto-detect the serial port. However, it will fail if there're multiple available ports. On linux, serial ports can be found at `/dev/ttyUSB*`, `/dev/ttyACM*` or `/dev/serial/*`. On windows, they will be named like `COM1`, `COM2` etc. Check the device manager to choose the correct one.
|
||||||
|
|
||||||
|
### Bluetooth connection:
|
||||||
|
|
||||||
|
It seems like B21 (and maybe other models?) has two bluetooth adresses. For me, they start with `C2:E1` and `E2:E1` respectively. Connection works only if you disconnect from `C2:E1` and connect to `E2:E1`. Also after connecting to `E2:E1` via bluetoothctl I always get `org.bluez.Error.NotAvailable br-connection-profile-unavailable` error, but printing works fine regardless.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
**B21, USB connection, 30x15 mm (240x120 px) label**
|
**B21, USB connection, 30x15 mm (240x120 px) label**
|
||||||
|
|
||||||
```
|
```
|
||||||
python niimprint -a /dev/ttyACM0 -i examples/B21_30x15mm_240x120px.png
|
python niimprint -c usb -a /dev/ttyACM0 -r 90 -i examples/B21_30x15mm_240x120px.png
|
||||||
```
|
```
|
||||||
|
|
||||||
Notes: on linux, you can try to omit `--addr` option and let the script to auto-detect the serial port (it will fail if there're multiple available ports). On windows, serial ports will be named like `COM1`, `COM2` etc. (check in device manager).
|
[]()
|
||||||
|
|
||||||
**B21, bluetooth connection, same label**
|
**B21, Bluetooth connection, 80x50 mm (640x384 px) label**
|
||||||
|
|
||||||
```
|
```
|
||||||
python niimprint -c bluetooth -a "E2:E1:08:03:09:87" -i examples/B21_30x15mm_240x120px.png
|
python niimprint -c bluetooth -a "E2:E1:08:03:09:87" -r 90 -i examples/B21_80x50mm_640x384px.png
|
||||||
```
|
```
|
||||||
|
|
||||||
Notes: it seems like B21 has two bluetooth adresses starting with `C2:E1` and `E2:E1` respectively. It works only if you disconnect from `C2:E1` and connect to `E2:E1`. Also after connecting to `E2:E1` in bluetoothctl I always get `org.bluez.Error.NotAvailable br-connection-profile-unavailable` error, but printing works regardless.
|
[]()
|
||||||
|
|
||||||
**D11:** completly untested, however original fork supports it. If you have D11 at hand and willing to test, please contact me!
|
**D11**
|
||||||
|
|
||||||
## Image vs Label size
|
Completly untested yet, however original fork supports it. If you have D11 at hand and willing to test, please open an issue!
|
||||||
|
|
||||||
### Niimbot B21
|
## Licence
|
||||||
|
|
||||||
According to my observations, B21 has **8 pixels per mm** resolution. But there's one catch: printer specs say it supports up to 50mm-wide labels (which sould translate to `50 * 8 = 400` pixels). However trying to print anything larger than `384` pixels resulted in error. My guess it's the actual hardware limit, which is "almost equal" to stated 50 mm.
|
[MIT](https://choosealicense.com/licenses/mit/). Originally developed by [kjy00302](https://github.com/kjy00302), forked & enhanced by [AndBondStyle](https://github.com/AndBondStyle)
|
||||||
|
|
||||||
### Niimbot D11
|
|
||||||
|
|
||||||
???
|
|
||||||
|
|||||||
Reference in New Issue
Block a user