mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-10 16:22:22 +03:00
Fixed type hints
This commit is contained in:
parent
f41910c406
commit
753964ca48
|
@ -24,7 +24,7 @@ from .helper import assert_image_equal, assert_image_similar
|
||||||
(2000, 2048),
|
(2000, 2048),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_closest_power(size: int, expected_size: int):
|
def test_closest_power(size: int, expected_size: int) -> None:
|
||||||
assert _closest_power(size) == expected_size
|
assert _closest_power(size) == expected_size
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ def test_closest_power(size: int, expected_size: int):
|
||||||
((1024, 1), 11),
|
((1024, 1), 11),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_get_mipmap_count(size: tuple[int, int], expected_count: int):
|
def test_get_mipmap_count(size: tuple[int, int], expected_count: int) -> None:
|
||||||
assert _get_mipmap_count(*size) == expected_count
|
assert _get_mipmap_count(*size) == expected_count
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ def test_get_mipmap_count(size: tuple[int, int], expected_count: int):
|
||||||
)
|
)
|
||||||
def test_get_texture_size(
|
def test_get_texture_size(
|
||||||
pixel_format: VtfPF, size: tuple[int, int], expected_size: int
|
pixel_format: VtfPF, size: tuple[int, int], expected_size: int
|
||||||
):
|
) -> None:
|
||||||
assert _get_texture_size(pixel_format, *size) == expected_size
|
assert _get_texture_size(pixel_format, *size) == expected_size
|
||||||
|
|
||||||
|
|
||||||
|
@ -82,15 +82,17 @@ def test_get_texture_size(
|
||||||
("Tests/images/vtf_rgba8888.png", "Tests/images/vtf_rgba8888.vtf", "RGBA", 0),
|
("Tests/images/vtf_rgba8888.png", "Tests/images/vtf_rgba8888.vtf", "RGBA", 0),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_vtf_read(etalon_path: str, file_path: str, expected_mode: str, epsilon: float):
|
def test_vtf_read(
|
||||||
e = Image.open(etalon_path)
|
etalon_path: str, file_path: str, expected_mode: str, epsilon: float
|
||||||
f = Image.open(file_path)
|
) -> None:
|
||||||
assert f.mode == expected_mode
|
with Image.open(file_path) as f:
|
||||||
e = e.convert(expected_mode)
|
assert f.mode == expected_mode
|
||||||
if epsilon == 0:
|
with Image.open(etalon_path) as e:
|
||||||
assert_image_equal(e, f)
|
converted_e = e.convert(expected_mode)
|
||||||
else:
|
if epsilon == 0:
|
||||||
assert_image_similar(e, f, epsilon)
|
assert_image_equal(converted_e, f)
|
||||||
|
else:
|
||||||
|
assert_image_similar(converted_e, f, epsilon)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
@ -109,7 +111,7 @@ def test_vtf_read(etalon_path: str, file_path: str, expected_mode: str, epsilon:
|
||||||
)
|
)
|
||||||
def test_vtf_save(
|
def test_vtf_save(
|
||||||
pixel_format: VtfPF, file_path: str, expected_mode: str, epsilon: float, tmp_path
|
pixel_format: VtfPF, file_path: str, expected_mode: str, epsilon: float, tmp_path
|
||||||
):
|
) -> None:
|
||||||
f: Image.Image = Image.open(file_path)
|
f: Image.Image = Image.open(file_path)
|
||||||
out = (tmp_path / "tmp.vtf").as_posix()
|
out = (tmp_path / "tmp.vtf").as_posix()
|
||||||
f.save(out, pixel_format=pixel_format)
|
f.save(out, pixel_format=pixel_format)
|
||||||
|
|
|
@ -14,9 +14,9 @@ from __future__ import annotations
|
||||||
|
|
||||||
import struct
|
import struct
|
||||||
from enum import IntEnum, IntFlag
|
from enum import IntEnum, IntFlag
|
||||||
from io import BufferedIOBase, BytesIO
|
from io import BytesIO
|
||||||
from math import ceil, log
|
from math import ceil, log
|
||||||
from typing import NamedTuple
|
from typing import IO, NamedTuple
|
||||||
|
|
||||||
from . import Image, ImageFile
|
from . import Image, ImageFile
|
||||||
|
|
||||||
|
@ -149,7 +149,7 @@ def _get_texture_size(pixel_format: VtfPF, width, height):
|
||||||
raise VTFException(msg)
|
raise VTFException(msg)
|
||||||
|
|
||||||
|
|
||||||
def _get_mipmap_count(width: int, height: int):
|
def _get_mipmap_count(width: int, height: int) -> int:
|
||||||
mip_count = 1
|
mip_count = 1
|
||||||
while True:
|
while True:
|
||||||
mip_width = width >> mip_count
|
mip_width = width >> mip_count
|
||||||
|
@ -159,8 +159,9 @@ def _get_mipmap_count(width: int, height: int):
|
||||||
mip_count += 1
|
mip_count += 1
|
||||||
|
|
||||||
|
|
||||||
def _write_image(fp: BufferedIOBase, im: Image.Image, pixel_format: VtfPF):
|
def _write_image(fp: IO[bytes], im: Image.Image, pixel_format: VtfPF) -> None:
|
||||||
extents = (0, 0) + im.size
|
extents = (0, 0) + im.size
|
||||||
|
encoder_args: tuple[int, str] | tuple[str, int, int]
|
||||||
if pixel_format == VtfPF.DXT1:
|
if pixel_format == VtfPF.DXT1:
|
||||||
encoder = "bcn"
|
encoder = "bcn"
|
||||||
encoder_args = (1, "DXT1")
|
encoder_args = (1, "DXT1")
|
||||||
|
@ -309,8 +310,7 @@ class VtfImageFile(ImageFile.ImageFile):
|
||||||
self.tile = [tile]
|
self.tile = [tile]
|
||||||
|
|
||||||
|
|
||||||
def _save(im, fp, filename):
|
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
im: Image.Image
|
|
||||||
if im.mode not in ("RGB", "RGBA", "L", "LA"):
|
if im.mode not in ("RGB", "RGBA", "L", "LA"):
|
||||||
msg = f"cannot write mode {im.mode} as VTF"
|
msg = f"cannot write mode {im.mode} as VTF"
|
||||||
raise OSError(msg)
|
raise OSError(msg)
|
||||||
|
@ -406,7 +406,7 @@ def _save(im, fp, filename):
|
||||||
_write_image(fp, im, pixel_format)
|
_write_image(fp, im, pixel_format)
|
||||||
|
|
||||||
|
|
||||||
def _accept(prefix):
|
def _accept(prefix: bytes) -> bool:
|
||||||
valid_header = prefix[:4] == b"VTF\x00"
|
valid_header = prefix[:4] == b"VTF\x00"
|
||||||
valid_version = struct.unpack_from("<2I", prefix, 4) >= (7, 0)
|
valid_version = struct.unpack_from("<2I", prefix, 4) >= (7, 0)
|
||||||
return valid_header and valid_version
|
return valid_header and valid_version
|
||||||
|
|
Loading…
Reference in New Issue
Block a user