Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,6 +16,10 @@ logger = logging.getLogger(__name__)
|
|
16 |
def stitch_images_vertically(images: List[Image.Image]) -> Image.Image:
|
17 |
if not images:
|
18 |
return None
|
|
|
|
|
|
|
|
|
19 |
|
20 |
max_width = max(img.width for img in images)
|
21 |
total_height = sum(img.height for img in images)
|
@@ -33,11 +37,13 @@ def stitch_images_in_grid(images: List[Image.Image], num_columns: int) -> Image.
|
|
33 |
if not images:
|
34 |
return None
|
35 |
|
36 |
-
num_images = len(images)
|
37 |
columns = [images[i::num_columns] for i in range(num_columns)]
|
38 |
|
39 |
-
stitched_columns = [stitch_images_vertically(col) for col in columns]
|
40 |
|
|
|
|
|
|
|
41 |
max_height = max(col.height for col in stitched_columns if col)
|
42 |
total_width = sum(col.width for col in stitched_columns if col)
|
43 |
|
@@ -51,7 +57,7 @@ def stitch_images_in_grid(images: List[Image.Image], num_columns: int) -> Image.
|
|
51 |
|
52 |
return grid_image
|
53 |
|
54 |
-
def process_pdf(pdf_file, pdf_url, dpi, num_columns, progress=gr.Progress()):
|
55 |
pdf_input_source = None
|
56 |
is_bytes = False
|
57 |
source_name = "document"
|
@@ -93,12 +99,30 @@ def process_pdf(pdf_file, pdf_url, dpi, num_columns, progress=gr.Progress()):
|
|
93 |
|
94 |
logger.info(f"Successfully converted {len(images)} pages to images.")
|
95 |
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
if num_columns > 1:
|
99 |
-
stitched_image = stitch_images_in_grid(
|
100 |
else:
|
101 |
-
stitched_image = stitch_images_vertically(
|
102 |
|
103 |
if stitched_image is None:
|
104 |
raise gr.Error("Image stitching failed.")
|
@@ -136,42 +160,36 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
136 |
placeholder="e.g., https://arxiv.org/pdf/1706.03762.pdf"
|
137 |
)
|
138 |
|
139 |
-
dpi_slider = gr.Slider(
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
minimum=1,
|
150 |
-
maximum=10,
|
151 |
-
step=1,
|
152 |
-
value=1,
|
153 |
-
label="Number of Columns",
|
154 |
-
info="Increase to make the final image wider and less tall."
|
155 |
-
)
|
156 |
|
157 |
submit_btn = gr.Button("Stitch PDF Pages", variant="primary")
|
158 |
|
159 |
with gr.Column(scale=2):
|
160 |
gr.Markdown("## Output")
|
161 |
-
output_image_preview = gr.Image(
|
162 |
-
|
163 |
-
type="filepath",
|
164 |
-
interactive=False,
|
165 |
-
height=600,
|
166 |
-
)
|
167 |
-
output_image_download = gr.File(
|
168 |
-
label="Download Stitched Image",
|
169 |
-
interactive=False
|
170 |
-
)
|
171 |
|
172 |
submit_btn.click(
|
173 |
fn=process_pdf,
|
174 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
outputs=[output_image_preview, output_image_download]
|
176 |
)
|
177 |
|
|
|
16 |
def stitch_images_vertically(images: List[Image.Image]) -> Image.Image:
|
17 |
if not images:
|
18 |
return None
|
19 |
+
|
20 |
+
if not all(isinstance(i, Image.Image) for i in images):
|
21 |
+
logger.error("Non-Image object found in list for vertical stitching.")
|
22 |
+
return None
|
23 |
|
24 |
max_width = max(img.width for img in images)
|
25 |
total_height = sum(img.height for img in images)
|
|
|
37 |
if not images:
|
38 |
return None
|
39 |
|
|
|
40 |
columns = [images[i::num_columns] for i in range(num_columns)]
|
41 |
|
42 |
+
stitched_columns = [stitch_images_vertically(col) for col in columns if col]
|
43 |
|
44 |
+
if not stitched_columns:
|
45 |
+
return None
|
46 |
+
|
47 |
max_height = max(col.height for col in stitched_columns if col)
|
48 |
total_width = sum(col.width for col in stitched_columns if col)
|
49 |
|
|
|
57 |
|
58 |
return grid_image
|
59 |
|
60 |
+
def process_pdf(pdf_file, pdf_url, dpi, num_columns, crop_top, crop_bottom, crop_left, crop_right, progress=gr.Progress()):
|
61 |
pdf_input_source = None
|
62 |
is_bytes = False
|
63 |
source_name = "document"
|
|
|
99 |
|
100 |
logger.info(f"Successfully converted {len(images)} pages to images.")
|
101 |
|
102 |
+
cropped_images = []
|
103 |
+
if crop_top > 0 or crop_bottom > 0 or crop_left > 0 or crop_right > 0:
|
104 |
+
progress(0.6, desc="Cropping images...")
|
105 |
+
for i, img in enumerate(images):
|
106 |
+
width, height = img.size
|
107 |
+
|
108 |
+
left = crop_left
|
109 |
+
top = crop_top
|
110 |
+
right = width - crop_right
|
111 |
+
bottom = height - crop_bottom
|
112 |
+
|
113 |
+
if left >= right or top >= bottom:
|
114 |
+
raise gr.Error(f"Crop values are too large for page {i+1}. The page dimensions are {width}x{height}, but crop settings result in an invalid area.")
|
115 |
+
|
116 |
+
cropped_images.append(img.crop((left, top, right, bottom)))
|
117 |
+
else:
|
118 |
+
cropped_images = images
|
119 |
+
|
120 |
+
progress(0.7, desc=f"Stitching {len(cropped_images)} images together...")
|
121 |
|
122 |
if num_columns > 1:
|
123 |
+
stitched_image = stitch_images_in_grid(cropped_images, num_columns)
|
124 |
else:
|
125 |
+
stitched_image = stitch_images_vertically(cropped_images)
|
126 |
|
127 |
if stitched_image is None:
|
128 |
raise gr.Error("Image stitching failed.")
|
|
|
160 |
placeholder="e.g., https://arxiv.org/pdf/1706.03762.pdf"
|
161 |
)
|
162 |
|
163 |
+
dpi_slider = gr.Slider(minimum=100, maximum=600, step=50, value=200, label="Image Resolution (DPI)")
|
164 |
+
columns_slider = gr.Slider(minimum=1, maximum=10, step=1, value=1, label="Number of Columns")
|
165 |
+
|
166 |
+
with gr.Accordion("Advanced Options", open=False):
|
167 |
+
with gr.Row():
|
168 |
+
crop_left = gr.Slider(minimum=0, maximum=500, step=10, value=0, label="Crop Left")
|
169 |
+
crop_right = gr.Slider(minimum=0, maximum=500, step=10, value=0, label="Crop Right")
|
170 |
+
with gr.Row():
|
171 |
+
crop_top = gr.Slider(minimum=0, maximum=500, step=10, value=0, label="Crop Top")
|
172 |
+
crop_bottom = gr.Slider(minimum=0, maximum=500, step=10, value=0, label="Crop Bottom")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
submit_btn = gr.Button("Stitch PDF Pages", variant="primary")
|
175 |
|
176 |
with gr.Column(scale=2):
|
177 |
gr.Markdown("## Output")
|
178 |
+
output_image_preview = gr.Image(label="Stitched Image Preview", type="filepath", interactive=False, height=600)
|
179 |
+
output_image_download = gr.File(label="Download Stitched Image", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
submit_btn.click(
|
182 |
fn=process_pdf,
|
183 |
+
inputs=[
|
184 |
+
pdf_file_input,
|
185 |
+
pdf_url_input,
|
186 |
+
dpi_slider,
|
187 |
+
columns_slider,
|
188 |
+
crop_top,
|
189 |
+
crop_bottom,
|
190 |
+
crop_left,
|
191 |
+
crop_right
|
192 |
+
],
|
193 |
outputs=[output_image_preview, output_image_download]
|
194 |
)
|
195 |
|