1

I have saved some images of my work in .pdf format using matplotlib, I know this is my fault from the beginning and I should save it directly as image but I did not know that I can not display pdf files on colab. To get these results I need another 10 days which is not good choice for me. Actually I have found this which express my problem precisely but there was not answer. It just seems strange to me that using matplotlib I can save pdf files but I can not load them using it again. I just need to display the pdf file in colab cell ,I have tried:

import subprocess
subprocess.Popen(['myfile.pdf'],shell=True)

and this was the result:

<subprocess.Popen at 0x7f4d6a395978>

another methods as in this page do not work for me

Arij Aladel
  • 356
  • 1
  • 3
  • 10

2 Answers2

2

Ok this works for me, maybe there are a simpler solution but for now this works

from pdf2image import convert_from_path
from IPython.display import display, Image

images = convert_from_path("myfile.pdf")
for i, image in enumerate(images):    
    fname = "image" + str(i) + ".png"
    image.save(fname, "PNG")
Image(fname, width=600, height=300)


Arij Aladel
  • 356
  • 1
  • 3
  • 10
2

In a Jupyter notebook / Colab you can simply

from pdf2image import convert_from_path

images = convert_from_path("myfile.pdf")
images[0]  # first page

The image will be able to render as the cell output. No need for IPython.display

Roy Shilkrot
  • 3,079
  • 29
  • 25