Skip to content

Commit

Permalink
Add note to PIL.Image.DecompressionBombError msgs
Browse files Browse the repository at this point in the history
See #413
  • Loading branch information
jsvine committed Jul 20, 2022
1 parent 71ad60f commit b6ff9e8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. The format
### Added

- Add `strict=True/False` parameter to `Page.crop(...)` and `Page.within_bbox(...)`; default is `True`, while `False` bypasses the `test_proposed_bbox(...)` check. ([#421](https://github.com/jsvine/pdfplumber/issues/421))
- Add more guidance to exception when `.to_image(...)` raises `PIL.Image.DecompressionBombError`. ([#413](https://github.com/jsvine/pdfplumber/issues/413))

### Fixed

Expand Down
13 changes: 12 additions & 1 deletion pdfplumber/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,18 @@ def postprocess(img: WandImage) -> WandImage:
colorspace="rgb",
) as bg:
bg.composite(img, 0, 0)
im = PIL.Image.open(BytesIO(bg.make_blob("png")))
try:
im = PIL.Image.open(BytesIO(bg.make_blob("png")))
except PIL.Image.DecompressionBombError:
raise PIL.Image.DecompressionBombError(
"Image conversion raised a DecompressionBombError. "
"PIL.Image.MAX_IMAGE_PIXELS is currently set to "
f"{PIL.Image.MAX_IMAGE_PIXELS}. "
"If you trust this PDF, you can try setting "
"PIL.Image.MAX_IMAGE_PIXELS to a higher value. "
"See https://github.com/jsvine/pdfplumber/issues/413"
"#issuecomment-1190650404 for more information."
)
return im.convert("RGB")


Expand Down
8 changes: 8 additions & 0 deletions tests/test_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import unittest

import PIL.Image
import pytest

import pdfplumber
Expand Down Expand Up @@ -82,3 +83,10 @@ def test__repr_png_(self):
71939,
61247,
) # PNG encoder seems to work differently on different setups

def test_decompression_bomb(self):
original_max = PIL.Image.MAX_IMAGE_PIXELS
PIL.Image.MAX_IMAGE_PIXELS = 10
with pytest.raises(PIL.Image.DecompressionBombError):
self.pdf.pages[0].to_image()
PIL.Image.MAX_IMAGE_PIXELS = original_max

0 comments on commit b6ff9e8

Please sign in to comment.