在 iPython Notebook 中查看 pdf 图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19470099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
View pdf image in an iPython Notebook
提问by Curious2learn
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.
以下代码允许我png
在 iPython 笔记本中查看图像。有没有办法查看pdf
图像?我不一定需要使用 IPython.display。我正在寻找一种将文件中的 pdf 图像打印到 iPython 笔记本输出单元格的方法。
## This is for an `png` image
from IPython.display import Image
fig = Image(filename=('./temp/my_plot.png'))
fig
Thank you.
谢谢你。
采纳答案by Jakob
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.
您(和其他人)面临的问题是 PDF 无法直接在浏览器中显示。获得类似内容的唯一可能方法是使用图像转换器从 PDF 创建 PNG 或 JPG 并显示这个。
这可以通过 imagemagick 和自定义显示功能来完成。
Update 1
更新 1
A simple solution is to use wand (http://docs.wand-py.org) a python-imagemagick binding. I tried with Ubuntu 13.04:
一个简单的解决方案是使用 wand ( http://docs.wand-py.org) 一个 python-imagemagick 绑定。我尝试使用 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:
对于多页pdf,您可以通过以下方式获得例如第二页:
img = WImage(filename='hat.pdf[1]')
Update 2
更新 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
由于最近的浏览器支持使用嵌入式 pdf 查看器显示 pdf,因此可以将基于 iframe 的可能替代解决方案实现为
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.0\textwidth]{{{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
这个类实现了 html 和 latex 表示,因此 pdf 也将在 nbconversion 到 latex 中幸存下来。它可以像
PDF('hat.pdf',size=(300,250))
With Firefox 33 this results in
使用 Firefox 33 这会导致
回答by Jeremy Ellis
Assuming a multi-image pdf called Rplots.pdf
假设一个名为 Rplots.pdf 的多图像 pdf
The following works in the jupyter notebook cell. For installation I used
以下在 jupyter notebook 单元中工作。对于安装我使用
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
回答by Levon
回答by Rave
In addition to Jakob's excellent answer recommending the Wand bindings for ImageMagick:
除了 Jakob 为 ImageMagick 推荐 Wand 绑定的优秀回答:
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.
如果您的 PDF 包含矢量图形,请使用resolution
关键字来控制渲染图像的大小。ImageMagick 的默认值为 72 dpi。更高的值会产生更多的像素。
from wand.image import Image as WImage
img = WImage(filename='hat.pdf', resolution=100) # bigger
img