Skip to content

Commit

Permalink
add type hints for ImageCms.{ImageCmsProfile,ImageCmsTransform}
Browse files Browse the repository at this point in the history
  • Loading branch information
nulano committed Jan 1, 2024
1 parent 02fd759 commit 0eeb3f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2903,7 +2903,7 @@ def _check_size(size):
return True


def new(mode, size, color=0):
def new(mode, size, color=0) -> Image:
"""
Creates a new image with the given mode and size.
Expand Down
31 changes: 16 additions & 15 deletions src/PIL/ImageCms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import sys
from enum import IntEnum
from typing import BinaryIO

from . import Image

Expand Down Expand Up @@ -157,7 +158,7 @@ class Direction(IntEnum):


class ImageCmsProfile:
def __init__(self, profile):
def __init__(self, profile: str | BinaryIO | core.CmsProfile) -> None:
"""
:param profile: Either a string representing a filename,
a file like object containing a profile or a
Expand All @@ -180,16 +181,16 @@ def __init__(self, profile):
elif isinstance(profile, _imagingcms.CmsProfile):
self._set(profile)
else:
msg = "Invalid type for Profile"
msg = "Invalid type for Profile" # type: ignore[unreachable]
raise TypeError(msg)

def _set(self, profile, filename=None):
def _set(self, profile: core.CmsProfile, filename: str | None = None) -> None:
self.profile = profile
self.filename = filename
self.product_name = None # profile.product_name
self.product_info = None # profile.product_info

def tobytes(self):
def tobytes(self) -> bytes:
"""
Returns the profile in a format suitable for embedding in
saved images.
Expand All @@ -211,14 +212,14 @@ class ImageCmsTransform(Image.ImagePointHandler):

def __init__(
self,
input,
output,
input_mode,
output_mode,
intent=Intent.PERCEPTUAL,
proof=None,
proof_intent=Intent.ABSOLUTE_COLORIMETRIC,
flags=0,
input: ImageCmsProfile,
output: ImageCmsProfile,
input_mode: str,
output_mode: str,
intent: Intent = Intent.PERCEPTUAL,
proof: ImageCmsProfile | None = None,
proof_intent: Intent = Intent.ABSOLUTE_COLORIMETRIC,
flags: int = 0,
):
if proof is None:
self.transform = core.buildTransform(
Expand All @@ -241,18 +242,18 @@ def __init__(

self.output_profile = output

def point(self, im):
def point(self, im: Image.Image) -> Image.Image:
return self.apply(im)

def apply(self, im, imOut=None):
def apply(self, im: Image.Image, imOut: Image.Image | None = None) -> Image.Image:
im.load()
if imOut is None:
imOut = Image.new(self.output_mode, im.size, None)
self.transform.apply(im.im.id, imOut.im.id)
imOut.info["icc_profile"] = self.output_profile.tobytes()
return imOut

def apply_in_place(self, im):
def apply_in_place(self, im: Image.Image) -> Image.Image:
im.load()
if im.mode != self.output_mode:
msg = "mode mismatch"
Expand Down

0 comments on commit 0eeb3f5

Please sign in to comment.