Delete Images from PDF File using Python

Use this page when you need to remove unnecessary graphics, reduce PDF size, or clean sensitive visual content from a document.

Delete Images from PDF File

Use the following steps to delete one image from a page:

  1. Load the source PDF with ap.Document(infile).
  2. Select the page and image resource index.
  3. Delete the image with resources.images.delete(index).
  4. Save the updated PDF.
import aspose.pdf as ap


def delete_image(infile, outfile):
    document = ap.Document(infile)
    document.pages[1].resources.images.delete(1)
    document.save(outfile)

Delete All Images from a Page

Use this example to remove every image from a specific page.

import aspose.pdf as ap


def delete_all_images_from_page(infile, outfile, page_number):
    document = ap.Document(infile)
    page = document.pages[page_number]

    while len(page.resources.images) != 0:
        page.resources.images.delete(1)

    document.save(outfile)