Read the PNG file and decode it. Returns (`width`, `height`, `rows`, `info`). May use excessive memory. `rows` is a sequence of rows; each row is a sequence of values. If the optional `lenient` argument evaluates to True, checksum failures
(self, lenient=False)
| 1785 | self.unit_is_meter = bool(unit) |
| 1786 | |
| 1787 | def read(self, lenient=False): |
| 1788 | """ |
| 1789 | Read the PNG file and decode it. |
| 1790 | Returns (`width`, `height`, `rows`, `info`). |
| 1791 | |
| 1792 | May use excessive memory. |
| 1793 | |
| 1794 | `rows` is a sequence of rows; |
| 1795 | each row is a sequence of values. |
| 1796 | |
| 1797 | If the optional `lenient` argument evaluates to True, |
| 1798 | checksum failures will raise warnings rather than exceptions. |
| 1799 | """ |
| 1800 | |
| 1801 | def iteridat(): |
| 1802 | """Iterator that yields all the ``IDAT`` chunks as strings.""" |
| 1803 | while True: |
| 1804 | type, data = self.chunk(lenient=lenient) |
| 1805 | if type == b"IEND": |
| 1806 | # http://www.w3.org/TR/PNG/#11IEND |
| 1807 | break |
| 1808 | if type != b"IDAT": |
| 1809 | continue |
| 1810 | # type == b'IDAT' |
| 1811 | # http://www.w3.org/TR/PNG/#11IDAT |
| 1812 | if self.colormap and not self.plte: |
| 1813 | warnings.warn("PLTE chunk is required before IDAT chunk") |
| 1814 | yield data |
| 1815 | |
| 1816 | self.preamble(lenient=lenient) |
| 1817 | raw = decompress(iteridat()) |
| 1818 | |
| 1819 | if self.interlace: |
| 1820 | |
| 1821 | def rows_from_interlace(): |
| 1822 | """Yield each row from an interlaced PNG.""" |
| 1823 | # It's important that this iterator doesn't read |
| 1824 | # IDAT chunks until it yields the first row. |
| 1825 | bs = bytearray(itertools.chain(*raw)) |
| 1826 | arraycode = "BH"[self.bitdepth > 8] |
| 1827 | # Like :meth:`group` but |
| 1828 | # producing an array.array object for each row. |
| 1829 | values = self._deinterlace(bs) |
| 1830 | vpr = self.width * self.planes |
| 1831 | for i in range(0, len(values), vpr): |
| 1832 | row = array(arraycode, values[i : i + vpr]) |
| 1833 | yield row |
| 1834 | |
| 1835 | rows = rows_from_interlace() |
| 1836 | else: |
| 1837 | rows = self._iter_bytes_to_values(self._iter_straight_packed(raw)) |
| 1838 | info = dict() |
| 1839 | for attr in "greyscale alpha planes bitdepth interlace".split(): |
| 1840 | info[attr] = getattr(self, attr) |
| 1841 | info["size"] = (self.width, self.height) |
| 1842 | for attr in "gamma transparent background".split(): |
| 1843 | a = getattr(self, attr, None) |
| 1844 | if a is not None: |