Vihang28 commited on
Commit
b0a8ef0
·
verified ·
1 Parent(s): b9e29a5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import fitz
2
+ import io
3
+ import base64
4
+ from PIL import Image
5
+ import gradio as gr
6
+ import cv2
7
+ import tempfile
8
+ import os
9
+
10
+ def pdf_to_img(pdf_path):
11
+ pdf_document = fitz.open(pdf_path)
12
+ counter = 1
13
+ img_list = []
14
+ for page_number in range(len(pdf_document)):
15
+ page = pdf_document[page_number]
16
+ image_list = page.get_images()
17
+ for image in image_list:
18
+ base_img = pdf_document.extract_image(image[0])
19
+ image_data = base_img["image"]
20
+ img = Image.open(io.BytesIO(image_data))
21
+ extention = base_img['ext']
22
+ img.save(open(f"image{counter}.{extention}","wb"))
23
+ img_list.append(f"image{counter}.{extention}")
24
+ counter += 1
25
+ return (img_list)
26
+
27
+ title = "Extract Image and Text"
28
+
29
+
30
+ with gr.Blocks(theme=gr.themes.Glass(primary_hue=gr.themes.colors.slate)) as demo:
31
+ gr.Markdown(f'<h1 style="text-align: center;">{title}</h1>')
32
+ with gr.Row():
33
+ with gr.Row():
34
+ with gr.Column():
35
+ file_input = gr.File(type="filepath", label="Upload .pdf file")
36
+ upload_button = gr.Button(value="Show Images")
37
+ img_gallery = gr.Gallery(label="Generated images", show_label=True, elem_id="gallery", object_fit="contain", height="auto",allow_preview=True)
38
+
39
+ upload_button.click(pdf_to_img, inputs=file_input, outputs=[img_gallery])
40
+ demo.launch()