STLooo commited on
Commit
75b73d9
·
verified ·
1 Parent(s): 97ead3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
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, selected_pages=None):
35
  images = []
36
  with fitz.open(pdf_file.name) as doc:
37
- for i, page in enumerate(doc, 1):
38
- if selected_pages and i not in selected_pages:
39
- continue
40
- pix = page.get_pixmap(matrix=fitz.Matrix(2, 2), colorspace=fitz.csRGB)
41
- img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
 
 
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