File size: 3,650 Bytes
8956ec9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
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
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
86
87
88
89
90
91
92
93
import os
import sys
from PIL import Image, ImageOps, ImageChops
import io
import fitz  # PyMuPDF
from docx import Document
from rembg import remove
import gradio as gr

def create_output_folder(file_path):
    base_name = os.path.splitext(os.path.basename(file_path))[0]
    output_folder = os.path.join(os.path.dirname(file_path), base_name)
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    return output_folder

def convert_image_to_jpeg(input_path, output_folder):
    image = Image.open(input_path)
    image = image.convert('RGB')
    output_path = os.path.join(output_folder, f"{os.path.splitext(os.path.basename(input_path))[0]}.jpg")
    image.save(output_path, 'JPEG')
    return output_path

def trim_whitespace(image):
    gray_image = ImageOps.grayscale(image)
    inverted_image = ImageChops.invert(gray_image)
    bbox = inverted_image.getbbox()
    trimmed_image = image.crop(bbox)
    return trimmed_image

def convert_pdf_to_images(pdf_path, output_folder, zoom=2):
    pdf_document = fitz.open(pdf_path)
    name_with_extension = os.path.basename(pdf_path)
    name = os.path.splitext(name_with_extension)[0]
    
    output_paths = []
    for page_num in range(len(pdf_document)):
        page = pdf_document.load_page(page_num)
        matrix = fitz.Matrix(zoom, zoom)
        pix = page.get_pixmap(matrix=matrix)
        image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
        trimmed_image = trim_whitespace(image)
        output_path = os.path.join(output_folder, f"{name}_page_{page_num + 1}.jpg")
        trimmed_image.save(output_path, 'JPEG')
        output_paths.append(output_path)
    return output_paths

def convert_docx_to_jpeg(docx_path, output_folder):
    document = Document(docx_path)
    output_paths = []
    for i, image_shape in enumerate(document.inline_shapes, start=1):
        image_stream = image_shape.image.blob
        image = Image.open(io.BytesIO(image_stream))
        output_path = os.path.join(output_folder, f"{os.path.splitext(os.path.basename(docx_path))[0]}_page_{i}.jpg")
        image.save(output_path, 'JPEG')
        output_paths.append(output_path)
    return output_paths

def process_file(input_file):
    file_extension = os.path.splitext(input_file.name)[1].lower()
    output_folder = create_output_folder(input_file.name)

    if file_extension in ['.png', '.jpeg', '.jpg', '.bmp', '.gif']:
        output_path = convert_image_to_jpeg(input_file.name, output_folder)
        return remove_background(output_path)
    elif file_extension == '.pdf':
        image_paths = convert_pdf_to_images(input_file.name, output_folder)
        return [remove_background(path) for path in image_paths]
    elif file_extension in ['.docx', '.doc']:
        image_paths = convert_docx_to_jpeg(input_file.name, output_folder)
        return [remove_background(path) for path in image_paths]
    else:
        return "File format not supported."

def remove_background(image_path):
    input_image = Image.open(image_path)
    output_image = remove(input_image)
    output_path = image_path.replace('.jpg', '_no_bg.png')
    output_image.save(output_path, 'PNG')
    return output_path

def gradio_interface(input_file):
    return process_file(input_file)

iface = gr.Interface(
    fn=gradio_interface,
    inputs=gr.inputs.File(label="Upload Word, PDF, or Image"),
    outputs=gr.outputs.Image(type="file", label="Processed Image(s)"),
    title="Document to Image Converter with Background Removal"
)

if __name__ == "__main__":
    iface.launch()