Update app.py
Browse files
app.py
CHANGED
@@ -1,93 +1,84 @@
|
|
1 |
-
import os
|
2 |
-
import
|
3 |
-
|
4 |
-
import
|
5 |
-
import
|
6 |
-
from
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
file_extension
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
iface = gr.Interface(
|
86 |
-
fn=gradio_interface,
|
87 |
-
inputs=gr.inputs.File(label="Upload Word, PDF, or Image"),
|
88 |
-
outputs=gr.outputs.Image(type="file", label="Processed Image(s)"),
|
89 |
-
title="Document to Image Converter with Background Removal"
|
90 |
-
)
|
91 |
-
|
92 |
-
if __name__ == "__main__":
|
93 |
iface.launch()
|
|
|
1 |
+
import os
|
2 |
+
from PIL import Image, ImageOps, ImageChops
|
3 |
+
import io
|
4 |
+
import fitz # PyMuPDF
|
5 |
+
from docx import Document
|
6 |
+
from rembg import remove
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
def convert_image_to_jpeg(input_path):
|
10 |
+
image = Image.open(input_path)
|
11 |
+
image = image.convert('RGB')
|
12 |
+
output_path = os.path.splitext(input_path)[0] + ".jpg"
|
13 |
+
image.save(output_path, 'JPEG')
|
14 |
+
return output_path
|
15 |
+
|
16 |
+
def trim_whitespace(image):
|
17 |
+
gray_image = ImageOps.grayscale(image)
|
18 |
+
inverted_image = ImageChops.invert(gray_image)
|
19 |
+
bbox = inverted_image.getbbox()
|
20 |
+
trimmed_image = image.crop(bbox)
|
21 |
+
return trimmed_image
|
22 |
+
|
23 |
+
def convert_pdf_to_images(pdf_path, zoom=2):
|
24 |
+
pdf_document = fitz.open(pdf_path)
|
25 |
+
name_with_extension = os.path.basename(pdf_path)
|
26 |
+
name = os.path.splitext(name_with_extension)[0]
|
27 |
+
|
28 |
+
output_paths = []
|
29 |
+
for page_num in range(len(pdf_document)):
|
30 |
+
page = pdf_document.load_page(page_num)
|
31 |
+
matrix = fitz.Matrix(zoom, zoom)
|
32 |
+
pix = page.get_pixmap(matrix=matrix)
|
33 |
+
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
34 |
+
trimmed_image = trim_whitespace(image)
|
35 |
+
output_path = f"{name}_page_{page_num + 1}.jpg"
|
36 |
+
trimmed_image.save(output_path, 'JPEG')
|
37 |
+
output_paths.append(output_path)
|
38 |
+
return output_paths
|
39 |
+
|
40 |
+
def convert_docx_to_jpeg(docx_path):
|
41 |
+
document = Document(docx_path)
|
42 |
+
output_paths = []
|
43 |
+
for i, image_shape in enumerate(document.inline_shapes, start=1):
|
44 |
+
image_stream = image_shape.image.blob
|
45 |
+
image = Image.open(io.BytesIO(image_stream))
|
46 |
+
output_path = f"{os.path.splitext(os.path.basename(docx_path))[0]}_page_{i}.jpg"
|
47 |
+
image.save(output_path, 'JPEG')
|
48 |
+
output_paths.append(output_path)
|
49 |
+
return output_paths
|
50 |
+
|
51 |
+
def process_file(input_file):
|
52 |
+
file_extension = os.path.splitext(input_file.name)[1].lower()
|
53 |
+
|
54 |
+
if file_extension in ['.png', '.jpeg', '.jpg', '.bmp', '.gif']:
|
55 |
+
output_path = convert_image_to_jpeg(input_file.name)
|
56 |
+
return remove_background(output_path)
|
57 |
+
elif file_extension == '.pdf':
|
58 |
+
image_paths = convert_pdf_to_images(input_file.name)
|
59 |
+
return [remove_background(path) for path in image_paths]
|
60 |
+
elif file_extension in ['.docx', '.doc']:
|
61 |
+
image_paths = convert_docx_to_jpeg(input_file.name)
|
62 |
+
return [remove_background(path) for path in image_paths]
|
63 |
+
else:
|
64 |
+
return "File format not supported."
|
65 |
+
|
66 |
+
def remove_background(image_path):
|
67 |
+
input_image = Image.open(image_path)
|
68 |
+
output_image = remove(input_image)
|
69 |
+
output_path = image_path.replace('.jpg', '_no_bg.png')
|
70 |
+
output_image.save(output_path, 'PNG')
|
71 |
+
return output_path
|
72 |
+
|
73 |
+
def gradio_interface(input_file):
|
74 |
+
return process_file(input_file)
|
75 |
+
|
76 |
+
iface = gr.Interface(
|
77 |
+
fn=gradio_interface,
|
78 |
+
inputs=gr.inputs.File(label="Upload Word, PDF, or Image"),
|
79 |
+
outputs=gr.outputs.Image(type="file", label="Processed Image(s)"),
|
80 |
+
title="Document to Image Converter with Background Removal"
|
81 |
+
)
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
iface.launch()
|