Fix inverted colors during JBIG2 optimization on paletted images

Fixes #640
This commit is contained in:
James R. Barlow
2020-10-07 04:08:50 -07:00
parent 6eb393590b
commit 204c9d6ae1
2 changed files with 17 additions and 0 deletions
+11
View File
@@ -116,12 +116,23 @@ def extract_image_jbig2(
and jbig2enc.available()
):
try:
# Save any colorspace associated with the image, so that we
# will export a pure 1-bit PNG with no palette or ICC profile.
# Showing the palette or ICC to jbig2enc will cause it to perform
# colorspace transform to 1bpp, which will conflict the palette or
# ICC if it exists.
colorspace = pim.obj.ColorSpace
# Set to DeviceGray temporarily; we already in 1 bpc.
pim.obj.ColorSpace = pikepdf.Name.DeviceGray
imgname = root / f'{xref:08d}'
with imgname.open('wb') as f:
ext = pim.extract_to(stream=f)
imgname.rename(imgname.with_suffix(ext))
except pikepdf.UnsupportedImageTypeError:
return None
finally:
# Restore image colorspace after temporarily setting it to DeviceGray
pim.obj.ColorSpace = colorspace
return XrefExt(xref, ext)
return None