Write a PNG image to the output file. `rows` should be an iterable that yields each row (each row is a sequence of values). The rows should be the rows of the original image, so there should be ``self.height`` rows of ``self.width * self.planes`` valu
(self, outfile, rows)
| 624 | self.psize = (self.bitdepth / 8) * self.planes |
| 625 | |
| 626 | def write(self, outfile, rows): |
| 627 | """ |
| 628 | Write a PNG image to the output file. |
| 629 | `rows` should be an iterable that yields each row |
| 630 | (each row is a sequence of values). |
| 631 | The rows should be the rows of the original image, |
| 632 | so there should be ``self.height`` rows of |
| 633 | ``self.width * self.planes`` values. |
| 634 | If `interlace` is specified (when creating the instance), |
| 635 | then an interlaced PNG file will be written. |
| 636 | Supply the rows in the normal image order; |
| 637 | the interlacing is carried out internally. |
| 638 | |
| 639 | .. note :: |
| 640 | |
| 641 | Interlacing requires the entire image to be in working memory. |
| 642 | """ |
| 643 | |
| 644 | # Values per row |
| 645 | vpr = self.width * self.planes |
| 646 | |
| 647 | def check_rows(rows): |
| 648 | """ |
| 649 | Yield each row in rows, |
| 650 | but check each row first (for correct width). |
| 651 | """ |
| 652 | for i, row in enumerate(rows): |
| 653 | try: |
| 654 | wrong_length = len(row) != vpr |
| 655 | except TypeError: |
| 656 | # When using an itertools.ichain object or |
| 657 | # other generator not supporting __len__, |
| 658 | # we set this to False to skip the check. |
| 659 | wrong_length = False |
| 660 | if wrong_length: |
| 661 | # Note: row numbers start at 0. |
| 662 | raise ProtocolError( |
| 663 | "Expected %d values but got %d values, in row %d" |
| 664 | % (vpr, len(row), i) |
| 665 | ) |
| 666 | yield row |
| 667 | |
| 668 | if self.interlace: |
| 669 | fmt = "BH"[self.bitdepth > 8] |
| 670 | a = array(fmt, itertools.chain(*check_rows(rows))) |
| 671 | return self.write_array(outfile, a) |
| 672 | |
| 673 | nrows = self.write_passes(outfile, check_rows(rows)) |
| 674 | if nrows != self.height: |
| 675 | raise ProtocolError( |
| 676 | "rows supplied (%d) does not match height (%d)" % (nrows, self.height) |
| 677 | ) |
| 678 | |
| 679 | def write_passes(self, outfile, rows): |
| 680 | """ |
no test coverage detected