2424
2525import os
2626import struct
27+ from io import BytesIO
2728
2829from . import Image , ImageFile
2930from ._binary import i16be as i16
3031from ._binary import o8
3132
3233
33- def _accept (prefix ) :
34+ def _accept (prefix : bytes ) -> bool :
3435 return len (prefix ) >= 2 and i16 (prefix ) == 474
3536
3637
@@ -52,8 +53,10 @@ class SgiImageFile(ImageFile.ImageFile):
5253 format = "SGI"
5354 format_description = "SGI Image File Format"
5455
55- def _open (self ):
56+ def _open (self ) -> None :
5657 # HEAD
58+ assert self .fp is not None
59+
5760 headlen = 512
5861 s = self .fp .read (headlen )
5962
@@ -122,7 +125,7 @@ def _open(self):
122125 ]
123126
124127
125- def _save (im , fp , filename ) :
128+ def _save (im : Image . Image , fp : BytesIO , filename : str ) -> None :
126129 if im .mode not in {"RGB" , "RGBA" , "L" }:
127130 msg = "Unsupported SGI image mode"
128131 raise ValueError (msg )
@@ -168,8 +171,8 @@ def _save(im, fp, filename):
168171 # Maximum Byte value (255 = 8bits per pixel)
169172 pinmax = 255
170173 # Image name (79 characters max, truncated below in write)
171- img_name = os .path .splitext ( os . path . basename (filename ))[ 0 ]
172- img_name = img_name .encode ("ascii" , "ignore" )
174+ filename = os .path .basename (filename )
175+ img_name = os . path . splitext ( filename )[ 0 ] .encode ("ascii" , "ignore" )
173176 # Standard representation of pixel in the file
174177 colormap = 0
175178 fp .write (struct .pack (">h" , magic_number ))
@@ -201,7 +204,10 @@ def _save(im, fp, filename):
201204class SGI16Decoder (ImageFile .PyDecoder ):
202205 _pulls_fd = True
203206
204- def decode (self , buffer ):
207+ def decode (self , buffer : bytes ) -> tuple [int , int ]:
208+ assert self .fd is not None
209+ assert self .im is not None
210+
205211 rawmode , stride , orientation = self .args
206212 pagesize = self .state .xsize * self .state .ysize
207213 zsize = len (self .mode )
0 commit comments