View pdf image in an iPython Notebook
Question:
The following code allows me to view a png
image in an iPython notebook. Is there a way to view pdf
image? I don’t need to use IPython.display necessarily. I am looking for a way to print a pdf image in a file to the iPython notebook output cell.
## This is for an `png` image
from IPython.display import Image
fig = Image(filename=('./temp/my_plot.png'))
fig
Thank you.
Answers:
The problem you (and others) face is that PDFs cannot be displayed directly in the browser.
The only possible way to get something similar is to use an image-converter to create a PNG or JPG out of the PDF and display this one.
This could be done via imagemagick and a custom display function.
Update 1
A simple solution is to use wand (http://docs.wand-py.org) a python-imagemagick binding. I tried with Ubuntu 13.04:
In text form:
from wand.image import Image as WImage
img = WImage(filename='hat.pdf')
img
For a multi-page pdf, you can get e.g. the second page via:
img = WImage(filename='hat.pdf[1]')
Update 2
As recent browsers support to display pdfs with their embedded pdf viewer a possible alternative solution based on an iframe can be implemented as
class PDF(object):
def __init__(self, pdf, size=(200,200)):
self.pdf = pdf
self.size = size
def _repr_html_(self):
return '<iframe src={0} width={1[0]} height={1[1]}></iframe>'.format(self.pdf, self.size)
def _repr_latex_(self):
return r'includegraphics[width=1.0textwidth]{{{0}}}'.format(self.pdf)
This class implements html and latex representations, hence the pdf will also survive a nbconversion to latex. It can be used like
PDF('hat.pdf',size=(300,250))
With Firefox 33 this results in
Assuming a multi-image pdf called Rplots.pdf
The following works in the jupyter notebook cell. For installation I used
pip install Wand
This code pastes into a cell
from wand.image import Image
imageFromPdf = Image(filename='Rplots.pdf')
pages = len(imageFromPdf.sequence)
image = Image(
width=imageFromPdf.width,
height=imageFromPdf.height * pages
)
for i in range(pages):
image.composite(
imageFromPdf.sequence[i],
top=imageFromPdf.height * i,
left=0
)
image.format="png"
image
In addition to Jakob’s excellent answer recommending the Wand bindings for ImageMagick:
If your PDF contains vector graphics, use the resolution
keyword to control the size of the rendered image. ImageMagick’s default value is 72 dpi. Higher values yield more pixels.
from wand.image import Image as WImage
img = WImage(filename='hat.pdf', resolution=100) # bigger
img
The following code allows me to view a png
image in an iPython notebook. Is there a way to view pdf
image? I don’t need to use IPython.display necessarily. I am looking for a way to print a pdf image in a file to the iPython notebook output cell.
## This is for an `png` image
from IPython.display import Image
fig = Image(filename=('./temp/my_plot.png'))
fig
Thank you.
The problem you (and others) face is that PDFs cannot be displayed directly in the browser.
The only possible way to get something similar is to use an image-converter to create a PNG or JPG out of the PDF and display this one.
This could be done via imagemagick and a custom display function.
Update 1
A simple solution is to use wand (http://docs.wand-py.org) a python-imagemagick binding. I tried with Ubuntu 13.04:
In text form:
from wand.image import Image as WImage
img = WImage(filename='hat.pdf')
img
For a multi-page pdf, you can get e.g. the second page via:
img = WImage(filename='hat.pdf[1]')
Update 2
As recent browsers support to display pdfs with their embedded pdf viewer a possible alternative solution based on an iframe can be implemented as
class PDF(object):
def __init__(self, pdf, size=(200,200)):
self.pdf = pdf
self.size = size
def _repr_html_(self):
return '<iframe src={0} width={1[0]} height={1[1]}></iframe>'.format(self.pdf, self.size)
def _repr_latex_(self):
return r'includegraphics[width=1.0textwidth]{{{0}}}'.format(self.pdf)
This class implements html and latex representations, hence the pdf will also survive a nbconversion to latex. It can be used like
PDF('hat.pdf',size=(300,250))
With Firefox 33 this results in
Assuming a multi-image pdf called Rplots.pdf
The following works in the jupyter notebook cell. For installation I used
pip install Wand
This code pastes into a cell
from wand.image import Image
imageFromPdf = Image(filename='Rplots.pdf')
pages = len(imageFromPdf.sequence)
image = Image(
width=imageFromPdf.width,
height=imageFromPdf.height * pages
)
for i in range(pages):
image.composite(
imageFromPdf.sequence[i],
top=imageFromPdf.height * i,
left=0
)
image.format="png"
image
In addition to Jakob’s excellent answer recommending the Wand bindings for ImageMagick:
If your PDF contains vector graphics, use the resolution
keyword to control the size of the rendered image. ImageMagick’s default value is 72 dpi. Higher values yield more pixels.
from wand.image import Image as WImage
img = WImage(filename='hat.pdf', resolution=100) # bigger
img