Created
October 23, 2025 08:41
-
-
Save OriBenHur/4940774f048b7db2902f53476f49da94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| Script to split a long image into 8 A4 pages and save as high-resolution PDF | |
| """ | |
| from PIL import Image | |
| import sys | |
| import os | |
| def split_image_to_pdf(input_image_path, output_pdf_path): | |
| """ | |
| Split a long image into dynamic number of A4 pages and save as high-resolution PDF | |
| Args: | |
| input_image_path (str): Path to the input image | |
| output_pdf_path (str): Path for the output PDF file | |
| """ | |
| # A4 dimensions at 300 DPI (high resolution) | |
| A4_WIDTH = 2480 | |
| A4_HEIGHT = 3508 | |
| try: | |
| # Open the image | |
| print(f"Loading image from: {input_image_path}") | |
| image = Image.open(input_image_path) | |
| # Get original dimensions | |
| original_width, original_height = image.size | |
| print(f"Original image size: {original_width}x{original_height}") | |
| # Calculate optimal number of pages based on image height | |
| # We want each page to have a reasonable height when scaled to A4 width | |
| scale_factor = A4_WIDTH / original_width | |
| scaled_height = original_height * scale_factor | |
| # Calculate number of pages needed (each page should be roughly A4 height) | |
| num_pages = max(1, int(scaled_height / A4_HEIGHT) + (1 if scaled_height % A4_HEIGHT > 0 else 0)) | |
| print(f"Calculated {num_pages} pages needed for optimal display") | |
| # Calculate the height of each section | |
| section_height = original_height // num_pages | |
| print(f"Each section will be: {original_width}x{section_height}") | |
| # Create a list to store the A4 pages | |
| a4_pages = [] | |
| # Split the image into calculated number of sections | |
| for i in range(num_pages): | |
| # Calculate the y-coordinate for this section | |
| y_start = i * section_height | |
| y_end = (i + 1) * section_height | |
| # Handle the last section to include any remaining pixels | |
| if i == num_pages - 1: | |
| y_end = original_height | |
| print(f"Processing section {i+1}/{num_pages}: y={y_start} to y={y_end}") | |
| # Crop the section | |
| section = image.crop((0, y_start, original_width, y_end)) | |
| # Resize to A4 dimensions while maintaining aspect ratio | |
| # Calculate scaling factor to fit width | |
| scale_factor = A4_WIDTH / original_width | |
| new_height = int(section.height * scale_factor) | |
| # Resize the section | |
| resized_section = section.resize((A4_WIDTH, new_height), Image.Resampling.LANCZOS) | |
| # Create a new A4-sized image with white background | |
| a4_page = Image.new('RGB', (A4_WIDTH, A4_HEIGHT), 'white') | |
| # Paste the resized section onto the A4 page (centered vertically) | |
| y_offset = (A4_HEIGHT - new_height) // 2 | |
| a4_page.paste(resized_section, (0, y_offset)) | |
| a4_pages.append(a4_page) | |
| # Save as PDF | |
| print(f"Saving PDF to: {output_pdf_path}") | |
| a4_pages[0].save( | |
| output_pdf_path, | |
| format='PDF', | |
| resolution=300.0, | |
| save_all=True, | |
| append_images=a4_pages[1:] | |
| ) | |
| print(f"Successfully created PDF with {len(a4_pages)} pages!") | |
| print(f"Output file: {output_pdf_path}") | |
| print(f"Each page optimized for A4 display at 300 DPI") | |
| except FileNotFoundError: | |
| print(f"Error: Image file not found at {input_image_path}") | |
| sys.exit(1) | |
| except Exception as e: | |
| print(f"Error processing image: {str(e)}") | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| # Check if input image path is provided | |
| if len(sys.argv) != 2: | |
| print("Usage: python split_image_to_pdf.py <input_image_path>") | |
| print("Example: python split_image_to_pdf.py /path/to/image.png") | |
| sys.exit(1) | |
| input_path = sys.argv[1] | |
| # Generate output path based on input image name | |
| input_dir = os.path.dirname(input_path) | |
| input_filename = os.path.splitext(os.path.basename(input_path))[0] | |
| output_path = os.path.join(input_dir, f"{input_filename}.pdf") | |
| # Check if input file exists | |
| if not os.path.exists(input_path): | |
| print(f"Error: Input file does not exist: {input_path}") | |
| sys.exit(1) | |
| print(f"Output PDF will be saved as: {output_path}") | |
| split_image_to_pdf(input_path, output_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment