-
-
Notifications
You must be signed in to change notification settings - Fork 665
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
147 additions
and
41 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# TOIF Tool | ||
|
||
A helpful CLI tool to work with TOIFs. | ||
|
||
## Installation | ||
|
||
The command `toiftool` is available within firmware repository's Poetry shell. | ||
|
||
To install the tool separate from the firmware repository, run: | ||
|
||
```bash | ||
pip install -e . | ||
``` | ||
|
||
## Displaying TOIFs | ||
|
||
To display a TOIF image in a viewer, run: | ||
|
||
```bash | ||
toiftool show <path/to/toif> | ||
``` | ||
|
||
## Converting TOIFs | ||
|
||
To convert to or from TOIF, run: | ||
|
||
```bash | ||
toiftool convert <path/to/image.toif> <path/to/output.jpg> | ||
toiftool convert <path/to/image.png> <path/to/output.toif> | ||
``` | ||
|
||
The in/out format will be determined by the file extension. | ||
|
||
## Getting information about TOIFs | ||
|
||
To get information about a TOIF, run: | ||
|
||
```bash | ||
toiftool info <path/to/toif> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[tool.poetry] | ||
name = "toiftool" | ||
version = "0.1.0" | ||
description = "" | ||
authors = ["matejcik <[email protected]>"] | ||
readme = "README.md" | ||
|
||
[tool.poetry.dependencies] | ||
python = ">=3.8" | ||
trezor = ">=0.13" | ||
click = ">=7.0,<9.0" | ||
Pillow = "^9" | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" | ||
|
||
[tool.poetry.scripts] | ||
toiftool = "toiftool:cli" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/env python3 | ||
from typing import BinaryIO | ||
|
||
import click | ||
from PIL import Image | ||
|
||
from trezorlib import toif | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
"""TOIF toolkit.""" | ||
|
||
|
||
@cli.command() | ||
@click.argument("infile", type=click.File("rb")) | ||
@click.argument("outfile", type=click.File("wb")) | ||
def convert(infile: BinaryIO, outfile: BinaryIO) -> None: | ||
"""Convert any image format to/from TOIF or vice-versa. | ||
\b | ||
Examples: | ||
toiftool convert somefile.jpg outfile.toif | ||
toiftool convert infile.toif outfile.png | ||
\b | ||
# ensure gray-scale output TOIF | ||
mogrify -colorspace gray icon.png | ||
toiftool convert icon.png icon.toif | ||
""" | ||
if infile.name.endswith(".toif") or infile.name == "-": | ||
toi = toif.from_bytes(infile.read()) | ||
im = toi.to_image() | ||
im.save(outfile) | ||
|
||
elif outfile.name.endswith(".toif") or outfile.name == "-": | ||
im = Image.open(infile) | ||
toi = toif.from_image(im) | ||
outfile.write(toi.to_bytes()) | ||
|
||
else: | ||
raise click.ClickException("At least one of the arguments must end with .toif") | ||
|
||
|
||
@cli.command() | ||
@click.argument("toif_file", type=click.File("rb")) | ||
def info(toif_file: BinaryIO) -> None: | ||
"""Print information about TOIF file.""" | ||
toif_bytes = toif_file.read() | ||
toi = toif.from_bytes(toif_bytes) | ||
click.echo(f"TOIF file: {toif_file.name}") | ||
click.echo(f"Size: {len(toif_bytes)} bytes") | ||
w, h = toi.size | ||
click.echo(f"Dimensions: {w}x{h}") | ||
click.echo(f"Format: {toi.mode}") | ||
|
||
|
||
@cli.command() | ||
@click.argument("toif_file", type=click.File("rb")) | ||
def show(toif_file: BinaryIO) -> None: | ||
"""Show TOIF file in a new window.""" | ||
toi = toif.from_bytes(toif_file.read()) | ||
im = toi.to_image() | ||
im.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |