Skip to content

2-bit and 4-bit /DeviceGray images are extracted as black images #4316

Closed

Description

@MartinThoma

page.images extracts /DeviceGray images with 2 or 4 bits per component as black images: they become palette images (mode P) whose palette has a single black entry. No warning or exception is raised. Images with 1 and 8 bits per component are extracted correctly. This happens with and without a filter, for example with /FlateDecode.

Environment

$ python -m platform
Linux-6.8.0-147-generic-x86_64-with-glibc2.39

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==6.21.4, crypt_provider=('cryptography', '45.0.4'), PIL=11.1.0

Code + PDF

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

from io import BytesIO

from pypdf import PdfReader, PdfWriter
from pypdf.generic import DecodedStreamObject, DictionaryObject, NameObject, NumberObject


def extract_gray_image(bits: int, data: bytes) -> list[int]:
    """Write a 4 x 1 /DeviceGray image without a filter and extract it again."""
    image = DecodedStreamObject()
    image.set_data(data)
    image.update({
        NameObject("/Type"): NameObject("/XObject"),
        NameObject("/Subtype"): NameObject("/Image"),
        NameObject("/Width"): NumberObject(4),
        NameObject("/Height"): NumberObject(1),
        NameObject("/BitsPerComponent"): NumberObject(bits),
        NameObject("/ColorSpace"): NameObject("/DeviceGray"),
    })
    writer = PdfWriter()
    page = writer.add_blank_page(20, 20)
    page[NameObject("/Resources")] = DictionaryObject(
        {NameObject("/XObject"): DictionaryObject({NameObject("/Im0"): writer._add_object(image)})}
    )
    output = BytesIO()
    writer.write(output)
    extracted = PdfReader(output).pages[0].images[0].image
    print(f"{bits} bits: mode {extracted.mode}, palette {extracted.getpalette()}")
    return list(extracted.convert("L").getdata())


# Black, dark gray, light gray, white
print(extract_gray_image(2, bytes([0b00_01_10_11])))  # Expected [0, 85, 170, 255]
print(extract_gray_image(4, bytes([0x05, 0xAF])))  # Expected [0, 85, 170, 255]
print(extract_gray_image(8, bytes([0, 85, 170, 255])))  # Correct

Output:

2 bits: mode P, palette [0, 0, 0]
[0, 0, 0, 0]
4 bits: mode P, palette [0, 0, 0]
[0, 0, 0, 0]
8 bits: mode L, palette None
[0, 85, 170, 255]

Files in the tests that show it:

Traceback

None: the images are extracted without an exception or a warning.

Activity

  1. added
    workflow-imagesFrom a users perspective, image handling is the affected feature/workflow
    on Oct 30, 2026
  2. added a commit that references this issue on Oct 30, 2026
  3. b6tang commented on Oct 30, 2026

    @b6tang

    Hi Martin. I will take #4316. I will first reproduce it on current main with the /DeviceGray example from the issue, then investigate the low-bit-depth conversion. I'll report back by November 2.

  4. MartinThoma commented on Oct 30, 2026

    @MartinThoma
    MemberAuthor

    @b6tang Yoou might be interested in test_4bits_image :-)

  5. added a commit that references this issue on Oct 31, 2026
    e523d1f
  6. b6tang commented on Oct 31, 2026

    @b6tang

    @MartinThoma Thanks for the testing suggestion. I noticed that a related fix has already been submitted in #4324. To avoid duplicating work, I'll stop pursuing my planned fix.

  7. added a commit that references this issue on Nov 12, 2026
    b35c1e6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    workflow-imagesFrom a users perspective, image handling is the affected feature/workflow

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions