Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Tests/test_imagecms.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ def test_exceptions() -> None:
psRGB = ImageCms.createProfile("sRGB")
pLab = ImageCms.createProfile("LAB")
t = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB")
with pytest.raises(ValueError, match="mode mismatch"):
t.apply(hopper("RGBA"))
with pytest.raises(ValueError, match="mode mismatch"):
t.apply(hopper("LAB"), hopper("RGBA"))
with pytest.raises(ValueError, match="mode mismatch"):
t.apply_in_place(hopper("RGBA"))

Expand Down
16 changes: 9 additions & 7 deletions src/PIL/ImageCms.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,19 +317,21 @@ def point(self, im: Image.Image) -> Image.Image:
return self.apply(im)

def apply(self, im: Image.Image, imOut: Image.Image | None = None) -> Image.Image:
if imOut is None:
if im.mode != self.input_mode:
msg = "mode mismatch"
raise ValueError(msg)
if imOut is not None:
if imOut.mode != self.output_mode:
msg = "mode mismatch"
raise ValueError(msg)
else:
imOut = Image.new(self.output_mode, im.size, None)
self.transform.apply(im.getim(), imOut.getim())
imOut.info["icc_profile"] = self.output_profile.tobytes()
return imOut

def apply_in_place(self, im: Image.Image) -> Image.Image:
if im.mode != self.output_mode:
msg = "mode mismatch"
raise ValueError(msg) # wrong output mode
self.transform.apply(im.getim(), im.getim())
im.info["icc_profile"] = self.output_profile.tobytes()
return im
return self.apply(im, im)


def get_display_profile(handle: SupportsInt | None = None) -> ImageCmsProfile | None:
Expand Down
Loading