File size: 2,146 Bytes
a4d4413
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3019c8e
 
a4d4413
b64f050
 
a4d4413
 
 
 
 
 
 
 
4c4efd2
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
import gradio as gr
import pdf2image
import numpy as np

# Convert a PDF to images
def pdf_to_imgs(pdf):
  """
  pdf: pdf file
  first_page: convert to image only the first page
  return numpy array of the first page and number of images
  """

  # get path to pdf
  path_to_pdf = pdf.name
  
  # convert PDF to PIL images (one image by page)
  first_page=True # we want here only the first page as image
  if first_page: last_page = 1
  else: last_page = None

  imgs = pdf2image.convert_from_path(path_to_pdf, last_page=last_page)
  #num_pages = len(imgs)

  #return np.array(imgs[0]), num_pages
  return np.array(imgs[0])

title = "PDF to images"
description = "Drop a PDF (WARNING: only the first page will be converted into an image)."
examples =[["exemple.pdf"]]
css = ".output-image, .input-image, .image-preview {height: 600px !important}"
allow_flagging = "never"
theme = "peach" # default, huggingface, grass, peach

iface = gr.Interface(fn=pdf_to_imgs, 
                     #inputs=[gr.File(label="PDF"), gr.Checkbox(label="Only first page?", value=True)],
                     #inputs=gr.File(label="PDF"), # sdk_version: 3.0.2
                     inputs=gr.inputs.File(type="file", label="PDF"), # sdk_version: 2.9.4 needed to use gr.Interface.load("spaces/pierreguillou/pdf2imgs") in other Speces
                     #outputs=[gr.Image(type="numpy", label="page image"), gr.Textbox(label="number of pages")],
                     #outputs=gr.outputs.Image(type="numpy", label="image of the first page"), # sdk_version: 2.9.4 needed to use gr.Interface.load("spaces/pierreguillou/pdf2imgs") in other Speces
                     outputs=gr.outputs.Image(label="image of the first page"), # sdk_version: 2.9.4 needed to use gr.Interface.load("spaces/pierreguillou/pdf2imgs") in other Speces
                     title=title,
                     description=description,
                     examples=examples,
                     #article=article,
                     css=css,
                     allow_flagging=allow_flagging,
                     theme=theme
                     )
iface.launch(debug=False, enable_queue=True)