Spaces:
Sleeping
Sleeping
gene commited on
Commit ·
02b2d80
1
Parent(s): dc07627
pdf to image
Browse files- app.py +51 -45
- requirements.txt +2 -3
app.py
CHANGED
|
@@ -1,62 +1,68 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
-
def
|
| 7 |
"""
|
| 8 |
-
|
| 9 |
"""
|
| 10 |
-
if
|
| 11 |
-
return "Please upload
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Create the Gradio interface
|
| 43 |
-
with gr.Blocks(title="Image
|
| 44 |
-
gr.Markdown("#
|
| 45 |
-
gr.Markdown("Upload
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
with gr.Column():
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
with gr.Column():
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
fn=
|
| 58 |
-
inputs=[
|
| 59 |
-
outputs=[
|
| 60 |
)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import fitz # PyMuPDF
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
|
| 6 |
+
def convert_pdf_to_image(pdf_file):
|
| 7 |
"""
|
| 8 |
+
Convert PDF to image(s) and return the first page as PIL Image
|
| 9 |
"""
|
| 10 |
+
if pdf_file is None:
|
| 11 |
+
return "Please upload a PDF file first.", None
|
| 12 |
|
| 13 |
+
try:
|
| 14 |
+
# Open the PDF file
|
| 15 |
+
pdf_document = fitz.open(pdf_file.name)
|
| 16 |
+
|
| 17 |
+
# Get the first page
|
| 18 |
+
first_page = pdf_document[0]
|
| 19 |
+
|
| 20 |
+
# Convert page to image (pixmap)
|
| 21 |
+
pix = first_page.get_pixmap(matrix=fitz.Matrix(2, 2)) # 2x zoom for better quality
|
| 22 |
+
|
| 23 |
+
# Convert pixmap to PIL Image
|
| 24 |
+
img_data = pix.tobytes("png")
|
| 25 |
+
img = Image.open(io.BytesIO(img_data))
|
| 26 |
+
|
| 27 |
+
# Close the PDF document
|
| 28 |
+
pdf_document.close()
|
| 29 |
+
|
| 30 |
+
# Get PDF info
|
| 31 |
+
pdf_info = f"""
|
| 32 |
+
**PDF Conversion Results:**
|
| 33 |
+
- Total pages: {len(pdf_document)}
|
| 34 |
+
- First page converted to image
|
| 35 |
+
- Image size: {img.width} x {img.height} pixels
|
| 36 |
+
- Format: PNG
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
return pdf_info, img
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"Error processing PDF: {str(e)}", None
|
| 43 |
|
| 44 |
# Create the Gradio interface
|
| 45 |
+
with gr.Blocks(title="PDF to Image Converter") as demo:
|
| 46 |
+
gr.Markdown("# 📄 PDF to Image Converter")
|
| 47 |
+
gr.Markdown("Upload a PDF file to convert the first page to an image!")
|
| 48 |
|
| 49 |
with gr.Row():
|
| 50 |
with gr.Column():
|
| 51 |
+
pdf_input = gr.File(
|
| 52 |
+
label="Upload PDF File",
|
| 53 |
+
file_types=[".pdf"],
|
| 54 |
+
file_count="single"
|
| 55 |
+
)
|
| 56 |
+
convert_btn = gr.Button("Convert to Image", variant="primary")
|
| 57 |
|
| 58 |
with gr.Column():
|
| 59 |
+
conversion_output = gr.Markdown(label="Conversion Results")
|
| 60 |
+
converted_image = gr.Image(label="Converted Image (First Page)")
|
| 61 |
|
| 62 |
+
convert_btn.click(
|
| 63 |
+
fn=convert_pdf_to_image,
|
| 64 |
+
inputs=[pdf_input],
|
| 65 |
+
outputs=[conversion_output, converted_image]
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
gradio==5.49.0
|
| 2 |
-
|
| 3 |
-
Pillow==10.0.1
|
| 4 |
-
numpy==1.24.3
|
|
|
|
| 1 |
gradio==5.49.0
|
| 2 |
+
PyMuPDF==1.23.8
|
| 3 |
+
Pillow==10.0.1
|
|
|