| 98 | |
| 99 | |
| 100 | def test_indexed_image(): |
| 101 | # An image with low color count should compress to a palette-indexed format. |
| 102 | pikepdf = pytest.importorskip('pikepdf') |
| 103 | |
| 104 | data = np.zeros((256, 1, 3), dtype=np.uint8) |
| 105 | data[:, 0, 0] = np.arange(256) # Maximum unique colours for an indexed image. |
| 106 | |
| 107 | rcParams['pdf.compression'] = True |
| 108 | fig = plt.figure() |
| 109 | fig.figimage(data, resize=True) |
| 110 | buf = io.BytesIO() |
| 111 | fig.savefig(buf, format='pdf', dpi='figure') |
| 112 | |
| 113 | with pikepdf.Pdf.open(buf) as pdf: |
| 114 | page, = pdf.pages |
| 115 | image, = page.images.values() |
| 116 | pdf_image = pikepdf.PdfImage(image) |
| 117 | assert pdf_image.indexed |
| 118 | pil_image = pdf_image.as_pil_image() |
| 119 | rgb = np.asarray(pil_image.convert('RGB')) |
| 120 | |
| 121 | np.testing.assert_array_equal(data, rgb) |
| 122 | |
| 123 | |
| 124 | def test_savefig_metadata(monkeypatch): |