Delete Images from PDF File using Python
Contents
[
Hide
]
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:
- Load the source PDF with
ap.Document(infile). - Select the page and image resource index.
- Delete the image with
resources.images.delete(index). - 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)