(Consumer) Close the stream. :returns: An image object. :exception OSError: If the parser failed to parse the image file either because it cannot be identified or cannot be decoded.
(self)
| 608 | self.close() |
| 609 | |
| 610 | def close(self) -> Image.Image: |
| 611 | """ |
| 612 | (Consumer) Close the stream. |
| 613 | |
| 614 | :returns: An image object. |
| 615 | :exception OSError: If the parser failed to parse the image file either |
| 616 | because it cannot be identified or cannot be |
| 617 | decoded. |
| 618 | """ |
| 619 | # finish decoding |
| 620 | if self.decoder: |
| 621 | # get rid of what's left in the buffers |
| 622 | self.feed(b"") |
| 623 | self.data = self.decoder = None |
| 624 | if not self.finished: |
| 625 | msg = "image was incomplete" |
| 626 | raise OSError(msg) |
| 627 | if not self.image: |
| 628 | msg = "cannot parse this image" |
| 629 | raise OSError(msg) |
| 630 | if self.data: |
| 631 | # incremental parsing not possible; reopen the file |
| 632 | # not that we have all data |
| 633 | with io.BytesIO(self.data) as fp: |
| 634 | try: |
| 635 | self.image = Image.open(fp) |
| 636 | finally: |
| 637 | self.image.load() |
| 638 | return self.image |
| 639 | |
| 640 | |
| 641 | # -------------------------------------------------------------------- |