在 Windows 上获取 PDF 的预览 JPEG?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/502/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:04:15  来源:igfitidea点击:

Get a preview JPEG of a PDF on Windows?

pythonwindowsimagepdf

提问by Gareth Simpson

I have a cross-platform (Python) application which needs to generate a JPEG preview of the first page of a PDF.

我有一个跨平台 (Python) 应用程序,它需要生成 PDF 第一页的 JPEG 预览。

On the Mac I am spawning sips. Is there something similarly simple I can do on Windows?

在 Mac 上,我正在生成sips。我可以在 Windows 上做一些同样简单的事情吗?

采纳答案by Chris Jester-Young

ImageMagick delegates the PDF->bitmap conversion to GhostScript anyway, so here's a command you can use (it's based on the actual command listed by the ps:alphadelegate in ImageMagick, just adjusted to use JPEG as output):

无论如何,ImageMagick 将 PDF-> 位图转换委托给 GhostScript,所以这里有一个您可以使用的命令(它基于ps:alpha委托在 ImageMagick 中列出的实际命令,只是调整为使用 JPEG 作为输出):

gs -q -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT \
-dMaxBitmap=500000000 -dLastPage=1 -dAlignToPixels=0 -dGridFitTT=0 \
-sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r72x72 \
-sOutputFile=$OUTPUT -f$INPUT

where $OUTPUTand $INPUTare the output and input filenames. Adjust the 72x72to whatever resolution you need. (Obviously, strip out the backslashes if you're writing out the whole command as one line.)

其中$OUTPUT$INPUT是输出和输入文件名。将其调整为72x72您需要的任何分辨率。(显然,如果您将整个命令写成一行,请去掉反斜杠。)

This is good for two reasons:

这有两个好处:

  1. You don't need to have ImageMagick installed anymore. Not that I have anything against ImageMagick (I love it to bits), but I believe in simple solutions.
  2. ImageMagick does a two-step conversion. First PDF->PPM, then PPM->JPEG. This way, the conversion is one-step.
  1. 您不再需要安装 ImageMagick。并不是说我反对 ImageMagick(我非常喜欢它),但我相信简单的解决方案。
  2. ImageMagick 进行两步转换。首先是 PDF->PPM,然后是 PPM->JPEG。这样,转换是一步。

Other things to consider: with the files I've tested, PNG compresses better than JPEG. If you want to use PNG, change the -sDEVICE=jpegto -sDEVICE=png16m.

其他需要考虑的事情:对于我测试过的文件,PNG 比 JPEG 压缩得更好。如果你想使用PNG,改-sDEVICE=jpeg-sDEVICE=png16m

回答by Federico Builes

You can use ImageMagick's convert utility for this, see some examples in http://studio.imagemagick.org/pipermail/magick-users/2002-May/002636.html:

您可以为此使用 ImageMagick 的转换实用程序,请参阅http://studio.imagemagick.org/pipermail/magick-users/2002-May/002636.html 中的一些示例 :

Convert taxes.pdf taxes.jpg 

Will convert a two page PDF file into [2] jpeg files: taxes.jpg.0, taxes.jpg.1

I can also convert these JPEGS to a thumbnail as follows:

convert -size 120x120 taxes.jpg.0 -geometry 120x120 +profile '*' thumbnail.jpg

I can even convert the PDF directly to a jpeg thumbnail as follows:

convert -size 120x120 taxes.pdf -geometry 120x120 +profile '*' thumbnail.jpg

This will result in a thumbnail.jpg.0 and thumbnail.jpg.1 for the two pages.

Convert taxes.pdf taxes.jpg 

将两页 PDF 文件转换为 [2] jpeg 文件:tax.jpg.0、tax.jpg.1

我还可以将这些 JPEGS 转换为缩略图,如下所示:

convert -size 120x120 taxes.jpg.0 -geometry 120x120 +profile '*' thumbnail.jpg

我什至可以将 PDF 直接转换为 jpeg 缩略图,如下所示:

convert -size 120x120 taxes.pdf -geometry 120x120 +profile '*' thumbnail.jpg

这将导致两个页面的缩略图.jpg.0 和缩略图.jpg.1。

回答by Dominic Cooney

Is the PC likely to have Acrobat installed? I think Acrobat installs a shell extension so previews of the first page of a PDF document appear in Windows Explorer's thumbnail view. You can get thumbnails yourself via the IExtractImage COM API, which you'll need to wrap. VBAccelerator has an example in C#that you could port to Python.

PC 是否可能安装了 Acrobat?我认为 Acrobat 安装了一个外壳扩展,因此 PDF 文档第一页的预览会出现在 Windows 资源管理器的缩略图视图中。您可以通过 IExtractImage COM API 自己获取缩略图,您需要对其进行包装。VBAccelerator 有一个 C# 示例,您可以将其移植到 Python。