Extract images to files

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Parser MCP server lets your agent work with files on your machine; they are never uploaded. It runs in Docker: replace /path/to/documents with the folder it may use.

claude mcp add groupdocs-parser -- docker run --rm -i -v /path/to/documents:/data ghcr.io/groupdocs-parser/parser-net-mcp:latest
Folders, licensing and more →

GroupDocs.Parser provides functionality to save extracted images directly to files with support for format conversion.

Prerequisites

  • GroupDocs.Parser for Python via .NET installed
  • Sample documents with images
  • Write permissions to output directory

Save images to files

To extract and save images to files:

from groupdocs.parser import Parser
import os

# Create output directory
output_dir = "extracted_images"
os.makedirs(output_dir, exist_ok=True)

# Create an instance of Parser class
with Parser("./document.pdf") as parser:
    # Extract images
    images = parser.get_images()
    
    # Check if image extraction is supported
    if images is None:
        print("Image extraction isn't supported")
    elif not images:
        print("No images found")
    else:
        # Iterate over images and save them
        for idx, image in enumerate(images):
            # Generate filename with appropriate extension
            filename = f"image_{idx + 1}{image.file_type.extension}"
            filepath = os.path.join(output_dir, filename)
            
            # Save image to file
            image.save(filepath)
            print(f"Saved: {filename}")

The following sample file is used in this example: document.pdf

extracted_images/image_1.jpeg (10 KB)
extracted_images/image_2.jpeg (88 KB)
extracted_images/image_3.jpeg (122 KB)
extracted_images/image_4.jpeg (160 KB)
extracted_images/image_5.jpeg (93 KB)

Download full output

Expected behavior: Saves each extracted image to a separate file using the image’s original format.

Save images with format conversion

To save images in a specific format (e.g., PNG):

from groupdocs.parser import Parser
from groupdocs.parser.options import ImageOptions, ImageFormat
import os

# Create output directory
output_dir = "images_png"
os.makedirs(output_dir, exist_ok=True)

# Create an instance of Parser class
with Parser("presentation.pptx") as parser:
    # Extract images
    images = parser.get_images()
    
    if images is None:
        print("Image extraction not supported")
    elif not images:
        print("No images found")
    else:
        # Create image options for PNG format
        png_options = ImageOptions(ImageFormat.PNG)
        
        # Save all images as PNG
        for idx, image in enumerate(images):
            filename = f"image_{idx + 1}.png"
            filepath = os.path.join(output_dir, filename)
            
            # Save with format conversion
            image.save(filepath, png_options)
            print(f"Saved as PNG: {filename}")

The following sample file is used in this example: presentation.pptx

images_png/image_1.png (4 KB)
images_png/image_2.png (739 KB)
images_png/image_3.png (437 KB)
images_png/image_4.png (565 KB)

Download full output

Expected behavior: Converts all extracted images to PNG format before saving.

Save images with custom naming

To save images with descriptive filenames:

from groupdocs.parser import Parser
import os
from datetime import datetime

def save_images_with_custom_names(file_path, output_dir, prefix="doc"):
    """
    Save images with custom naming pattern.
    """
    os.makedirs(output_dir, exist_ok=True)
    
    # Generate timestamp
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    
    with Parser(file_path) as parser:
        images = parser.get_images()
        
        if images is None:
            print("Image extraction not supported")
            return 0
        
        saved_count = 0
        
        for idx, image in enumerate(images):
            # Create custom filename
            filename = f"{prefix}_{timestamp}_page{image.page.index + 1}_img{idx + 1}{image.file_type.extension}"
            filepath = os.path.join(output_dir, filename)
            
            # Save image
            image.save(filepath)
            print(f"Saved: {filename}")
            saved_count += 1
        
        return saved_count

# Usage
count = save_images_with_custom_names("report.pdf", "saved_images", prefix="report")
print(f"\nTotal images saved: {count}")

The following sample file is used in this example: report.pdf

saved_images/report_20261105_041430_page1_img1.jpeg (10 KB)
saved_images/report_20261105_041430_page1_img2.jpeg (88 KB)
saved_images/report_20261105_041430_page2_img3.jpeg (122 KB)
saved_images/report_20261105_041430_page3_img4.jpeg (160 KB)
saved_images/report_20261105_041430_page4_img5.jpeg (93 KB)

Download full output

Expected behavior: Saves images with descriptive filenames including timestamp, page number, and index.

Save images in multiple formats

To save each image in multiple formats:

from groupdocs.parser import Parser
from groupdocs.parser.options import ImageOptions, ImageFormat
import os

# Create output directory
output_dir = "images_multi_format"
os.makedirs(output_dir, exist_ok=True)

# Create an instance of Parser class
with Parser("document.docx") as parser:
    images = parser.get_images()
    
    if images:
        # Define formats to save
        formats = {
            'png': ImageOptions(ImageFormat.PNG),
            'jpg': ImageOptions(ImageFormat.JPEG),
            'bmp': ImageOptions(ImageFormat.BMP)
        }
        
        for idx, image in enumerate(images):
            print(f"\nProcessing image {idx + 1}:")
            
            # Save in each format
            for format_name, options in formats.items():
                filename = f"image_{idx + 1}.{format_name}"
                filepath = os.path.join(output_dir, filename)
                
                image.save(filepath, options)
                print(f"  Saved as {format_name.upper()}")
    else:
        print("No images found")

The following sample file is used in this example: document.docx

images_multi_format/image_1.bmp (422 KB)
images_multi_format/image_1.jpg (16 KB)
images_multi_format/image_1.png (4 KB)
images_multi_format/image_2.bmp (3516 KB)
images_multi_format/image_2.jpg (160 KB)
images_multi_format/image_2.png (1035 KB)
images_multi_format/image_3.bmp (3516 KB)
images_multi_format/image_3.jpg (122 KB)
images_multi_format/image_3.png (739 KB)

Download full output

Expected behavior: Saves each image in PNG, JPEG, and BMP formats.

Organize images by page

To save images organized by page number:

from groupdocs.parser import Parser
import os

def save_images_by_page(file_path, output_dir):
    """
    Save images organized in subdirectories by page.
    """
    os.makedirs(output_dir, exist_ok=True)
    
    with Parser(file_path) as parser:
        images = parser.get_images()
        
        if images is None:
            print("Image extraction not supported")
            return
        
        # Group images by page
        images_by_page = {}
        
        for image in images:
            page_num = image.page.index + 1
            if page_num not in images_by_page:
                images_by_page[page_num] = []
            images_by_page[page_num].append(image)
        
        # Save images organized by page
        total_saved = 0
        
        for page_num, page_images in sorted(images_by_page.items()):
            # Create page directory
            page_dir = os.path.join(output_dir, f"page_{page_num}")
            os.makedirs(page_dir, exist_ok=True)
            
            print(f"\nPage {page_num}: {len(page_images)} images")
            
            for img_idx, image in enumerate(page_images):
                filename = f"image_{img_idx + 1}{image.file_type.extension}"
                filepath = os.path.join(page_dir, filename)
                
                image.save(filepath)
                print(f"  Saved: {filename}")
                total_saved += 1
        
        print(f"\nTotal images saved: {total_saved}")

# Usage
save_images_by_page("multi_page.pdf", "images_by_page")

The following sample file is used in this example: multi_page.pdf

images_by_page/page_1/image_1.jpeg (10 KB)
images_by_page/page_1/image_2.jpeg (88 KB)
images_by_page/page_2/image_1.jpeg (122 KB)
images_by_page/page_3/image_1.jpeg (160 KB)
images_by_page/page_4/image_1.jpeg (93 KB)

Download full output

Expected behavior: Creates subdirectories for each page and saves images within their respective page folders.

Save only images of specific type

To save only images of a particular format:

from groupdocs.parser import Parser
import os

def save_images_by_type(file_path, output_dir, target_extensions):
    """
    Save only images of specific types.
    
    Args:
        file_path: Path to document
        output_dir: Output directory
        target_extensions: List of extensions to save (e.g., ['.jpg', '.png'])
    """
    os.makedirs(output_dir, exist_ok=True)
    
    with Parser(file_path) as parser:
        images = parser.get_images()
        
        if images is None:
            print("Image extraction not supported")
            return
        
        saved_count = 0
        skipped_count = 0
        
        for idx, image in enumerate(images):
            ext = image.file_type.extension.lower()
            
            if ext in target_extensions:
                filename = f"image_{idx + 1}{ext}"
                filepath = os.path.join(output_dir, filename)
                
                image.save(filepath)
                print(f"Saved: {filename}")
                saved_count += 1
            else:
                print(f"Skipped: image_{idx + 1}{ext}")
                skipped_count += 1
        
        print(f"\nSaved: {saved_count}, Skipped: {skipped_count}")

# Usage - save only JPG and PNG images
save_images_by_type("document.pdf", "filtered_images", ['.jpg', '.jpeg', '.png'])

The following sample file is used in this example: document.pdf

filtered_images/image_1.jpeg (10 KB)
filtered_images/image_2.jpeg (88 KB)
filtered_images/image_3.jpeg (122 KB)
filtered_images/image_4.jpeg (160 KB)
filtered_images/image_5.jpeg (93 KB)

Download full output

Expected behavior: Saves only images matching the specified formats, skipping others.

Batch save images from multiple documents

To extract and save images from multiple documents:

from groupdocs.parser import Parser
from pathlib import Path
import os

def batch_save_images(input_dir, output_dir):
    """
    Extract and save images from all documents in a directory.
    """
    os.makedirs(output_dir, exist_ok=True)
    
    extensions = ['.pdf', '.docx', '.doc', '.pptx', '.ppt']
    
    for file_path in sorted(Path(input_dir).rglob('*')):
        if file_path.suffix.lower() in extensions:
            print(f"\nProcessing: {file_path.name}")
            
            # Create subdirectory for this document
            doc_output_dir = os.path.join(output_dir, file_path.stem)
            os.makedirs(doc_output_dir, exist_ok=True)
            
            try:
                with Parser(str(file_path)) as parser:
                    images = parser.get_images()
                    
                    if images:
                        image_count = 0
                        for idx, image in enumerate(images):
                            filename = f"image_{idx + 1}{image.file_type.extension}"
                            filepath = os.path.join(doc_output_dir, filename)
                            image.save(filepath)
                            image_count += 1
                        
                        print(f"  Saved {image_count} images")
                    else:
                        print(f"  No images or not supported")
            
            except Exception as e:
                print(f"  Error: {e}")

# Usage
batch_save_images("documents", "all_extracted_images")

The following sample files are used in this example: annual-report.pdf, product-catalog.pptx, service-agreement.docx

all_extracted_images/annual-report/image_1.jpeg (10 KB)
all_extracted_images/annual-report/image_2.jpeg (88 KB)
all_extracted_images/annual-report/image_3.jpeg (122 KB)
all_extracted_images/annual-report/image_4.jpeg (160 KB)
all_extracted_images/annual-report/image_5.jpeg (93 KB)
all_extracted_images/product-catalog/image_1.png (4 KB)
all_extracted_images/product-catalog/image_2.jpeg (136 KB)
all_extracted_images/product-catalog/image_3.jpeg (75 KB)
all_extracted_images/product-catalog/image_4.jpeg (105 KB)
all_extracted_images/service-agreement/image_1.png (4 KB)
[TRUNCATED] (12 files total)

Download full output

Expected behavior: Processes multiple documents and saves their images in organized subdirectories.

Notes

  • The save() method saves images to the specified file path
  • Use ImageOptions to convert images to different formats
  • Supported output formats: BMP, GIF, JPEG, PNG, WebP
  • Original image format is preserved unless ImageOptions is specified
  • Always create the output directory before saving
  • File paths can be absolute or relative
  • Consider disk space when extracting many images