Skip to content

Commit

Permalink
Merge branch 'main' into context_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere authored Jan 31, 2025
2 parents a8c88c2 + 793e1fc commit b9d3bb6
Show file tree
Hide file tree
Showing 22 changed files with 143 additions and 260 deletions.
12 changes: 7 additions & 5 deletions Tests/test_file_libtiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,13 +1132,15 @@ def test_exif_transpose(self) -> None:
)
def test_buffering(self, test_file: str) -> None:
# load exif first
with Image.open(open(test_file, "rb", buffering=1048576)) as im:
exif = dict(im.getexif())
with open(test_file, "rb", buffering=1048576) as f:
with Image.open(f) as im:
exif = dict(im.getexif())

# load image before exif
with Image.open(open(test_file, "rb", buffering=1048576)) as im2:
im2.load()
exif_after_load = dict(im2.getexif())
with open(test_file, "rb", buffering=1048576) as f:
with Image.open(f) as im2:
im2.load()
exif_after_load = dict(im2.getexif())

assert exif == exif_after_load

Expand Down
22 changes: 22 additions & 0 deletions Tests/test_imagedraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,28 @@ def test_stroke_descender() -> None:
assert_image_similar_tofile(im, "Tests/images/imagedraw_stroke_descender.png", 6.76)


@skip_unless_feature("freetype2")
def test_stroke_inside_gap() -> None:
# Arrange
im = Image.new("RGB", (120, 130))
draw = ImageDraw.Draw(im)
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 120)

# Act
draw.text((12, 12), "i", "#f00", font, stroke_width=20)

# Assert
for y in range(im.height):
glyph = ""
for x in range(im.width):
if im.getpixel((x, y)) == (0, 0, 0):
if glyph == "started":
glyph = "ended"
else:
assert glyph != "ended", "Gap inside stroked glyph"
glyph = "started"


@skip_unless_feature("freetype2")
def test_split_word() -> None:
# Arrange
Expand Down
14 changes: 14 additions & 0 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,20 @@ def test_free_type_font_get_mask(font: ImageFont.FreeTypeFont) -> None:
assert mask.size == (108, 13)


def test_stroke_mask() -> None:
# Arrange
text = "i"

# Act
font = ImageFont.truetype(FONT_PATH, 128)
mask = font.getmask(text, stroke_width=2)

# Assert
assert mask.getpixel((34, 5)) == 255
assert mask.getpixel((38, 5)) == 0
assert mask.getpixel((42, 5)) == 255


def test_load_when_image_not_found() -> None:
with tempfile.NamedTemporaryFile(delete=False) as tmp:
pass
Expand Down
5 changes: 2 additions & 3 deletions src/PIL/BufrStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#
from __future__ import annotations

import os
from typing import IO

from . import Image, ImageFile
Expand Down Expand Up @@ -41,13 +42,11 @@ class BufrStubImageFile(ImageFile.StubImageFile):

def _open(self) -> None:
assert self.fp is not None
offset = self.fp.tell()

if not _accept(self.fp.read(4)):
msg = "Not a BUFR file"
raise SyntaxError(msg)

self.fp.seek(offset)
self.fp.seek(-4, os.SEEK_CUR)

# make something up
self._mode = "F"
Expand Down
5 changes: 2 additions & 3 deletions src/PIL/GribStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#
from __future__ import annotations

import os
from typing import IO

from . import Image, ImageFile
Expand Down Expand Up @@ -41,13 +42,11 @@ class GribStubImageFile(ImageFile.StubImageFile):

def _open(self) -> None:
assert self.fp is not None
offset = self.fp.tell()

if not _accept(self.fp.read(8)):
msg = "Not a GRIB file"
raise SyntaxError(msg)

self.fp.seek(offset)
self.fp.seek(-8, os.SEEK_CUR)

# make something up
self._mode = "F"
Expand Down
5 changes: 2 additions & 3 deletions src/PIL/Hdf5StubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#
from __future__ import annotations

import os
from typing import IO

from . import Image, ImageFile
Expand Down Expand Up @@ -41,13 +42,11 @@ class HDF5StubImageFile(ImageFile.StubImageFile):

def _open(self) -> None:
assert self.fp is not None
offset = self.fp.tell()

if not _accept(self.fp.read(8)):
msg = "Not an HDF file"
raise SyntaxError(msg)

self.fp.seek(offset)
self.fp.seek(-8, os.SEEK_CUR)

# make something up
self._mode = "F"
Expand Down
4 changes: 3 additions & 1 deletion src/PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ def draw_text(ink: int, stroke_width: float = 0) -> None:
features=features,
language=language,
stroke_width=stroke_width,
stroke_filled=True,
anchor=anchor,
ink=ink,
start=start,
Expand Down Expand Up @@ -692,7 +693,8 @@ def draw_text(ink: int, stroke_width: float = 0) -> None:
draw_text(stroke_ink, stroke_width)

# Draw normal text
draw_text(ink, 0)
if ink != stroke_ink:
draw_text(ink)
else:
# Only draw normal text
draw_text(ink)
Expand Down
1 change: 1 addition & 0 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ def fill(width: int, height: int) -> Image.core.ImagingCore:
features,
language,
stroke_width,
kwargs.get("stroke_filled", False),
anchor,
ink,
start[0],
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ def _save(
b"\0", # 12: interlace flag
)

chunks = [b"cHRM", b"gAMA", b"sBIT", b"sRGB", b"tIME"]
chunks = [b"cHRM", b"cICP", b"gAMA", b"sBIT", b"sRGB", b"tIME"]

icc = im.encoderinfo.get("icc_profile", im.info.get("icc_profile"))
if icc:
Expand Down
1 change: 1 addition & 0 deletions src/PIL/_imagingft.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Font:
features: list[str] | None,
lang: str | None,
stroke_width: float,
stroke_filled: bool,
anchor: str | None,
foreground_ink_long: int,
x_start: float,
Expand Down
Loading

0 comments on commit b9d3bb6

Please sign in to comment.