Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import tempfile
|
| 3 |
import logging
|
| 4 |
from typing import List
|
|
|
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import requests
|
|
@@ -31,10 +32,35 @@ def stitch_images_vertically(images: List[Image.Image]) -> Image.Image:
|
|
| 31 |
|
| 32 |
return stitched_image
|
| 33 |
|
| 34 |
-
def
|
| 35 |
"""
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
"""
|
| 39 |
pdf_input_source = None
|
| 40 |
is_bytes = False
|
|
@@ -79,7 +105,11 @@ def process_pdf(pdf_file, pdf_url, dpi, progress=gr.Progress()):
|
|
| 79 |
|
| 80 |
progress(0.7, desc=f"Stitching {len(images)} images together...")
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
if stitched_image is None:
|
| 84 |
raise gr.Error("Image stitching failed.")
|
| 85 |
|
|
@@ -101,7 +131,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 101 |
"""
|
| 102 |
# PDF Page Stitcher 📄 ➡️ 🖼️
|
| 103 |
Upload a PDF file or provide a URL. This tool will convert every page of the PDF into an image
|
| 104 |
-
and then append them
|
| 105 |
"""
|
| 106 |
)
|
| 107 |
|
|
@@ -115,7 +145,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 115 |
label="PDF URL",
|
| 116 |
placeholder="e.g., https://arxiv.org/pdf/1706.03762.pdf"
|
| 117 |
)
|
| 118 |
-
|
| 119 |
dpi_slider = gr.Slider(
|
| 120 |
minimum=100,
|
| 121 |
maximum=600,
|
|
@@ -124,6 +154,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 124 |
label="Image Resolution (DPI)",
|
| 125 |
info="Higher DPI results in a clearer image but increases processing time and file size."
|
| 126 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
submit_btn = gr.Button("Stitch PDF Pages", variant="primary")
|
| 129 |
|
|
@@ -142,7 +181,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 142 |
|
| 143 |
submit_btn.click(
|
| 144 |
fn=process_pdf,
|
| 145 |
-
inputs=[pdf_file_input, pdf_url_input, dpi_slider],
|
| 146 |
outputs=[output_image_preview, output_image_download]
|
| 147 |
)
|
| 148 |
|
|
|
|
| 2 |
import tempfile
|
| 3 |
import logging
|
| 4 |
from typing import List
|
| 5 |
+
import math
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
import requests
|
|
|
|
| 32 |
|
| 33 |
return stitched_image
|
| 34 |
|
| 35 |
+
def stitch_images_in_grid(images: List[Image.Image], num_columns: int) -> Image.Image:
|
| 36 |
"""
|
| 37 |
+
Stitches images into a grid with a specified number of columns.
|
| 38 |
+
"""
|
| 39 |
+
if not images:
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
+
num_images = len(images)
|
| 43 |
+
num_rows = math.ceil(num_images / num_columns)
|
| 44 |
+
|
| 45 |
+
columns = [images[i::num_columns] for i in range(num_columns)]
|
| 46 |
+
|
| 47 |
+
stitched_columns = [stitch_images_vertically(col) for col in columns]
|
| 48 |
+
|
| 49 |
+
max_height = max(col.height for col in stitched_columns if col)
|
| 50 |
+
total_width = sum(col.width for col in stitched_columns if col)
|
| 51 |
+
|
| 52 |
+
grid_image = Image.new('RGB', (total_width, max_height), (255, 255, 255))
|
| 53 |
+
|
| 54 |
+
current_x = 0
|
| 55 |
+
for col_img in stitched_columns:
|
| 56 |
+
if col_img:
|
| 57 |
+
grid_image.paste(col_img, (current_x, 0))
|
| 58 |
+
current_x += col_img.width
|
| 59 |
+
|
| 60 |
+
return grid_image
|
| 61 |
+
|
| 62 |
+
"""
|
| 63 |
+
Processes a PDF, converts it to images, and stitches them into a single image or a grid.
|
| 64 |
"""
|
| 65 |
pdf_input_source = None
|
| 66 |
is_bytes = False
|
|
|
|
| 105 |
|
| 106 |
progress(0.7, desc=f"Stitching {len(images)} images together...")
|
| 107 |
|
| 108 |
+
if num_columns > 1:
|
| 109 |
+
stitched_image = stitch_images_in_grid(images, num_columns)
|
| 110 |
+
else:
|
| 111 |
+
stitched_image = stitch_images_vertically(images)
|
| 112 |
+
|
| 113 |
if stitched_image is None:
|
| 114 |
raise gr.Error("Image stitching failed.")
|
| 115 |
|
|
|
|
| 131 |
"""
|
| 132 |
# PDF Page Stitcher 📄 ➡️ 🖼️
|
| 133 |
Upload a PDF file or provide a URL. This tool will convert every page of the PDF into an image
|
| 134 |
+
and then append them to create a single image that you can download.
|
| 135 |
"""
|
| 136 |
)
|
| 137 |
|
|
|
|
| 145 |
label="PDF URL",
|
| 146 |
placeholder="e.g., https://arxiv.org/pdf/1706.03762.pdf"
|
| 147 |
)
|
| 148 |
+
|
| 149 |
dpi_slider = gr.Slider(
|
| 150 |
minimum=100,
|
| 151 |
maximum=600,
|
|
|
|
| 154 |
label="Image Resolution (DPI)",
|
| 155 |
info="Higher DPI results in a clearer image but increases processing time and file size."
|
| 156 |
)
|
| 157 |
+
|
| 158 |
+
columns_slider = gr.Slider(
|
| 159 |
+
minimum=1,
|
| 160 |
+
maximum=10,
|
| 161 |
+
step=1,
|
| 162 |
+
value=1,
|
| 163 |
+
label="Number of Columns",
|
| 164 |
+
info="Increase to make the final image wider and less tall."
|
| 165 |
+
)
|
| 166 |
|
| 167 |
submit_btn = gr.Button("Stitch PDF Pages", variant="primary")
|
| 168 |
|
|
|
|
| 181 |
|
| 182 |
submit_btn.click(
|
| 183 |
fn=process_pdf,
|
| 184 |
+
inputs=[pdf_file_input, pdf_url_input, dpi_slider, columns_slider],
|
| 185 |
outputs=[output_image_preview, output_image_download]
|
| 186 |
)
|
| 187 |
|