2019-06-09 16:06:09 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2019-11-28 15:50:08 +00:00
|
|
|
import click
|
2019-06-09 17:04:10 +00:00
|
|
|
from PIL import Image
|
2019-06-09 16:06:09 +00:00
|
|
|
|
2019-11-28 15:50:08 +00:00
|
|
|
from trezorlib._internal import toif
|
2019-06-09 16:06:09 +00:00
|
|
|
|
|
|
|
|
2019-11-28 15:50:08 +00:00
|
|
|
@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.
|
2019-06-09 16:06:09 +00:00
|
|
|
|
2019-11-28 15:50:08 +00:00
|
|
|
\b
|
|
|
|
Examples:
|
|
|
|
toif_convert.py somefile.jpg outfile.toif
|
|
|
|
toif_convert.py infile.toif outfile.png
|
2019-06-09 16:06:09 +00:00
|
|
|
"""
|
2019-11-28 15:50:08 +00:00
|
|
|
if infile.name.endswith(".toif") or infile.name == "-":
|
|
|
|
toi = toif.from_bytes(infile.read())
|
|
|
|
im = toi.to_image()
|
|
|
|
im.save(outfile)
|
2019-06-09 16:06:09 +00:00
|
|
|
|
2019-11-28 15:50:08 +00:00
|
|
|
elif outfile.name.endswith(".toif") or outfile.name == "-":
|
|
|
|
im = Image.open(infile)
|
|
|
|
toi = toif.from_image(im)
|
|
|
|
outfile.write(toi.to_bytes())
|
2019-06-09 16:06:09 +00:00
|
|
|
|
|
|
|
else:
|
2019-11-28 15:50:08 +00:00
|
|
|
raise click.ClickException("At least one of the arguments must end with .toif")
|
2019-06-09 16:06:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-11-28 15:50:08 +00:00
|
|
|
toif_convert()
|