not-lain commited on
Commit
bb2ddfa
·
1 Parent(s): df1c5ff

fix file to image implementation

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -1,6 +1,10 @@
1
  import gradio as gr
2
  import os
3
  from pdfitdown.pdfconversion import Converter
 
 
 
 
4
 
5
  converter = Converter()
6
 
@@ -19,7 +23,7 @@ def convert_file_to_pdf(filename:str) -> str:
19
  converter.convert(filename.name, output_path)
20
  return output_path
21
 
22
- def convert_file_to_img(image_file) -> str:
23
  """
24
  Convert an image file to PDF format.
25
 
@@ -32,9 +36,15 @@ def convert_file_to_img(image_file) -> str:
32
  str: The file path of the generated PDF file. The output filename will be
33
  the same as the input filename but with a .pdf extension.
34
  """
35
- output_path = image_file.name.replace(os.path.splitext(image_file.name)[1], '.pdf')
 
36
  converter.convert(image_file.name, output_path)
37
- return output_path
 
 
 
 
 
38
 
39
 
40
 
@@ -50,9 +60,9 @@ file_to_pdf = gr.Interface(
50
  image_to_pdf = gr.Interface(
51
  fn=convert_file_to_img,
52
  inputs=gr.File(label="Upload Image", file_types=["image"]),
53
- outputs=gr.File(label="Converted PDF"),
54
- title="Image to PDF Converter",
55
- description="Convert your images to PDF format"
56
  )
57
 
58
  # Create tabbed interface
 
1
  import gradio as gr
2
  import os
3
  from pdfitdown.pdfconversion import Converter
4
+ import fitz
5
+ from typing import List
6
+ from PIL import Image
7
+ from loadimg import load_img
8
 
9
  converter = Converter()
10
 
 
23
  converter.convert(filename.name, output_path)
24
  return output_path
25
 
26
+ def convert_file_to_img(image_file:str) -> List[Image.Image] :
27
  """
28
  Convert an image file to PDF format.
29
 
 
36
  str: The file path of the generated PDF file. The output filename will be
37
  the same as the input filename but with a .pdf extension.
38
  """
39
+ img_list = []
40
+ output_path = image_file.name.rsplit('.', 1)[0] + '.pdf'
41
  converter.convert(image_file.name, output_path)
42
+ doc = fitz.open(output_path)
43
+ for page in doc:
44
+ img_list.append(load_img(page.get_pixmap()).convert("RGB"))
45
+ doc.close()
46
+ return img_list
47
+
48
 
49
 
50
 
 
60
  image_to_pdf = gr.Interface(
61
  fn=convert_file_to_img,
62
  inputs=gr.File(label="Upload Image", file_types=["image"]),
63
+ outputs=gr.Gallery(label="Converted Images"),
64
+ title="File to Images Converter",
65
+ description="Convert your images to an image format"
66
  )
67
 
68
  # Create tabbed interface