Delete Images from PDF File using Python

There are many reasons for removing all or specific images from PDFs.

Sometimes a PDF file may contain important images that need to be removed to protect privacy or prevent unauthorized access to certain information.

Removing unwanted or redundant images can help reduce file size, making it easier to share or store PDFs.

If necessary, you can reduce the number of pages by removing all images from the document. Also, deleting images from the document will help prepare the PDF for compression or extraction of text information.

Aspose.PDF for Python via .NET will help you with this task.

Delete Images from PDF File

To delete an image from a PDF file:

  1. Open existing PDF Document.
  2. Delete a particular image.
  3. Save updated PDF file.

The following code snippet shows how to delete an image from a PDF file.


    import aspose.pdf as ap

    # Open document
    document = ap.Document(input_file)

    # Delete particular image
    document.pages[2].resources.images.delete(1)

    # Save updated PDF file
    document.save(output_pdf)

Delete all images from input PDF


    import aspose.pdf as ap

    # Open document
    document = ap.Document(input_file)

    # Delete all images on all pages
    for i in range(len(document.pages)):
        while len(document.pages[i + 1].resources.images) != 0:
            document.pages[i + 1].resources.images.delete(1)

    # Save updated PDF file
    document.save(output_file)