| import fitz |
| import gradio as gr |
| from PIL import Image, ImageEnhance, ImageFilter |
| import io |
|
|
| |
| def pdf_to_image(pdf_file, dpi=600): |
| try: |
| print(f"Opening PDF: {pdf_file.name}") |
| pdf_document = fitz.open(pdf_file.name) |
| page = pdf_document.load_page(0) |
|
|
| |
| zoom = dpi / 72 |
| mat = fitz.Matrix(zoom, zoom) |
| pix = page.get_pixmap(matrix=mat, alpha=False) |
|
|
| |
| image_bytes = io.BytesIO(pix.tobytes("png")) |
| img = Image.open(image_bytes) |
| return img |
| except Exception as e: |
| print(f"Error during PDF conversion: {str(e)}") |
| return None |
|
|
| |
| def inches_to_pixels(inches, dpi=600): |
| return int(inches * dpi) |
|
|
| |
| def adjust_image(pdf_file, height_in, width_in, brightness): |
| img = pdf_to_image(pdf_file) |
| if img is None: |
| return "Unable to convert PDF to image." |
|
|
| try: |
| |
| height_px = inches_to_pixels(height_in) |
| width_px = inches_to_pixels(width_in) |
|
|
| |
| img_resized = img.resize((width_px, height_px), Image.LANCZOS) |
|
|
| |
| img_smoothed = img_resized.filter(ImageFilter.SMOOTH) |
|
|
| |
| enhancer = ImageEnhance.Brightness(img_smoothed) |
| img_bright = enhancer.enhance(brightness) |
|
|
| return img_bright |
| except Exception as e: |
| print(f"Error during image adjustment: {str(e)}") |
| return "Error adjusting the image." |
|
|
| |
| iface = gr.Interface( |
| fn=adjust_image, |
| inputs=[ |
| gr.File(label="Upload PDF"), |
| gr.Number(label="Height (inches)", value=5.0), |
| gr.Number(label="Width (inches)", value=4.0), |
| gr.Slider(minimum=0.5, maximum=2, step=0.1, value=1.0, label="Brightness"), |
| ], |
| outputs="image", |
| title="Plate Design Creation - Convert & Adjust PDF Page", |
| description="Upload a PDF containing a technical diagram or nameplate. Adjust the dimensions and brightness for optimal clarity." |
| ) |
|
|
| |
| iface.launch() |