Write the image *img* into the pdf file using png predictors with Flate compression.
(self, img)
| 1620 | return rgb, alpha |
| 1621 | |
| 1622 | def _writePng(self, img): |
| 1623 | """ |
| 1624 | Write the image *img* into the pdf file using png |
| 1625 | predictors with Flate compression. |
| 1626 | """ |
| 1627 | buffer = BytesIO() |
| 1628 | img.save(buffer, format="png") |
| 1629 | buffer.seek(8) |
| 1630 | png_data = b'' |
| 1631 | bit_depth = palette = None |
| 1632 | while True: |
| 1633 | length, type = struct.unpack(b'!L4s', buffer.read(8)) |
| 1634 | if type in [b'IHDR', b'PLTE', b'IDAT']: |
| 1635 | data = buffer.read(length) |
| 1636 | if len(data) != length: |
| 1637 | raise RuntimeError("truncated data") |
| 1638 | if type == b'IHDR': |
| 1639 | bit_depth = int(data[8]) |
| 1640 | elif type == b'PLTE': |
| 1641 | palette = data |
| 1642 | elif type == b'IDAT': |
| 1643 | png_data += data |
| 1644 | elif type == b'IEND': |
| 1645 | break |
| 1646 | else: |
| 1647 | buffer.seek(length, 1) |
| 1648 | buffer.seek(4, 1) # skip CRC |
| 1649 | return png_data, bit_depth, palette |
| 1650 | |
| 1651 | def _writeImg(self, data, id, smask=None): |
| 1652 | """ |