Skip to main content

fix images_only_pdf for the new version of PyMuPDF

ID
8f6c097
date
2022-11-20 10:05:38+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
1a39d41
message
fix images_only_pdf for the new version of PyMuPDF
changed files
1 file, 7 additions, 8 deletions

Changed files

images_only_pdf (1339) → images_only_pdf (1323)

diff --git a/images_only_pdf b/images_only_pdf
index 6f662c3..891c646 100755
--- a/images_only_pdf
+++ b/images_only_pdf
@@ -11,7 +11,7 @@ borders around the images which is precisely what I don't want.
 import os
 import sys
 
-import fitz  # PyMuPDF
+import fitz  # PyMuPDF==1.21.0
 
 
 if __name__ == "__main__":
@@ -22,21 +22,20 @@ if __name__ == "__main__":
 
     with fitz.open(path) as pdf_original, fitz.open() as pdf_new:
         for page_number, page in enumerate(pdf_original, start=1):
-            for image_number, image in enumerate(page.getImageList(), start=1):
+            for image_number, image in enumerate(page.get_images(), start=1):
                 # Get the XREF of the image
                 xref = image[0]
 
                 # Extract the image bytes
-                base_image = pdf_original.extractImage(xref)
+                base_image = pdf_original.extract_image(xref)
 
                 new_page = pdf_new.new_page(
                     width=base_image["width"], height=base_image["height"]
                 )
                 rect = fitz.Rect(0.0, 0.0, base_image["width"], base_image["height"])
-                new_page.insertImage(rect, stream=base_image["image"])
+                new_page.insert_image(rect, stream=base_image["image"])
 
-        out_path = path.replace(".pdf", "-noimages.pdf")
-        assert path != out_path
+        # out_path = path.replace(".pdf", "-noimages.pdf")
+        # assert path != out_path
 
-        pdf_new.save(out_path)
-        print(out_path)
+        pdf_new.save(path)