mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-10-28 06:31:32 +03:00
Added type hints (#9269)
This commit is contained in:
commit
cc4ca5bf17
|
|
@ -33,7 +33,7 @@ def test_multiple_load_operations() -> None:
|
||||||
assert_image_equal_tofile(im, "Tests/images/gbr.png")
|
assert_image_equal_tofile(im, "Tests/images/gbr.png")
|
||||||
|
|
||||||
|
|
||||||
def create_gbr_image(info: dict[str, int] = {}, magic_number=b"") -> BytesIO:
|
def create_gbr_image(info: dict[str, int] = {}, magic_number: bytes = b"") -> BytesIO:
|
||||||
return BytesIO(
|
return BytesIO(
|
||||||
b"".join(
|
b"".join(
|
||||||
_binary.o32be(i)
|
_binary.o32be(i)
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ TEST_FILE = "Tests/images/iptc.jpg"
|
||||||
|
|
||||||
|
|
||||||
def create_iptc_image(info: dict[str, int] = {}) -> BytesIO:
|
def create_iptc_image(info: dict[str, int] = {}) -> BytesIO:
|
||||||
def field(tag, value):
|
def field(tag: tuple[int, int], value: bytes) -> bytes:
|
||||||
return bytes((0x1C,) + tag + (0, len(value))) + value
|
return bytes((0x1C,) + tag + (0, len(value))) + value
|
||||||
|
|
||||||
data = field((3, 60), bytes((info.get("layers", 1), info.get("component", 0))))
|
data = field((3, 60), bytes((info.get("layers", 1), info.get("component", 0))))
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,10 @@ def test_get_length(font: ImageFont.FreeTypeFont) -> None:
|
||||||
assert ImageText.Text("y", font).get_length() == 12
|
assert ImageText.Text("y", font).get_length() == 12
|
||||||
assert ImageText.Text("a", font).get_length() == 12
|
assert ImageText.Text("a", font).get_length() == 12
|
||||||
|
|
||||||
|
text = ImageText.Text("\n", font)
|
||||||
|
with pytest.raises(ValueError, match="can't measure length of multiline text"):
|
||||||
|
text.get_length()
|
||||||
|
|
||||||
|
|
||||||
def test_get_bbox(font: ImageFont.FreeTypeFont) -> None:
|
def test_get_bbox(font: ImageFont.FreeTypeFont) -> None:
|
||||||
assert ImageText.Text("A", font).get_bbox() == (0, 4, 12, 16)
|
assert ImageText.Text("A", font).get_bbox() == (0, 4, 12, 16)
|
||||||
|
|
@ -45,6 +49,7 @@ def test_standard_embedded_color(layout_engine: ImageFont.Layout) -> None:
|
||||||
font = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine)
|
font = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine)
|
||||||
text = ImageText.Text("Hello World!", font)
|
text = ImageText.Text("Hello World!", font)
|
||||||
text.embed_color()
|
text.embed_color()
|
||||||
|
assert text.get_length() == 288
|
||||||
|
|
||||||
im = Image.new("RGB", (300, 64), "white")
|
im = Image.new("RGB", (300, 64), "white")
|
||||||
draw = ImageDraw.Draw(im)
|
draw = ImageDraw.Draw(im)
|
||||||
|
|
@ -52,6 +57,12 @@ def test_standard_embedded_color(layout_engine: ImageFont.Layout) -> None:
|
||||||
|
|
||||||
assert_image_similar_tofile(im, "Tests/images/standard_embedded.png", 3.1)
|
assert_image_similar_tofile(im, "Tests/images/standard_embedded.png", 3.1)
|
||||||
|
|
||||||
|
text = ImageText.Text("", mode="1")
|
||||||
|
with pytest.raises(
|
||||||
|
ValueError, match="Embedded color supported only in RGB and RGBA modes"
|
||||||
|
):
|
||||||
|
text.embed_color()
|
||||||
|
|
||||||
|
|
||||||
@skip_unless_feature("freetype2")
|
@skip_unless_feature("freetype2")
|
||||||
def test_stroke() -> None:
|
def test_stroke() -> None:
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ class Text:
|
||||||
else:
|
else:
|
||||||
return "L"
|
return "L"
|
||||||
|
|
||||||
def get_length(self):
|
def get_length(self) -> float:
|
||||||
"""
|
"""
|
||||||
Returns length (in pixels with 1/64 precision) of text.
|
Returns length (in pixels with 1/64 precision) of text.
|
||||||
|
|
||||||
|
|
@ -130,8 +130,11 @@ class Text:
|
||||||
|
|
||||||
:return: Either width for horizontal text, or height for vertical text.
|
:return: Either width for horizontal text, or height for vertical text.
|
||||||
"""
|
"""
|
||||||
split_character = "\n" if isinstance(self.text, str) else b"\n"
|
if isinstance(self.text, str):
|
||||||
if split_character in self.text:
|
multiline = "\n" in self.text
|
||||||
|
else:
|
||||||
|
multiline = b"\n" in self.text
|
||||||
|
if multiline:
|
||||||
msg = "can't measure length of multiline text"
|
msg = "can't measure length of multiline text"
|
||||||
raise ValueError(msg)
|
raise ValueError(msg)
|
||||||
return self.font.getlength(
|
return self.font.getlength(
|
||||||
|
|
@ -313,6 +316,5 @@ class Text:
|
||||||
max(bbox[3], bbox_line[3]),
|
max(bbox[3], bbox_line[3]),
|
||||||
)
|
)
|
||||||
|
|
||||||
if bbox is None:
|
assert bbox is not None
|
||||||
return xy[0], xy[1], xy[0], xy[1]
|
|
||||||
return bbox
|
return bbox
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user