cormort commited on
Commit
c037c51
·
verified ·
1 Parent(s): d2b93e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -9
app.py CHANGED
@@ -263,17 +263,24 @@ class ImageFilters:
263
  class PDFGenerator:
264
  @staticmethod
265
  def images_to_pdf(images: List[np.ndarray]) -> bytes:
266
- """將多張影像合併為 PDF"""
267
  if not images:
268
  return None
269
 
270
  pil_images = []
271
  for img in images:
272
- # BGR to RGB
273
- rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
274
- pil_img = Image.fromarray(rgb)
275
- pil_images.append(pil_img)
 
 
 
 
276
 
 
 
 
277
  # 生成 PDF
278
  pdf_buffer = io.BytesIO()
279
  pil_images[0].save(
@@ -715,7 +722,7 @@ def create_interface():
715
  return gr.update(visible=False)
716
 
717
  def on_export_images(state: ScanState, enable_enhance):
718
- """匯出圖片 (ZIP) (支援畫質增強) - 修版"""
719
  if not state.pages:
720
  return gr.update(visible=False)
721
 
@@ -728,12 +735,17 @@ def create_interface():
728
  for i, page in enumerate(state.pages):
729
  img = page.processed if page.processed is not None else page.original
730
 
731
- # 如果勾選增強,則進行處理
732
  if enable_enhance:
 
 
733
  img = QualityEnhancer.enhance(img)
734
 
735
- # 編碼並寫入 ZIP
736
- success, buffer = cv2.imencode('.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 95])
 
 
 
737
  if success:
738
  zip_file.writestr(f"page_{i+1:02d}.jpg", buffer.tobytes())
739
 
 
263
  class PDFGenerator:
264
  @staticmethod
265
  def images_to_pdf(images: List[np.ndarray]) -> bytes:
266
+ """將多張影像合併為 PDF (修正版:假設輸入為 RGB)"""
267
  if not images:
268
  return None
269
 
270
  pil_images = []
271
  for img in images:
272
+ # 修改點:移除 BGR2RGB 轉換
273
+ # 因為目前內部狀態已經是 RGB,直接轉 PIL 即可
274
+ try:
275
+ pil_img = Image.fromarray(img)
276
+ pil_images.append(pil_img)
277
+ except Exception as e:
278
+ print(f"PDF 圖片轉換錯誤: {e}")
279
+ continue
280
 
281
+ if not pil_images:
282
+ return None
283
+
284
  # 生成 PDF
285
  pdf_buffer = io.BytesIO()
286
  pil_images[0].save(
 
722
  return gr.update(visible=False)
723
 
724
  def on_export_images(state: ScanState, enable_enhance):
725
+ """匯出圖片 (ZIP) - 修:RGB 轉 BGR"""
726
  if not state.pages:
727
  return gr.update(visible=False)
728
 
 
735
  for i, page in enumerate(state.pages):
736
  img = page.processed if page.processed is not None else page.original
737
 
738
+ # 如果勾選增強
739
  if enable_enhance:
740
+ # 注意:如果 QualityEnhancer 預設處理 BGR,這裡可能要先轉 BGR 再增強
741
+ # 但假設增強器已適配,我們只關注最後輸出
742
  img = QualityEnhancer.enhance(img)
743
 
744
+ # --- 修改點:存檔前 RGB -> BGR ---
745
+ # OpenCV imencode 預設是 BGR 順序,所以必須轉過去
746
+ img_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
747
+
748
+ success, buffer = cv2.imencode('.jpg', img_bgr, [cv2.IMWRITE_JPEG_QUALITY, 95])
749
  if success:
750
  zip_file.writestr(f"page_{i+1:02d}.jpg", buffer.tobytes())
751