Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,14 @@
"outputs": [],
"source": [
"import requests\n",
"import fitz\n",
"from PIL import Image\n",
"import pdfplumber\n",
"import io\n",
"import base64\n",
"import boto3\n",
"import os\n",
"from urllib.parse import urlparse\n",
"import tools\n",
"\n",
"\n",
"\n",
Expand All @@ -186,21 +187,19 @@
" return None\n",
"\n",
"def pdf_to_images(pdf_content, quality=75, max_size=(1024, 1024)):\n",
" \"\"\"Convert PDF to list of images\"\"\"\n",
" \"\"\"Convert PDF to list of images using pdfplumber\"\"\"\n",
" images = []\n",
" try:\n",
" with fitz.open(stream=pdf_content.getvalue(), filetype=\"pdf\") as doc:\n",
" for page_num, page in enumerate(doc):\n",
" # Get page pixmap\n",
" pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72))\n",
" # Convert to PIL Image\n",
" image = Image.frombytes(\"RGB\", [pix.width, pix.height], pix.samples)\n",
" with pdfplumber.open(pdf_content) as pdf:\n",
" for page_num, page in enumerate(pdf.pages):\n",
" # Convert page to image\n",
" img = page.to_image()\n",
" image = img.original\n",
" \n",
" # Resize if needed\n",
" if image.size[0] > max_size[0] or image.size[1] > max_size[1]:\n",
" image.thumbnail(max_size, Image.Resampling.LANCZOS)\n",
" \n",
" # Convert to bytes\n",
" img_byte_arr = io.BytesIO()\n",
" image.save(img_byte_arr, format='PNG', optimize=True, quality=quality)\n",
" img_byte_arr.seek(0)\n",
Expand All @@ -213,7 +212,7 @@
" except Exception as e:\n",
" print(f\"Error converting PDF to images: {e}\")\n",
" return None\n",
"\n",
" \n",
"def upload_images_to_s3(images, bucket_name, s3_prefix, pdf_name):\n",
" \"\"\"Upload images to S3\"\"\"\n",
" s3_client = boto3.client('s3')\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ sphinx==8.1.3
docutils>=0.20,<0.22
Events
fitz
PyMuPDF
frontend
mkl
pydub
pydub
pdfplumber
pillow
tools