diff --git a/src/PIL/BmpImagePlugin.py b/src/PIL/BmpImagePlugin.py index 48bdd9830..da9f2eba5 100644 --- a/src/PIL/BmpImagePlugin.py +++ b/src/PIL/BmpImagePlugin.py @@ -170,6 +170,8 @@ class BmpImageFile(ImageFile.ImageFile): # ------------------ Special case : header is reported 40, which # ---------------------- is shorter than real size for bpp >= 16 + assert isinstance(file_info["width"], int) + assert isinstance(file_info["height"], int) self._size = file_info["width"], file_info["height"] # ------- If color count was not found in the header, compute from bits diff --git a/src/PIL/EpsImagePlugin.py b/src/PIL/EpsImagePlugin.py index 35915e652..0ba0ab9c4 100644 --- a/src/PIL/EpsImagePlugin.py +++ b/src/PIL/EpsImagePlugin.py @@ -190,7 +190,7 @@ class EpsImageFile(ImageFile.ImageFile): self.fp.seek(offset) self._mode = "RGB" - self._size = None + image_size: tuple[int, int] | None = None byte_arr = bytearray(255) bytes_mv = memoryview(byte_arr) @@ -213,7 +213,7 @@ class EpsImageFile(ImageFile.ImageFile): raise SyntaxError(msg) def _read_comment(s: str) -> bool: - nonlocal reading_trailer_comments + nonlocal image_size, reading_trailer_comments try: m = split.match(s) except re.error as e: @@ -228,16 +228,18 @@ class EpsImageFile(ImageFile.ImageFile): if k == "BoundingBox": if v == "(atend)": reading_trailer_comments = True - elif not self._size or (trailer_reached and reading_trailer_comments): + elif image_size is None or ( + trailer_reached and reading_trailer_comments + ): try: # Note: The DSC spec says that BoundingBox # fields should be integers, but some drivers # put floating point values there anyway. box = [int(float(i)) for i in v.split()] - self._size = box[2] - box[0], box[3] - box[1] + image_size = box[2] - box[0], box[3] - box[1] self.tile = [ ImageFile._Tile( - "eps", (0, 0) + self.size, offset, (length, box) + "eps", (0, 0) + image_size, offset, (length, box) ) ] except Exception: @@ -334,8 +336,8 @@ class EpsImageFile(ImageFile.ImageFile): else: break - self._size = columns, rows - return + image_size = columns, rows + break elif bytes_mv[:5] == b"%%EOF": break elif trailer_reached and reading_trailer_comments: @@ -346,7 +348,9 @@ class EpsImageFile(ImageFile.ImageFile): trailer_reached = True bytes_read = 0 - if not self._size: + if image_size: + self._size = image_size + else: msg = "cannot determine EPS bounding box" raise OSError(msg) diff --git a/src/PIL/FpxImagePlugin.py b/src/PIL/FpxImagePlugin.py index 386e37233..0475b6d5f 100644 --- a/src/PIL/FpxImagePlugin.py +++ b/src/PIL/FpxImagePlugin.py @@ -80,7 +80,8 @@ class FpxImageFile(ImageFile.ImageFile): ) # size (highest resolution) - + assert isinstance(prop[0x1000002], int) + assert isinstance(prop[0x1000003], int) self._size = prop[0x1000002], prop[0x1000003] size = max(self.size) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index ebf4f46c4..224da5307 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -537,7 +537,7 @@ class Image: # FIXME: turn mode and size into delegating properties? self.im = None self._mode = "" - self._size = (0, 0) + self._size: tuple[int, int] = (0, 0) self.palette = None self.info = {} self.readonly = 0 diff --git a/src/PIL/QoiImagePlugin.py b/src/PIL/QoiImagePlugin.py index 202ef52d0..d20d0ab2d 100644 --- a/src/PIL/QoiImagePlugin.py +++ b/src/PIL/QoiImagePlugin.py @@ -26,7 +26,7 @@ class QoiImageFile(ImageFile.ImageFile): msg = "not a QOI file" raise SyntaxError(msg) - self._size = tuple(i32(self.fp.read(4)) for i in range(2)) + self._size = (i32(self.fp.read(4)), i32(self.fp.read(4))) channels = self.fp.read(1)[0] self._mode = "RGB" if channels == 3 else "RGBA"