mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-11 16:00:57 +00:00
python: export toif as public module
This commit is contained in:
parent
43b7ccefc7
commit
032a65e1cf
@ -6,7 +6,7 @@ import sys
|
|||||||
import click
|
import click
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from trezorlib._internal import toif
|
from trezorlib import toif
|
||||||
|
|
||||||
HERE = Path(__file__).parent.resolve()
|
HERE = Path(__file__).parent.resolve()
|
||||||
ROOT = HERE.parent.parent
|
ROOT = HERE.parent.parent
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
import click
|
import click
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from trezorlib._internal import toif
|
from trezorlib import toif
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
|
@ -3,9 +3,13 @@ import zlib
|
|||||||
from typing import Sequence, Tuple
|
from typing import Sequence, Tuple
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
from .. import firmware
|
try:
|
||||||
|
from PIL import Image
|
||||||
|
except ImportError:
|
||||||
|
Image = None
|
||||||
|
|
||||||
|
from . import firmware
|
||||||
|
|
||||||
RGBPixel = Tuple[int, int, int]
|
RGBPixel = Tuple[int, int, int]
|
||||||
|
|
||||||
@ -69,7 +73,10 @@ class Toif:
|
|||||||
else:
|
else:
|
||||||
return width * height * 2
|
return width * height * 2
|
||||||
|
|
||||||
def to_image(self) -> Image:
|
def to_image(self) -> "Image":
|
||||||
|
if Image is None:
|
||||||
|
raise RuntimeError("PIL is not available. Please install via 'pip install Pillow'")
|
||||||
|
|
||||||
uncompressed = _decompress(self.data)
|
uncompressed = _decompress(self.data)
|
||||||
expected_size = self._expected_data_length()
|
expected_size = self._expected_data_length()
|
||||||
if len(uncompressed) != expected_size:
|
if len(uncompressed) != expected_size:
|
||||||
@ -109,7 +116,10 @@ def load(filename: str) -> Toif:
|
|||||||
return from_bytes(f.read())
|
return from_bytes(f.read())
|
||||||
|
|
||||||
|
|
||||||
def from_image(image: Image, background=(0, 0, 0, 255)) -> Toif:
|
def from_image(image: "Image", background=(0, 0, 0, 255)) -> Toif:
|
||||||
|
if Image is None:
|
||||||
|
raise RuntimeError("PIL is not available. Please install via 'pip install Pillow'")
|
||||||
|
|
||||||
if image.mode == "RGBA":
|
if image.mode == "RGBA":
|
||||||
background = Image.new("RGBA", image.size, background)
|
background = Image.new("RGBA", image.size, background)
|
||||||
blend = Image.alpha_composite(background, image)
|
blend = Image.alpha_composite(background, image)
|
Loading…
Reference in New Issue
Block a user