做畢業(yè)論文的網(wǎng)站怎樣創(chuàng)建自己的網(wǎng)站
現(xiàn)在確實(shí)有很多線上的工具可以把pdf文件轉(zhuǎn)為圖片,比如smallpdf等等,都很好用。但我們有時(shí)會(huì)碰到一些敏感數(shù)據(jù),或者要批量去轉(zhuǎn),那么需要自己寫腳本來實(shí)現(xiàn),以下腳本可以提供這個(gè)功能~
def pdf2img(pdf_dir, result_path):if not os.path.exists(result_path):os.makedirs(result_path, exist_ok=True)pdf_path_list = glob.glob(os.path.join(pdf_dir, "*.pdf"))for pdf_path in tqdm(pdf_path_list):pdf_name = os.path.split(pdf_path)[1].split(".")[0]with fitz.open(pdf_path) as pdf:pdf_page_count = pdf.pageCountfor pg in range(0, pdf_page_count):page = pdf[pg]mat = fitz.Matrix(2, 2)pm = page.getPixmap(matrix=mat, alpha=False)# if width or height > 2000 pixels, don't enlarge the imageif pm.width > 2000 or pm.height > 2000:pm = page.getPixmap(matrix=fitz.Matrix(1, 1), alpha=False)img = Image.frombytes("RGB", [pm.width, pm.height], pm.samples)# img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)img = Image.fromarray(np.array(img))img.save(os.path.join(result_path, f"{pdf_name}_{pg}.jpg"))
說明:
pdf_dir:pdf文件的路徑
result_path:保存的路徑
以上即可完成pdf文件轉(zhuǎn)換為圖片, Enjoy~
~ O n e p e r s o n g o f a s t e r , a g r o u p o f p e o p l e c a n g o f u r t h e r ~ \sim_{One\ person\ go\ faster,\ a\ group\ of\ people\ can\ go\ further}\sim ~One?person?go?faster,?a?group?of?people?can?go?further?~