So I am trying to make a tool that basically does what iLovePDF does (cut a PDF into user defined sets of pages, and then compresses them to a user defined level).
In my code I have the following snippet:
# Compression (in the writer)
for page in writer.pages:
if compress:
if compress_level>1:
for img in page.images:
if img.image.mode != "RGBA":
img.replace(img.image, quality=95)
page.compress_content_streams()
However, for two images I get the following warning:
image and mask size not matching: {'/Type': '/XObject', '/Subtype': '/Image', '/Width': 2, '/Height': 2, '/ColorSpace': ['/Indexed', '/DeviceRGB', 1, b'x00x00x00xffxffxff'], '/BitsPerComponent': 1, '/Interpolate': False, '/SMask': IndirectObject(73, 0, 1823765420048)}
and
image and mask size not matching: {'/Type': '/XObject', '/Subtype': '/Image', '/Width': 2, '/Height': 2, '/ColorSpace': ['/Indexed', '/DeviceRGB', 1, b'x00x00x00xffxffxff'], '/BitsPerComponent': 1, '/Interpolate': False, '/SMask': IndirectObject(76, 0, 1823765420048)}
Here are the images before the compression:
and
And here after the compression:
and
I’ve not been able to find a good fix for this. I’ve seen some things with converting RGBA
to RGB
, but to no avail.
As you can see in the code, I don’t mind skipping the image replacing if this problem occurs (which I already did with RGBA
images), but I don’t know what to catch, as img.replace() doesn’t return anything (at least not when I tried), and I also am not sure I know what code to insert even if I could catch it.
Lastly, if you know of some much easier/better way to do this whole thing, that’s also a welcome answer.