Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,14 +31,16 @@ def parse_page_range(range_str):
|
|
| 31 |
return sorted(pages)
|
| 32 |
|
| 33 |
# PDF轉圖片
|
| 34 |
-
def pdf_to_images(pdf_file,
|
| 35 |
images = []
|
| 36 |
with fitz.open(pdf_file.name) as doc:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
images.append(img)
|
| 43 |
return images
|
| 44 |
|
|
|
|
| 31 |
return sorted(pages)
|
| 32 |
|
| 33 |
# PDF轉圖片
|
| 34 |
+
def pdf_to_images(pdf_file, start_page=None, end_page=None):
|
| 35 |
images = []
|
| 36 |
with fitz.open(pdf_file.name) as doc:
|
| 37 |
+
total_pages = len(doc)
|
| 38 |
+
start = max(0, (start_page or 1) - 1)
|
| 39 |
+
end = min(end_page or total_pages, total_pages)
|
| 40 |
+
for page_num in range(start, end):
|
| 41 |
+
pix = doc[page_num].get_pixmap(matrix=fitz.Matrix(2, 2), colorspace=fitz.csRGB)
|
| 42 |
+
img = np.frombuffer(pix.samples, dtype=np.uint8).reshape((pix.height, pix.width, 3))
|
| 43 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # ⭐ 修正色彩順序
|
| 44 |
images.append(img)
|
| 45 |
return images
|
| 46 |
|