Threscomma commited on
Commit
c18ad26
·
verified ·
1 Parent(s): 8634c01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -12
app.py CHANGED
@@ -2,13 +2,9 @@ import gradio as gr
2
  from docx import Document
3
  from PIL import Image, ImageDraw, ImageFont
4
  import io
5
- import tempfile
6
 
7
  def convert_word_to_image(word_file):
8
- # Load the Word document
9
  doc = Document(word_file)
10
-
11
- # Create a blank image with text (simplified example)
12
  img = Image.new("RGB", (800, 600), color="white")
13
  draw = ImageDraw.Draw(img)
14
  font = ImageFont.load_default()
@@ -18,19 +14,16 @@ def convert_word_to_image(word_file):
18
  draw.text((10, y_position), para.text, font=font, fill="black")
19
  y_position += 20
20
 
21
- # Save the image to a temporary file
22
- temp_img = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
23
- img.save(temp_img.name)
24
-
25
- return temp_img.name
26
 
27
- # Gradio Interface
28
  iface = gr.Interface(
29
  fn=convert_word_to_image,
30
- inputs=gr.File(label="📄 Upload Word Document (DOCX)"),
31
  outputs=gr.Image(label="🖼️ Converted Image"),
32
  title="Word to Image Converter (Simple)",
33
- description="Converts text from DOCX to an image. Works on Hugging Face!"
34
  )
35
 
36
  iface.launch()
 
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()
 
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()