简体   繁体   English

如何在shell中为graphicsmagick编写循环?

[英]How to write a loop in shell for graphicsmagick?

I managed (with the help of SO) to make perfect png-snippets from a pdf file with graphicsmagick . 我(在SO的帮助下)管理了一个带有graphicsmagick的pdf文件的完美png-snippets。 My pdf contains text and formula each "snippet" on a single page. 我的pdf在单个页面上包含每个“片段”的文本和公式。 My command trims the content of a page to the very content and finally scales this up to 2000 pixel width. 我的命令将页面内容修剪为内容,最后将其缩放到2000像素宽度。

Untill now, I need to repeat that command for each single page in every pdf. 直到现在,我需要为每个pdf中的每个页面重复该命令。 I am wondering how to automate this. 我想知道如何自动化这个。 I think I could try a loop for the repetition of the command for every page i untill the last page. 我想我可以尝试循环重复每个页面的命令,直到最后一页。

Assume file1.pdf is in my current working directory. 假设file1.pdf在我当前的工作目录中。

gm convert -density 300x300 file1.pdf[0] -trim -resize 2000x file1_page1.png
gm convert -density 300x300 file1.pdf[1] -trim -resize 2000x file1_page2.png
gm convert -density 300x300 file1.pdf[2] -trim -resize 2000x file1_page3.png
...

How can I set a counter and run a loop for every page in my document? 如何设置计数器并为文档中的每个页面运行循环?

You are in luck. 你很幸运。 GraphicsMagick knows how to do that for you: GraphicsMagick知道如何为您做到这一点:

gm convert -density 300x300 input.pdf -trim -resize 2000x +adjoin output-%d.png

If you are ok using ImageMagick instead, you can set the starting output file number to 1 instead of 0 and don't need the -adjoin : 如果您可以使用ImageMagick ,则可以将起始输出文件编号设置为1而不是0并且不需要-adjoin

convert -density 300x300 input.pdf -scene 1 -trim -resize 2000x output-%d.png

Or, if you want them all done in parallel, use GNU Parallel : 或者,如果您希望它们全部并行完成,请使用GNU Parallel

parallel gm convert -density 300x300 {} -trim -resize 2000x output-{#}.png  ::: $(identify input.pdf | awk '{print $1}')
for file in *.pdf
do
    pages=$(identify "$file" | wc -l)
    for (( i=0; i<$pages; i++ ))
    do
        name=$(sed "s/\.pdf$/$i.png/g" <<< "$file");
        gm convert -density 300x300 "$file[$i]" -trim -resize 2000x "$name"
    done
done

Try this one. 试试这个吧。 It will convert every page in every *.pdf file to .png. 它会将每个* .pdf文件中的每个页面转换为.png。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM