File size: 1,869 Bytes
a4d4413
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c1ec10
a4d4413
0c1ec10
a4d4413
847e893
a4d4413
 
f219a34
a4d4413
 
 
5d26f13
 
 
 
a4d4413
 
 
 
 
 
847e893
a4d4413
a41fdb9
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
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])
  #return np.array(imgs[0]), num_pages

title = "First page of a PDF >> image"
description = "Drop a PDF (WARNING: only the first page will be converted into an image)."
examples = [["example.pdf"]]
css = ".output-image, .input-image, .image-preview {height: 600px !important}"
allow_flagging = "never"
live = False

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 Spaces
                     #outputs=[gr.Image(type="numpy", label="page image"), gr.Textbox(label="number of pages")], # sdk_version: 3.0.2
                     outputs=gr.Image(type="numpy", label="image of the first page"), # sdk_version: 3.0.2 
                     title=title,
                     description=description,
                     examples=examples,
                     #article=article,
                     css=css,
                     allow_flagging=allow_flagging,
                     live=live
                     )
iface.launch(debug=True, enable_queue=True)