| | |
| | 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 |
| | """ |
| | |
| | os.makedirs(output_dir, exist_ok=True) |
| | |
| | print(f"Extracting pages {start_page}-{end_page} from {pdf_path}") |
| | print(f"Output directory: {output_dir}") |
| | |
| | |
| | |
| | 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: |
| | |
| | images = convert_from_path( |
| | pdf_path, |
| | dpi=300, |
| | first_page=batch_start, |
| | last_page=batch_end, |
| | fmt='PNG' |
| | ) |
| | |
| | |
| | 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) |