Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -214,23 +214,28 @@ def preview_pdf(pdf_path, scale_factor=0.5):
|
|
| 214 |
str: Path to the resized image preview.
|
| 215 |
"""
|
| 216 |
try:
|
| 217 |
-
doc = fitz.open(pdf_path) # Open PDF
|
| 218 |
-
first_page = doc[0] #
|
| 219 |
-
pix = first_page.get_pixmap() #
|
| 220 |
|
| 221 |
-
# Resize image using
|
| 222 |
new_width = int(pix.width * scale_factor)
|
| 223 |
new_height = int(pix.height * scale_factor)
|
| 224 |
-
resized_pix = fitz.Pixmap(pix,
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
# Save resized image temporarily
|
| 227 |
temp_image_path = os.path.join(tempfile.gettempdir(), "pdf_preview_resized.png")
|
| 228 |
-
resized_pix.save(temp_image_path)
|
| 229 |
return temp_image_path
|
|
|
|
| 230 |
except Exception as e:
|
| 231 |
st.error(f"Error generating PDF preview: {e}")
|
| 232 |
return None
|
| 233 |
|
|
|
|
| 234 |
if __name__ == "__main__":
|
| 235 |
st.set_page_config(
|
| 236 |
page_title="Patent Chat: Google Patents Chat Demo",
|
|
|
|
| 214 |
str: Path to the resized image preview.
|
| 215 |
"""
|
| 216 |
try:
|
| 217 |
+
doc = fitz.open(pdf_path) # Open the PDF document
|
| 218 |
+
first_page = doc[0] # Extract the first page
|
| 219 |
+
pix = first_page.get_pixmap() # Get the pixmap (image) of the page
|
| 220 |
|
| 221 |
+
# Resize the image manually using width and height scaling
|
| 222 |
new_width = int(pix.width * scale_factor)
|
| 223 |
new_height = int(pix.height * scale_factor)
|
| 224 |
+
resized_pix = fitz.Pixmap(pix, 0) # Clone the pixmap
|
| 225 |
+
|
| 226 |
+
# Manually downscale the image using `fitz.Pixmap` constructor
|
| 227 |
+
resized_pix = fitz.Pixmap(fitz.csRGB, pix, new_width, new_height)
|
| 228 |
|
| 229 |
# Save resized image temporarily
|
| 230 |
temp_image_path = os.path.join(tempfile.gettempdir(), "pdf_preview_resized.png")
|
| 231 |
+
resized_pix.save(temp_image_path) # Save the resized image
|
| 232 |
return temp_image_path
|
| 233 |
+
|
| 234 |
except Exception as e:
|
| 235 |
st.error(f"Error generating PDF preview: {e}")
|
| 236 |
return None
|
| 237 |
|
| 238 |
+
|
| 239 |
if __name__ == "__main__":
|
| 240 |
st.set_page_config(
|
| 241 |
page_title="Patent Chat: Google Patents Chat Demo",
|