Repository navigation
Closed
Labels
workflow-imagesFrom a users perspective, image handling is the affected feature/workflowFrom a users perspective, image handling is the affected feature/workflow
Description
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.0Code + 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]))) # CorrectOutput:
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:
4bits_image.pdf(test_4bits_image, TST: Add 4-bit image test #3761): a 16 x 16/DeviceGrayimage with 4 bits per component and/FlateDecode, a gradient from black to white. The reference PNG of the test was extracted with pypdf and is black as well, so the test passed. TST: Compare PNG predicted images with images without a predictor #4315 compares with the gradient instead and marks the test as xfail.032-png-predictors-low-bit-depth/png-predictors-low-bit-depth.pdfof the sample files (Add tagged PDF and PNG predictor samples, document 017 sample-files#45): pages 2 and 3 have/DeviceGrayimages with 2 and 4 bits per component. Their images without a predictor are extracted as black images as well. The images with a predictor are additionally affected by PNG predictor causes incorrect extraction of 1-bit and 4-bit images #4308.
Traceback
None: the images are extracted without an exception or a warning.
Metadata
Metadata
Assignees
Labels
workflow-imagesFrom a users perspective, image handling is the affected feature/workflowFrom a users perspective, image handling is the affected feature/workflow
Activity
TST: Refer to #4316 in the xfail reason of test_4bits_image
TST: Compare PNG predicted images with images without a predictor (#4315
b6tang commentedon Oct 30, 2026
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.
MartinThoma commentedon Oct 30, 2026
@b6tang Yoou might be interested in test_4bits_image :-)
BUG: Decode low-bit DeviceGray samples as grayscale
b6tang commentedon Oct 31, 2026
@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.
BUG: Decode 2/4-bit DeviceGray samples as scaled grayscale (py-pdf#4351)