mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-18 04:18:10 +00:00
feat: toiftool
This commit is contained in:
parent
49bee50132
commit
6cd174d540
@ -1,40 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
import click
|
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
from trezorlib import toif
|
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
|
||||||
@click.argument("infile", type=click.File("rb"))
|
|
||||||
@click.argument("outfile", type=click.File("wb"))
|
|
||||||
def toif_convert(infile, outfile):
|
|
||||||
"""Convert any image format to/from TOIF or vice-versa.
|
|
||||||
|
|
||||||
\b
|
|
||||||
Examples:
|
|
||||||
toif_convert.py somefile.jpg outfile.toif
|
|
||||||
toif_convert.py infile.toif outfile.png
|
|
||||||
|
|
||||||
\b
|
|
||||||
# ensure gray-scale output TOIF
|
|
||||||
mogrify -colorspace gray icon.png
|
|
||||||
toif_convert.py 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")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
toif_convert()
|
|
20
poetry.lock
generated
20
poetry.lock
generated
@ -1475,6 +1475,24 @@ files = [
|
|||||||
[package.extras]
|
[package.extras]
|
||||||
tests = ["pytest", "pytest-cov"]
|
tests = ["pytest", "pytest-cov"]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toiftool"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = ""
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.8"
|
||||||
|
files = []
|
||||||
|
develop = true
|
||||||
|
|
||||||
|
[package.dependencies]
|
||||||
|
click = ">=7.0,<9.0"
|
||||||
|
Pillow = "^9"
|
||||||
|
trezor = ">=0.13"
|
||||||
|
|
||||||
|
[package.source]
|
||||||
|
type = "directory"
|
||||||
|
url = "python/tools/toiftool"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml"
|
name = "toml"
|
||||||
version = "0.10.2"
|
version = "0.10.2"
|
||||||
@ -1776,4 +1794,4 @@ testing = ["func-timeout", "jaraco.itertools", "pytest (>=6)", "pytest-black (>=
|
|||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.0"
|
lock-version = "2.0"
|
||||||
python-versions = "^3.7"
|
python-versions = "^3.7"
|
||||||
content-hash = "3e671a37f0bf4b63d64d3f17c54f7a87006d65f4747335cb6d86cc07166764ed"
|
content-hash = "40852ed8dec342c2386342584a7bd656d2e45bade0e78a15a17bf4b7b62240af"
|
||||||
|
@ -74,6 +74,7 @@ vulture = "^2.6"
|
|||||||
|
|
||||||
# tools
|
# tools
|
||||||
binsize = "^0.1.3"
|
binsize = "^0.1.3"
|
||||||
|
toiftool = {path = "./python/tools/toiftool", develop = true, python = ">=3.8"}
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
scan-build = "*"
|
scan-build = "*"
|
||||||
|
40
python/tools/toiftool/README.md
Normal file
40
python/tools/toiftool/README.md
Normal file
@ -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>
|
||||||
|
```
|
19
python/tools/toiftool/pyproject.toml
Normal file
19
python/tools/toiftool/pyproject.toml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
[tool.poetry]
|
||||||
|
name = "toiftool"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = ""
|
||||||
|
authors = ["matejcik <ja@matejcik.cz>"]
|
||||||
|
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"
|
68
python/tools/toiftool/toiftool.py
Normal file
68
python/tools/toiftool/toiftool.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user