lokesh341 commited on
Commit
06149b8
·
verified ·
1 Parent(s): c93881c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -52
app.py CHANGED
@@ -1,59 +1,56 @@
1
  import fitz # PyMuPDF
 
2
  from PIL import Image
3
- import pytesseract
4
  from fpdf import FPDF
5
  import gradio as gr
6
 
7
- # Step 1: Convert PDF Pages to Images
8
- def pdf_to_images(pdf_path):
9
- pdf_document = fitz.open(pdf_path)
10
- images = []
11
- for page_num in range(len(pdf_document)):
12
- page = pdf_document[page_num]
13
- pix = page.get_pixmap() # Render the page as an image
14
- image_path = f"page_{page_num + 1}.png"
15
- pix.save(image_path)
16
- images.append(image_path)
17
- return images
18
-
19
- # Step 2: Extract Text Using Tesseract OCR
20
- def extract_text(images):
21
- text_pages = []
22
- for image_path in images:
23
- text = pytesseract.image_to_string(Image.open(image_path)) # Perform OCR
24
- text_pages.append(text)
25
- return text_pages
26
-
27
- # Step 3: Replace Curved Text with Editable Text in a New PDF
28
- def create_editable_pdf(images, text_pages, output_pdf_path):
29
- pdf = FPDF()
30
- pdf.set_auto_page_break(auto=True, margin=15)
31
- for text in text_pages:
32
- pdf.add_page()
33
- pdf.set_font("Arial", size=12)
34
- pdf.multi_cell(0, 10, text)
35
- pdf.output(output_pdf_path)
36
-
37
- # Main Function
38
- def process_pdf(file):
39
- input_pdf_path = file.name
40
- output_pdf_path = "Editable_Output.pdf"
41
-
42
- # Convert PDF to images and perform OCR
43
- images = pdf_to_images(input_pdf_path)
44
- text_pages = extract_text(images)
45
-
46
- # Create a new editable PDF
47
- create_editable_pdf(images, text_pages, output_pdf_path)
48
- return output_pdf_path
49
-
50
- # Gradio Interface
51
- iface = gr.Interface(
52
- fn=process_pdf,
53
- inputs=gr.File(label="Upload PDF"),
54
- outputs=gr.File(label="Download Editable PDF"),
55
- title="OCR PDF to Editable Text",
56
- description="Upload a PDF to extract and replace text while preserving shapes and layout.",
57
  )
58
 
59
- iface.launch()
 
 
 
1
  import fitz # PyMuPDF
2
+ from pytesseract import image_to_string
3
  from PIL import Image
 
4
  from fpdf import FPDF
5
  import gradio as gr
6
 
7
+ # Function to process PDF
8
+ def process_pdf(pdf_file):
9
+ # Open the PDF
10
+ pdf_document = fitz.open(stream=pdf_file.read(), filetype="pdf")
11
+ processed_pages = []
12
+
13
+ for page_number in range(len(pdf_document)):
14
+ # Extract page as an image
15
+ page = pdf_document.load_page(page_number)
16
+ pix = page.get_pixmap()
17
+ img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
18
+
19
+ # Perform OCR on the image
20
+ text = image_to_string(img)
21
+
22
+ # Save the processed text back to a new PDF with shapes preserved
23
+ processed_pages.append({"image": img, "text": text})
24
+
25
+ # Create a new PDF with editable text
26
+ pdf_output = FPDF()
27
+ pdf_output.set_auto_page_break(auto=True, margin=15)
28
+
29
+ for page_data in processed_pages:
30
+ pdf_output.add_page()
31
+ pdf_output.set_font("Arial", size=12)
32
+ pdf_output.multi_cell(0, 10, page_data["text"])
33
+
34
+ # Save the output PDF
35
+ output_file = "processed_output.pdf"
36
+ pdf_output.output(output_file)
37
+
38
+ return output_file
39
+
40
+ # Gradio interface
41
+ def process_pdf_interface(pdf_file):
42
+ output_file = process_pdf(pdf_file)
43
+ return output_file
44
+
45
+ # Create Gradio App
46
+ interface = gr.Interface(
47
+ fn=process_pdf_interface,
48
+ inputs=gr.inputs.File(label="Upload your PDF"),
49
+ outputs=gr.outputs.File(label="Download Processed PDF"),
50
+ title="PDF Text Processing App",
51
+ description="Upload a PDF to process its text, replace curved text with editable text, and download the updated PDF."
 
 
 
 
 
52
  )
53
 
54
+ # Launch Gradio app
55
+ if __name__ == "__main__":
56
+ interface.launch()