(Consumer) Feed data to the parser. :param data: A string buffer. :exception OSError: If the parser failed to parse the image file.
(self, data: bytes)
| 529 | assert self.data is None, "cannot reuse parsers" |
| 530 | |
| 531 | def feed(self, data: bytes) -> None: |
| 532 | """ |
| 533 | (Consumer) Feed data to the parser. |
| 534 | |
| 535 | :param data: A string buffer. |
| 536 | :exception OSError: If the parser failed to parse the image file. |
| 537 | """ |
| 538 | # collect data |
| 539 | |
| 540 | if self.finished: |
| 541 | return |
| 542 | |
| 543 | if self.data is None: |
| 544 | self.data = data |
| 545 | else: |
| 546 | self.data = self.data + data |
| 547 | |
| 548 | # parse what we have |
| 549 | if self.decoder: |
| 550 | if self.offset > 0: |
| 551 | # skip header |
| 552 | skip = min(len(self.data), self.offset) |
| 553 | self.data = self.data[skip:] |
| 554 | self.offset = self.offset - skip |
| 555 | if self.offset > 0 or not self.data: |
| 556 | return |
| 557 | |
| 558 | n, e = self.decoder.decode(self.data) |
| 559 | |
| 560 | if n < 0: |
| 561 | # end of stream |
| 562 | self.data = None |
| 563 | self.finished = 1 |
| 564 | if e < 0: |
| 565 | # decoding error |
| 566 | self.image = None |
| 567 | raise _get_oserror(e, encoder=False) |
| 568 | else: |
| 569 | # end of image |
| 570 | return |
| 571 | self.data = self.data[n:] |
| 572 | |
| 573 | elif self.image: |
| 574 | # if we end up here with no decoder, this file cannot |
| 575 | # be incrementally parsed. wait until we've gotten all |
| 576 | # available data |
| 577 | pass |
| 578 | |
| 579 | else: |
| 580 | # attempt to open this file |
| 581 | try: |
| 582 | with io.BytesIO(self.data) as fp: |
| 583 | im = Image.open(fp) |
| 584 | except OSError: |
| 585 | pass # not enough data |
| 586 | else: |
| 587 | flag = hasattr(im, "load_seek") or hasattr(im, "load_read") |
| 588 | if not flag and len(im.tile) == 1: |