This commit is contained in:
512song@sina.com committed 2023-01-05 14:40:57 +08:00
1 parent 0d605803ee
commit a0030fc57c
2 files changed
+137 -126

No files matched your search

+57 -28
View File
@@ -411,9 +411,60 @@
{
"cell_type": "code",
"execution_count": null,
"id": "88d84849-5b98-48cb-86b1-e96757a8615f",
"metadata": {},
"id": "99fff4b6-73ea-4d53-96bc-41b6b664e00a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import fitz\n",
"import os\n",
"\n",
"sor = \"中华药膳全书学做药膳不生病.pdf\" # 需要压缩的PDF文件\n",
"\n",
"doc = fitz.open(sor) \n",
"totaling = doc.pageCount\n",
"\n",
"zoom = 300 # 清晰度调节,缩放比率\n",
"if os.path.exists('pdf_cp'): # 临时文件,需为空\n",
" os.removedirs('pdf_cp')\n",
"os.mkdir('pdf_cp')\n",
"for pg in range(6,totaling):\n",
" page = doc[pg]\n",
" zoom = int(zoom) #值越大,分辨率越高,文件越清晰\n",
" rotate = int(0)\n",
" print(page)\n",
" trans = fitz.Matrix(zoom / 100.0, zoom / 100.0).preRotate(rotate)\n",
" pm = page.getPixmap(matrix=trans, alpha=False)\n",
"\n",
" lurl='pdf_cp/%s.jpg' % str(pg+1).rjust(3,\"0\")\n",
" pm.writePNG(lurl)\n",
"doc.close()\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "88d84849-5b98-48cb-86b1-e96757a8615f",
"metadata": {
"execution": {
"iopub.execute_input": "2023-01-05T05:09:41.478369Z",
"iopub.status.busy": "2023-01-05T05:09:41.477814Z",
"iopub.status.idle": "2023-01-05T05:11:07.156560Z",
"shell.execute_reply": "2023-01-05T05:11:07.155155Z",
"shell.execute_reply.started": "2023-01-05T05:09:41.478316Z"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/tmp/tmp0tguht7x\n"
]
}
],
"source": [
"from pdf2image import convert_from_path, convert_from_bytes\n",
"import os,sys\n",
@@ -425,7 +476,7 @@
")\n",
"#images = convert_from_path('1.pdf',dpi=200,fmt='jpg', output_folder='./sample_data')\n",
"with tempfile.TemporaryDirectory() as path:\n",
" images_from_path = convert_from_path('5.pdf', dpi=100,fmt='jpg', output_folder='./pic')\n",
" images_from_path = convert_from_path('中华药膳全书学做药膳不生病.pdf', dpi=300,fmt='jpg', output_folder='./pic')\n",
"print(path)"
]
},
@@ -650,16 +701,9 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": null,
"id": "9957a90c-7735-4224-b5b5-4a4b1b7688c9",
"metadata": {
"execution": {
"iopub.execute_input": "2022-12-12T14:12:26.428992Z",
"iopub.status.busy": "2022-12-12T14:12:26.428458Z",
"iopub.status.idle": "2022-12-12T14:12:26.536571Z",
"shell.execute_reply": "2022-12-12T14:12:26.535612Z",
"shell.execute_reply.started": "2022-12-12T14:12:26.428944Z"
},
"tags": []
},
"outputs": [],
@@ -683,27 +727,12 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": null,
"id": "42122edc-eb77-4cfc-bcd8-3f844ffcca58",
"metadata": {
"execution": {
"iopub.execute_input": "2022-12-07T07:30:29.738226Z",
"iopub.status.busy": "2022-12-07T07:30:29.737684Z",
"iopub.status.idle": "2022-12-07T07:30:30.656573Z",
"shell.execute_reply": "2022-12-07T07:30:30.655231Z",
"shell.execute_reply.started": "2022-12-07T07:30:29.738176Z"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"转换完成\n"
]
}
],
"outputs": [],
"source": [
"from subprocess import Popen\n",
"\n",