Backport PR #25824 on branch v3.7.x (pdf: Use explicit palette when saving indexed images) by meeseeksmachine · Pull Request #26215 · matplotlib/matplotlib · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions lib/matplotlib/backends/backend_pdf.py
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/imshow.pdf
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/imshow_clip.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_image/figimage.pdf
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_image/image_alpha.pdf
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_image/image_interps.pdf
Binary file not shown.
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_image/image_shift.pdf
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_image/imshow.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_image/rotate_image.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
24 changes: 24 additions & 0 deletions lib/matplotlib/tests/test_backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ def test_composite_image():
assert len(pdf._file._images) == 2


def test_indexed_image():
# An image with low color count should compress to a palette-indexed format.
pikepdf = pytest.importorskip('pikepdf')

data = np.zeros((256, 1, 3), dtype=np.uint8)
data[:, 0, 0] = np.arange(256) # Maximum unique colours for an indexed image.

rcParams['pdf.compression'] = True
fig = plt.figure()
fig.figimage(data, resize=True)
buf = io.BytesIO()
fig.savefig(buf, format='pdf', dpi='figure')

with pikepdf.Pdf.open(buf) as pdf:
page, = pdf.pages
image, = page.images.values()
pdf_image = pikepdf.PdfImage(image)
assert pdf_image.indexed
pil_image = pdf_image.as_pil_image()
rgb = np.asarray(pil_image.convert('RGB'))

np.testing.assert_array_equal(data, rgb)


def test_savefig_metadata(monkeypatch):
pikepdf = pytest.importorskip('pikepdf')
monkeypatch.setenv('SOURCE_DATE_EPOCH', '0')
Expand Down
6 changes: 1 addition & 5 deletions lib/matplotlib/tests/test_image.py