| | |
| |
|
| | import os |
| | import re |
| | import shutil |
| | from collections import defaultdict |
| | from pathlib import Path |
| |
|
| | def copy_middle_segments(): |
| | source_dir = Path("edited_segmented_images") |
| | target_dir = Path("middle_segments") |
| | |
| | |
| | target_dir.mkdir(exist_ok=True) |
| | |
| | |
| | page_segments = defaultdict(list) |
| | |
| | |
| | pattern = re.compile(r'page_(\d+)_segment_(\d+)\.png') |
| | |
| | |
| | for file in source_dir.glob("*.png"): |
| | match = pattern.search(file.name) |
| | if match: |
| | page_id = match.group(1) |
| | segment_num = int(match.group(2)) |
| | page_segments[page_id].append((segment_num, file.name)) |
| | |
| | |
| | total_copied = 0 |
| | for page_id, segments in page_segments.items(): |
| | if len(segments) <= 2: |
| | |
| | print(f"Page {page_id}: Only {len(segments)} segments, skipping") |
| | continue |
| | |
| | |
| | segments.sort(key=lambda x: x[0]) |
| | |
| | min_segment = segments[0][0] |
| | max_segment = segments[-1][0] |
| | |
| | print(f"Page {page_id}: segments {min_segment} to {max_segment}, copying middle segments...") |
| | |
| | |
| | for segment_num, filename in segments[1:-1]: |
| | source_path = source_dir / filename |
| | target_path = target_dir / filename |
| | shutil.copy2(source_path, target_path) |
| | total_copied += 1 |
| | print(f" Copied: {filename}") |
| | |
| | print(f"\nTotal files copied: {total_copied}") |
| | print(f"Files saved to: {target_dir.absolute()}") |
| |
|
| | if __name__ == "__main__": |
| | copy_middle_segments() |