11
11
import os
12
12
from typing import IO , Tuple
13
13
14
- from typing_extensions import Self
15
-
16
14
from docx .image .exceptions import UnrecognizedImageError
17
15
from docx .shared import Emu , Inches , Length , lazyproperty
18
16
@@ -28,14 +26,14 @@ def __init__(self, blob: bytes, filename: str, image_header: BaseImageHeader):
28
26
self ._image_header = image_header
29
27
30
28
@classmethod
31
- def from_blob (cls , blob : bytes ) -> Self :
29
+ def from_blob (cls , blob : bytes ) -> Image :
32
30
"""Return a new |Image| subclass instance parsed from the image binary contained
33
31
in `blob`."""
34
32
stream = io .BytesIO (blob )
35
33
return cls ._from_stream (stream , blob )
36
34
37
35
@classmethod
38
- def from_file (cls , image_descriptor ):
36
+ def from_file (cls , image_descriptor : str | IO [ bytes ] ):
39
37
"""Return a new |Image| subclass instance loaded from the image file identified
40
38
by `image_descriptor`, a path or file-like object."""
41
39
if isinstance (image_descriptor , str ):
@@ -57,7 +55,7 @@ def blob(self):
57
55
return self ._blob
58
56
59
57
@property
60
- def content_type (self ):
58
+ def content_type (self ) -> str :
61
59
"""MIME content type for this image, e.g. ``'image/jpeg'`` for a JPEG image."""
62
60
return self ._image_header .content_type
63
61
@@ -167,12 +165,11 @@ def _from_stream(
167
165
return cls (blob , filename , image_header )
168
166
169
167
170
- def _ImageHeaderFactory (stream ):
171
- """Return a |BaseImageHeader| subclass instance that knows how to parse the headers
172
- of the image in `stream`."""
168
+ def _ImageHeaderFactory (stream : IO [bytes ]):
169
+ """A |BaseImageHeader| subclass instance that can parse headers of image in `stream`."""
173
170
from docx .image import SIGNATURES
174
171
175
- def read_32 (stream ):
172
+ def read_32 (stream : IO [ bytes ] ):
176
173
stream .seek (0 )
177
174
return stream .read (32 )
178
175
@@ -188,32 +185,27 @@ def read_32(stream):
188
185
class BaseImageHeader :
189
186
"""Base class for image header subclasses like |Jpeg| and |Tiff|."""
190
187
191
- def __init__ (self , px_width , px_height , horz_dpi , vert_dpi ):
188
+ def __init__ (self , px_width : int , px_height : int , horz_dpi : int , vert_dpi : int ):
192
189
self ._px_width = px_width
193
190
self ._px_height = px_height
194
191
self ._horz_dpi = horz_dpi
195
192
self ._vert_dpi = vert_dpi
196
193
197
194
@property
198
- def content_type (self ):
195
+ def content_type (self ) -> str :
199
196
"""Abstract property definition, must be implemented by all subclasses."""
200
- msg = (
201
- "content_type property must be implemented by all subclasses of "
202
- "BaseImageHeader"
203
- )
197
+ msg = "content_type property must be implemented by all subclasses of " "BaseImageHeader"
204
198
raise NotImplementedError (msg )
205
199
206
200
@property
207
- def default_ext (self ):
201
+ def default_ext (self ) -> str :
208
202
"""Default filename extension for images of this type.
209
203
210
204
An abstract property definition, must be implemented by all subclasses.
211
205
"""
212
- msg = (
213
- "default_ext property must be implemented by all subclasses of "
214
- "BaseImageHeader"
206
+ raise NotImplementedError (
207
+ "default_ext property must be implemented by all subclasses of " "BaseImageHeader"
215
208
)
216
- raise NotImplementedError (msg )
217
209
218
210
@property
219
211
def px_width (self ):
0 commit comments