Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from pdf2image import convert_from_path | |
| from pathlib import Path | |
| import uuid | |
| def pdf_to_jpeg(pdf_file, dpi, quality): | |
| """ | |
| Converts a PDF file into a list of high-resolution JPEG images. | |
| Args: | |
| pdf_file (str): Path to the uploaded PDF file. | |
| dpi (int): Dots per inch for resolution. | |
| quality (int): JPEG quality (1-100). | |
| Returns: | |
| list: List of paths to the generated JPEG files. | |
| """ | |
| if pdf_file is None: | |
| raise gr.Error("Please upload a PDF file first.") | |
| try: | |
| # Create a unique directory for this session's output to avoid collisions | |
| session_id = str(uuid.uuid4()) | |
| output_dir = Path(f"outputs/{session_id}") | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| # Convert PDF to images | |
| # dpi=300 is generally considered high resolution for printing/viewing | |
| images = convert_from_path(pdf_file, dpi=dpi) | |
| jpeg_paths = [] | |
| for i, image in enumerate(images): | |
| # Generate filename: page_1.jpg, page_2.jpg... | |
| file_path = output_dir / f"page_{i+1}.jpg" | |
| # Save as JPEG with specified quality | |
| image.save(file_path, "JPEG", quality=quality) | |
| jpeg_paths.append(str(file_path)) | |
| return jpeg_paths | |
| except Exception as e: | |
| raise gr.Error(f"An error occurred during conversion: {str(e)}") | |
| # Custom professional theme for Gradio 6 | |
| custom_theme = gr.themes.Soft( | |
| primary_hue="indigo", | |
| secondary_hue="slate", | |
| neutral_hue="gray", | |
| font=gr.themes.GoogleFont("Inter"), | |
| spacing_size="lg", | |
| radius_size="md" | |
| ).set( | |
| button_primary_background_fill="*primary_600", | |
| button_primary_background_fill_hover="*primary_700", | |
| ) | |
| with gr.Blocks() as demo: | |
| # Header section | |
| gr.Markdown( | |
| """ | |
| # 📄 PDF to High-Res JPEG Converter | |
| Convert your PDF documents into professional, high-resolution JPEG images. | |
| Each page of your PDF will be processed as a separate high-quality image slice. | |
| *** | |
| [Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder) | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("### ⚙️ Conversion Settings") | |
| pdf_input = gr.File( | |
| label="Upload PDF", | |
| file_types=[".pdf"], | |
| file_count="single" | |
| ) | |
| with gr.Group(): | |
| dpi_slider = gr.Slider( | |
| minimum=72, | |
| maximum=600, | |
| value=300, | |
| step=1, | |
| label="Resolution (DPI)", | |
| info="300 DPI is standard for high quality. 600 DPI is ultra-high." | |
| ) | |
| quality_slider = gr.Slider( | |
| minimum=1, | |
| maximum=100, | |
| value=95, | |
| step=1, | |
| label="JPEG Quality", | |
| info="Higher value means better quality but larger file size." | |
| ) | |
| convert_btn = gr.Button("Convert to JPEG", variant="primary", size="lg") | |
| clear_btn = gr.Button("Clear All", variant="secondary") | |
| with gr.Column(scale=2): | |
| gr.Markdown("### 🖼️ Generated Images") | |
| # Gallery is perfect for showing multiple slices of the PDF | |
| output_gallery = gr.Gallery( | |
| label="Processed Pages", | |
| show_label=False, | |
| columns=2, | |
| object_fit="contain", | |
| height="auto" | |
| ) | |
| # Event Listeners | |
| convert_btn.click( | |
| fn=pdf_to_jpeg, | |
| inputs=[pdf_input, dpi_slider, quality_slider], | |
| outputs=[output_gallery], | |
| api_visibility="public" | |
| ) | |
| clear_btn.click( | |
| fn=lambda: (None, None), | |
| inputs=None, | |
| outputs=[pdf_input, output_gallery], | |
| api_visibility="public" | |
| ) | |
| # Gradio 6 Launch Configuration | |
| demo.launch( | |
| theme=custom_theme, | |
| footer_links=[ | |
| {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"} | |
| ] | |
| ) |