Python: Great for quick tasks.
*****************************************************************

Python: Great for quick tasks.

For every software developer, especially a full-stack developer or a programming team lead, any system requires logistical support, which means small programs that assist in preparing data, tools, images, and any type of service needed for the product. Instead of searching for helper programs to perform these operations, using the Python language to produce fast service-oriented software is more suitable than any other tool.

For example, if I have a 600-page PDF file and I need to transfer these pages after cropping the frame from each page to files in a specific format and resolution, then a single page of code using specific packages is enough to perform this task very quickly. This saves a lot of time, effort, and ensures the necessary output.

Example:

import fitz  
from PIL import Image

def crop_and_save(pdf_path, output_folder, x, y, width, height, dpi=300, quality=95):
    # Open the PDF file
    pdf_document = fitz.open(pdf_path)
    # Iterate through each page
    for page_number in range(pdf_document.page_count):
        # Get the page
        page = pdf_document[page_number]
        # Calculate the crop rectangle
        rect = fitz.Rect(x, y, x + width, y + height)
        # Crop the page
        cropped_page = page.get_pixmap(matrix=fitz.Matrix(dpi / 72.0, dpi / 72.0), clip=rect)
        # Convert the cropped page to a PIL Image
        image = Image.frombytes("RGB", (cropped_page.width, cropped_page.height), cropped_page.samples)
        # Save the cropped image as JPG with higher DPI and quality
        output_path = f"{output_folder}/page_{page_number + 1}.jpg"
        image.save(output_path, format="JPEG", dpi=(dpi, dpi), quality=quality)
    # Close the PDF file
    pdf_document.close()
# Example usage
pdf_path = "filename.pdf"
output_folder = "pages"

# Set the fixed crop parameters for all pages [x, y, width, height]
x, y, width, height = 20, 40, 253, 410

crop_and_save(pdf_path, output_folder, x, y, width, height, dpi=300, quality=70)        

This code snippet demonstrates how to quickly and efficiently crop and save images from a PDF file using Python's pdf2image library. It highlights the effectiveness of Python in automating repetitive tasks and streamlining workflows.

要查看或添加评论,请登录

Ayman Alheraki的更多文章

社区洞察

其他会员也浏览了