import fitz from rich import inspect # This creates the Document object doc doc: fitz.Document = fitz.open("file.pdf") for page in doc: for img in page.get_images(): xref = img[-2] xref_number = img[0] pix = fitz.Pixmap(doc, xref) bg_color = pix.pixel(pix.width - 1, int(pix.height / 2)) if bg_color == (12,12,12): pix.invert_irect() rect = page.get_image_bbox(xref) page.insert_image(rect, pixmap=pix, keep_proportion=False) # doc.save(filename=r"file.new.pdf", clean=True) # doc.save(filename=r"file.new.pdf", clean=True, garbage=4) # without deflate_images=1 the file size is 112MB, but now it is just 12MB doc.save(filename=r"file.new.pdf", clean=True, deflate=4, deflate_images=1, deflate_fonts=1) doc.close()