use fitz to export images of pdf
Browse files- main.py +43 -12
- requirements.txt +6 -0
main.py
CHANGED
|
@@ -142,14 +142,18 @@ def ocr_one_pdf(pth_pdf):
|
|
| 142 |
from pathlib import Path
|
| 143 |
import os
|
| 144 |
import json
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
base = Path(pth_pdf).stem
|
| 147 |
dir = os.path.dirname(pth_pdf)
|
| 148 |
|
| 149 |
|
| 150 |
-
def get_page_image(reader, page_num):
|
| 151 |
page_num = int(page_num)
|
| 152 |
page = reader.pages[page_num - 1]
|
|
|
|
| 153 |
text = page.extract_text()
|
| 154 |
# try:
|
| 155 |
# for idx, image_file_object in enumerate(page.images):
|
|
@@ -157,15 +161,38 @@ def ocr_one_pdf(pth_pdf):
|
|
| 157 |
# except Exception:
|
| 158 |
# return None
|
| 159 |
# 提取不到图像先用 Acrobat 导出全部图片,再新建 pdf 替换原 pdf 就可以了
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
return ocr_frame
|
| 170 |
|
| 171 |
def md5_file(fname):
|
|
@@ -191,7 +218,11 @@ def ocr_one_pdf(pth_pdf):
|
|
| 191 |
|
| 192 |
from pypdf import PdfReader
|
| 193 |
reader = PdfReader(pth_pdf)
|
|
|
|
| 194 |
number_of_pages = len(reader.pages)
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
# see huggingface/PPOCRLabel/PPOCRLabel.py for image notation
|
| 197 |
name_pp_label = 'Label.txt'
|
|
@@ -208,10 +239,10 @@ def ocr_one_pdf(pth_pdf):
|
|
| 208 |
|
| 209 |
for nth_page in range(1, number_of_pages+1):
|
| 210 |
|
| 211 |
-
if nth_page >
|
| 212 |
break
|
| 213 |
|
| 214 |
-
img_color = get_page_image(reader, nth_page)
|
| 215 |
if img_color is None:
|
| 216 |
continue
|
| 217 |
cv2.imwrite('./tmp.jpg', cv2.cvtColor(img_color, cv2.COLOR_RGB2BGR))
|
|
@@ -339,6 +370,6 @@ if __name__ == '__main__':
|
|
| 339 |
# ocr_one_pdf('pdfs/en/TIME片挑200/TIME片挑200.pdf')
|
| 340 |
|
| 341 |
|
| 342 |
-
ocr_one_pdf('pdfs/
|
| 343 |
|
| 344 |
|
|
|
|
| 142 |
from pathlib import Path
|
| 143 |
import os
|
| 144 |
import json
|
| 145 |
+
|
| 146 |
+
import fitz
|
| 147 |
+
from PIL import Image # 需要安装Pillow库
|
| 148 |
|
| 149 |
base = Path(pth_pdf).stem
|
| 150 |
dir = os.path.dirname(pth_pdf)
|
| 151 |
|
| 152 |
|
| 153 |
+
def get_page_image(reader, pdfDoc, page_num):
|
| 154 |
page_num = int(page_num)
|
| 155 |
page = reader.pages[page_num - 1]
|
| 156 |
+
|
| 157 |
text = page.extract_text()
|
| 158 |
# try:
|
| 159 |
# for idx, image_file_object in enumerate(page.images):
|
|
|
|
| 161 |
# except Exception:
|
| 162 |
# return None
|
| 163 |
# 提取不到图像先用 Acrobat 导出全部图片,再新建 pdf 替换原 pdf 就可以了
|
| 164 |
+
|
| 165 |
+
page = pdfDoc[page_num - 1]
|
| 166 |
+
# 关键参数设置(保持原始尺寸和分辨率)
|
| 167 |
+
matrix = fitz.Matrix(fitz.Identity) # 使用Identity矩阵确保不缩放[6,8](@ref)
|
| 168 |
+
pix = page.get_pixmap(
|
| 169 |
+
matrix=matrix, # 保持原始尺寸
|
| 170 |
+
dpi=300, # 设置输出分辨率(默认72dpi)
|
| 171 |
+
alpha=False, # 关闭透明通道(提高兼容性)
|
| 172 |
+
colorspace="rgb" # 使用RGB色彩空间[8](@ref)
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
from io import BytesIO
|
| 176 |
+
byte_io = BytesIO()
|
| 177 |
+
# 转换到内存流
|
| 178 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 179 |
+
byte_io = BytesIO()
|
| 180 |
+
img.save(byte_io, format="JPEG", quality=100) # 使用无损压缩
|
| 181 |
+
byte_io.seek(0)
|
| 182 |
+
img_bytes = byte_io.read()
|
| 183 |
|
| 184 |
+
img_buffer_numpy = np.frombuffer(img_bytes, dtype=np.uint8) # 将图片字节码 bytes 转换成一维的 numpy 数组到缓存中
|
| 185 |
+
ocr_frame = cv2.imdecode(img_buffer_numpy, 1) # 从指定的内存缓存中读取一维 numpy 数据,并把数据转换(解码)成图像矩阵格式
|
| 186 |
+
ocr_frame = cv2.cvtColor(ocr_frame, cv2.COLOR_BGR2RGB)
|
| 187 |
+
|
| 188 |
+
# for idx, image_file_object in enumerate(page.images):
|
| 189 |
+
# img_bytes = image_file_object.data
|
| 190 |
+
# img_buffer_numpy = np.frombuffer(img_bytes, dtype=np.uint8) # 将图片字节码 bytes 转换成一维的 numpy 数组到缓存中
|
| 191 |
+
# ocr_frame = cv2.imdecode(img_buffer_numpy, 1) # 从指定的内存缓存中读取一维 numpy 数据,并把数据转换(解码)成图像矩阵格式
|
| 192 |
+
# ocr_frame = cv2.cvtColor(ocr_frame, cv2.COLOR_BGR2RGB)
|
| 193 |
+
# # cv2.imshow('test', self.ocr_frame)
|
| 194 |
+
# # cv2.waitKey(0)
|
| 195 |
+
# break
|
| 196 |
return ocr_frame
|
| 197 |
|
| 198 |
def md5_file(fname):
|
|
|
|
| 218 |
|
| 219 |
from pypdf import PdfReader
|
| 220 |
reader = PdfReader(pth_pdf)
|
| 221 |
+
pdfDoc = fitz.open(pth_pdf)
|
| 222 |
number_of_pages = len(reader.pages)
|
| 223 |
+
number_of_pages2 = pdfDoc.page_count
|
| 224 |
+
|
| 225 |
+
assert number_of_pages == number_of_pages2
|
| 226 |
|
| 227 |
# see huggingface/PPOCRLabel/PPOCRLabel.py for image notation
|
| 228 |
name_pp_label = 'Label.txt'
|
|
|
|
| 239 |
|
| 240 |
for nth_page in range(1, number_of_pages+1):
|
| 241 |
|
| 242 |
+
if nth_page > 10:
|
| 243 |
break
|
| 244 |
|
| 245 |
+
img_color = get_page_image(reader, pdfDoc, nth_page)
|
| 246 |
if img_color is None:
|
| 247 |
continue
|
| 248 |
cv2.imwrite('./tmp.jpg', cv2.cvtColor(img_color, cv2.COLOR_RGB2BGR))
|
|
|
|
| 370 |
# ocr_one_pdf('pdfs/en/TIME片挑200/TIME片挑200.pdf')
|
| 371 |
|
| 372 |
|
| 373 |
+
ocr_one_pdf('pdfs/jp/荻原雲来梵语漢訳対照梵和大辞典/荻原雲来梵语漢訳対照梵和大辞典.pdf') # 李致雨N2词汇详解 提取的图片几乎不可见,不要了
|
| 374 |
|
| 375 |
|
requirements.txt
CHANGED
|
@@ -70,3 +70,9 @@ Werkzeug==3.0.4
|
|
| 70 |
######### PaddleOCR_ali1k_det_rec_300epoch end ########################
|
| 71 |
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
######### PaddleOCR_ali1k_det_rec_300epoch end ########################
|
| 71 |
|
| 72 |
|
| 73 |
+
######### export images of pdf begin ########################
|
| 74 |
+
fitz==0.0.1.dev2
|
| 75 |
+
pymupdf==1.25.4
|
| 76 |
+
pillow==10.4.0
|
| 77 |
+
# 先装 fitz 再装 pymupdf 才行。如果出错,先卸载 pymupdf ,再装一次 pymupdf 又不错了
|
| 78 |
+
######### export images of pdf end ########################
|