Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled ZeroDivisionError in _extended_image_frombytes #2783

Closed
williamgagnonpoka opened this issue Aug 1, 2024 · 1 comment · Fixed by #2786
Closed

Unhandled ZeroDivisionError in _extended_image_frombytes #2783

williamgagnonpoka opened this issue Aug 1, 2024 · 1 comment · Fixed by #2786
Labels
is-robustness-issue From a users perspective, this is about robustness workflow-images From a users perspective, image handling is the affected feature/workflow

Comments

@williamgagnonpoka
Copy link
Contributor

When extracting an image that contains no data, a ZeroDivisionError is thrown in the except block of the _extended_image_frombytes method.

This exception should be handled.

Environment

$ python -m platform
macOS-14.5-arm64-arm-64bit

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==4.3.1, crypt_provider=('cryptography', '43.0.0'), PIL=10.4.0

Code + PDF

This is a minimal, complete example that shows the issue:

for image in page.images:
    image.name = str(page.page_number) + "/" + image.name
    images.append(image)

pdf_with_one_pixel_image.pdf

Traceback

This is the complete traceback I see:

mode = 'RGB', size = (1, 1), data = b''

    def _extended_image_frombytes(
        mode: str, size: Tuple[int, int], data: bytes
    ) -> Image.Image:
        try:
            img = Image.frombytes(mode, size, data)
        except ValueError as exc:
            nb_pix = size[0] * size[1]
            if len(data) % nb_pix != 0:
                raise exc
>           k = nb_pix * len(mode) / len(data)
E           ZeroDivisionError: division by zero
@stefan6419846
Copy link
Collaborator

Thanks for the report. Feel free to open a corresponding PR.

@stefan6419846 stefan6419846 added workflow-images From a users perspective, image handling is the affected feature/workflow is-robustness-issue From a users perspective, this is about robustness labels Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is-robustness-issue From a users perspective, this is about robustness workflow-images From a users perspective, image handling is the affected feature/workflow
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants