cdcvd commited on
Commit
176abf3
1 Parent(s): d816ab7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -92
app.py CHANGED
@@ -1,93 +1,84 @@
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()
 
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()