如何快速生成PDF所有页面的PNG缩略图?

发布于 2024-10-20 22:59:20 字数 201 浏览 0 评论 0原文

我使用以下代码来执行此操作:

for i in `seq 1 $numPages`; do
    convert "$INPUTPDF"[$((i-1))] thumb_$i.png
done;

它有点慢,我认为这是因为它每次都会启动一个新进程。

如何更快地做到这一点?

预先非常感谢!

I use a following code to do this:

for i in `seq 1 $numPages`; do
    convert "$INPUTPDF"[$((i-1))] thumb_$i.png
done;

It's kinda slow, and I think it's because it starts a new process every time.

How to do this faster?

Thanks very much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯叶蝶 2024-10-27 22:59:20

以下是无耻地窃取

mikal@deathstar:~/foo$ convert foo.pdf pages-%03d.png
mikal@deathstar:~/foo$ ls pages*
pages-000.png  pages-001.png  pages-002.png  pages-003.png  pages-004.png
mikal@deathstar:~/foo$

这将解析您的pdf 只一次。呜呜。

根据此过程所需的时间,同时运行其中几个可能是有意义的。如果您拥有多核或 SMP 计算机,您可能会发现同时运行两个、三个甚至四个 convert(1) 进程的好处。简单的方法是将这样的命令放入两个或多个 shell 脚本中,然后并行运行这些 shell 脚本。您可以像这样生成这些脚本:

for f in *.pdf ; do echo "convert $f `basename $f .pdf`-%03d.png" >> /tmp/runme ; done
wc -l /tmp/runme
split -l [number of lines/2] /tmp/runme
sh /tmp/xaa & sh /tmp/xab &

不过,这已经是相当糟糕的了。如果您要经常这样做,我建议为 make(1) 编写一个 Makefile 或您自己的小型进程管理器工具,以利用多个CPU。哎呀,也许类似的东西已经存在了。 :)

The following was shamelessly stolen:

mikal@deathstar:~/foo$ convert foo.pdf pages-%03d.png
mikal@deathstar:~/foo$ ls pages*
pages-000.png  pages-001.png  pages-002.png  pages-003.png  pages-004.png
mikal@deathstar:~/foo$

This will parse your pdf only once. Woot.

Depending upon how long this process takes, it might make sense to run several of these simultaneously. If you've got a multi-core or SMP machine, you might see benefit to running two, three, or maybe even four convert(1) processes at once. The easy way would be to place commands like this into two or more shell scripts, and then run the shell scripts in parallel. You could generate these scripts like this:

for f in *.pdf ; do echo "convert $f `basename $f .pdf`-%03d.png" >> /tmp/runme ; done
wc -l /tmp/runme
split -l [number of lines/2] /tmp/runme
sh /tmp/xaa & sh /tmp/xab &

This is pretty hacked-together though. If you're going to be doing this often, I'd suggest writing a Makefile for make(1) or your own little process-manager tool to take advantage of multiple CPUs. Heck, maybe something like that already exists. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文