Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added type hints to SgiImagePlugin #7724

Merged
merged 1 commit into from
Jan 16, 2024
Merged
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
8 changes: 4 additions & 4 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ def __setstate__(self, state):
self.putpalette(palette)
self.frombytes(data)

def tobytes(self, encoder_name="raw", *args):
def tobytes(self, encoder_name: str = "raw", *args) -> bytes:
"""
Return image as a bytes object.

Expand Down Expand Up @@ -788,7 +788,7 @@ def tobitmap(self, name="image"):
]
)

def frombytes(self, data, decoder_name="raw", *args):
def frombytes(self, data: bytes, decoder_name: str = "raw", *args) -> None:
"""
Loads this image with pixel data from a bytes object.

Expand Down Expand Up @@ -1297,7 +1297,7 @@ def filter(self, filter):
]
return merge(self.mode, ims)

def getbands(self):
def getbands(self) -> tuple[str, ...]:
"""
Returns a tuple containing the name of each band in this image.
For example, ``getbands`` on an RGB image returns ("R", "G", "B").
Expand Down Expand Up @@ -2495,7 +2495,7 @@ def show(self, title=None):

_show(self, title=title)

def split(self):
def split(self) -> tuple[Image, ...]:
"""
Split this image into individual bands. This method returns a
tuple of individual image bands from an image. For example,
Expand Down
18 changes: 12 additions & 6 deletions src/PIL/SgiImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@

import os
import struct
from io import BytesIO

from . import Image, ImageFile
from ._binary import i16be as i16
from ._binary import o8


def _accept(prefix):
def _accept(prefix: bytes) -> bool:
return len(prefix) >= 2 and i16(prefix) == 474


Expand All @@ -52,8 +53,10 @@ class SgiImageFile(ImageFile.ImageFile):
format = "SGI"
format_description = "SGI Image File Format"

def _open(self):
def _open(self) -> None:
# HEAD
assert self.fp is not None

headlen = 512
s = self.fp.read(headlen)

Expand Down Expand Up @@ -122,7 +125,7 @@ def _open(self):
]


def _save(im, fp, filename):
def _save(im: Image.Image, fp: BytesIO, filename: str) -> None:
if im.mode not in {"RGB", "RGBA", "L"}:
msg = "Unsupported SGI image mode"
raise ValueError(msg)
Expand Down Expand Up @@ -168,8 +171,8 @@ def _save(im, fp, filename):
# Maximum Byte value (255 = 8bits per pixel)
pinmax = 255
# Image name (79 characters max, truncated below in write)
img_name = os.path.splitext(os.path.basename(filename))[0]
img_name = img_name.encode("ascii", "ignore")
filename = os.path.basename(filename)
img_name = os.path.splitext(filename)[0].encode("ascii", "ignore")
# Standard representation of pixel in the file
colormap = 0
fp.write(struct.pack(">h", magic_number))
Expand Down Expand Up @@ -201,7 +204,10 @@ def _save(im, fp, filename):
class SGI16Decoder(ImageFile.PyDecoder):
_pulls_fd = True

def decode(self, buffer):
def decode(self, buffer: bytes) -> tuple[int, int]:
assert self.fd is not None
assert self.im is not None

rawmode, stride, orientation = self.args
pagesize = self.state.xsize * self.state.ysize
zsize = len(self.mode)
Expand Down
Loading