File size: 1,857 Bytes
a98fc5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
import os
from pdf2image import convert_from_path

def extract_pages_to_png(pdf_path, start_page, end_page, output_dir="extracted_pages"):
    """
    Extract pages from PDF and save as high-quality PNG images
    """
    # Create output directory if it doesn't exist
    os.makedirs(output_dir, exist_ok=True)
    
    print(f"Extracting pages {start_page}-{end_page} from {pdf_path}")
    print(f"Output directory: {output_dir}")
    
    # Convert PDF pages to images with high DPI for quality
    # Extract in batches to manage memory usage
    batch_size = 50
    total_pages = end_page - start_page + 1
    
    for batch_start in range(start_page, end_page + 1, batch_size):
        batch_end = min(batch_start + batch_size - 1, end_page)
        
        print(f"Processing batch: pages {batch_start}-{batch_end}")
        
        try:
            # Convert pages with high DPI (300 DPI for high quality)
            images = convert_from_path(
                pdf_path, 
                dpi=300,
                first_page=batch_start,
                last_page=batch_end,
                fmt='PNG'
            )
            
            # Save each image
            for i, image in enumerate(images):
                page_num = batch_start + i
                output_path = os.path.join(output_dir, f"page_{page_num:04d}.png")
                image.save(output_path, "PNG", optimize=True)
                print(f"Saved: {output_path}")
                
        except Exception as e:
            print(f"Error processing batch {batch_start}-{batch_end}: {e}")
            continue
    
    print(f"Extraction complete! Total pages processed: {total_pages}")

if __name__ == "__main__":
    pdf_file = "中国手语.pdf"
    start_page = 665
    end_page = 1089
    
    extract_pages_to_png(pdf_file, start_page, end_page)