Threscomma commited on
Commit
faddd0a
Β·
verified Β·
1 Parent(s): c18ad26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -27
app.py CHANGED
@@ -1,29 +1,77 @@
1
  import gradio as gr
2
- from docx import Document
3
- from PIL import Image, ImageDraw, ImageFont
4
- import io
5
-
6
- def convert_word_to_image(word_file):
7
- doc = Document(word_file)
8
- img = Image.new("RGB", (800, 600), color="white")
9
- draw = ImageDraw.Draw(img)
10
- font = ImageFont.load_default()
11
-
12
- y_position = 10
13
- for para in doc.paragraphs:
14
- draw.text((10, y_position), para.text, font=font, fill="black")
15
- y_position += 20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- img_bytes = io.BytesIO()
18
- img.save(img_bytes, format="PNG")
19
- return img_bytes.getvalue()
20
-
21
- iface = gr.Interface(
22
- fn=convert_word_to_image,
23
- inputs=gr.File(label="πŸ“„ Upload Word Document (DOCX)", file_types=[".docx"]),
24
- outputs=gr.Image(label="πŸ–ΌοΈ Converted Image"),
25
- title="Word to Image Converter (Simple)",
26
- description="easy image shares!"
27
- )
28
-
29
- iface.launch()
 
 
 
 
 
1
  import gradio as gr
2
+ import subprocess
3
+ import tempfile
4
+ import os
5
+ from pdf2image import convert_from_path
6
+ import logging
7
+
8
+ # Configure logging
9
+ logging.basicConfig(level=logging.INFO)
10
+ logger = logging.getLogger(__name__)
11
+
12
+ def convert_docx_to_image(docx_path: str) -> str:
13
+ """
14
+ Converts DOCX to image via LibreOffice and pdf2image.
15
+ Returns path to the generated image.
16
+ """
17
+ with tempfile.TemporaryDirectory() as tmpdir:
18
+ try:
19
+ # Step 1: DOCX β†’ PDF using LibreOffice
20
+ pdf_path = os.path.join(tmpdir, "output.pdf")
21
+ cmd = [
22
+ "libreoffice", "--headless", "--convert-to", "pdf",
23
+ "--outdir", tmpdir, docx_path
24
+ ]
25
+ subprocess.run(cmd, check=True, capture_output=True)
26
+
27
+ if not os.path.exists(pdf_path):
28
+ raise FileNotFoundError("PDF conversion failed")
29
+
30
+ # Step 2: PDF β†’ Image
31
+ images = convert_from_path(
32
+ pdf_path,
33
+ dpi=300, # High resolution
34
+ output_folder=tmpdir,
35
+ fmt="png",
36
+ single_file=True
37
+ )
38
+ output_path = os.path.join(tmpdir, "output.png")
39
+ images[0].save(output_path, "PNG")
40
+
41
+ return output_path
42
+
43
+ except subprocess.CalledProcessError as e:
44
+ logger.error(f"LibreOffice error: {e.stderr.decode()}")
45
+ raise
46
+ except Exception as e:
47
+ logger.error(f"Conversion failed: {str(e)}")
48
+ raise
49
+
50
+ # Gradio Interface
51
+ with gr.Blocks(title="DOCX to Image (Pixel-Perfect)") as app:
52
+ gr.Markdown("## πŸ“„β†’πŸ–ΌοΈ Convert Word to Image (Exact Visual Copy)")
53
+ with gr.Row():
54
+ input_file = gr.File(
55
+ label="Upload DOCX",
56
+ file_types=[".docx"],
57
+ type="filepath"
58
+ )
59
+ output_image = gr.Image(label="Rendered Image", type="filepath")
60
 
61
+ convert_btn = gr.Button("Convert", variant="primary")
62
+ status = gr.Textbox(label="Status", interactive=False)
63
+
64
+ def process(file_path: str) -> tuple[str, str]:
65
+ try:
66
+ image_path = convert_docx_to_image(file_path)
67
+ return image_path, "βœ… Conversion successful!"
68
+ except Exception as e:
69
+ return None, f"❌ Error: {str(e)}"
70
+
71
+ convert_btn.click(
72
+ fn=process,
73
+ inputs=input_file,
74
+ outputs=[output_image, status]
75
+ )
76
+
77
+ app.launch(server_name="0.0.0.0")