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