在 Linux 中将多个 jpg 合并为单个 pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13618236/
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
Merge multiple jpg into single pdf in Linux
提问by Harikrishnan
I used the following command to convert and merge all the jpg
files in a directory to a single pdf file.
我使用以下命令将jpg
目录中的所有文件转换并合并为单个 pdf 文件。
convert *.jpg file.pdf
The files in the directory are numbered from 1.jpg
to 123.jpg
. The convertion went fine but after converting the pages were all mixed up. I wanted the pdf to have pages from 1.jpg
to 123.jpg
in the same order as they are named. I tried with the following command as well:
目录中的文件编号从1.jpg
到123.jpg
。转换进行得很顺利,但转换后的页面都混在一起了。我想要的PDF有从页面1.jpg
到123.jpg
以相同的顺序,因为它们命名。我也尝试使用以下命令:
cd 1
FILES=$( find . -type f -name "*jpg" | cut -d/ -f 2)
mkdir temp && cd temp
for file in $FILES; do
BASE=$(echo $file | sed 's/.jpg//g');
convert ../$BASE.jpg $BASE.pdf;
done &&
pdftk *pdf cat output ../1.pdf &&
cd ..
rm -rf temp
But still no luck. Operating platform Linux.
但仍然没有运气。运行平台Linux。
采纳答案by Delan Azabani
The problem is because your shell is expanding the wildcard in a purely alphabetical order, and because the lengths of the numbers are different, the order will be incorrect:
问题是因为您的 shell 以纯字母顺序扩展通配符,并且由于数字的长度不同,顺序将不正确:
$ echo *.jpg
1.jpg 10.jpg 100.jpg 101.jpg 102.jpg ...
The solution is to pad the filenames with zeros as required so they're the same length before running your convert command:
解决方案是根据需要用零填充文件名,以便在运行转换命令之前它们的长度相同:
$ for i in *.jpg; do num=`expr match "$i" '\([0-9]\+\).*'`;
> padded=`printf "%03d" $num`; mv -v "$i" "${i/$num/$padded}"; done
Now the files will be matched by the wildcard in the correct order, ready for the convert command:
现在文件将以正确的顺序由通配符匹配,为转换命令做好准备:
$ echo *.jpg
001.jpg 002.jpg 003.jpg 004.jpg 005.jpg 006.jpg 007.jpg 008.jpg ...
回答by Felix Defrance
Or just read the ls
manual and see :
或者只是阅读ls
手册并查看:
-v natural sort of (version) numbers within text
-v 文本中自然排序的(版本)数字
So, doing what we need in single command.
因此,在单个命令中执行我们需要的操作。
convert `ls -v *.jpg` foobar.pdf
Have fun ;) F.
玩得开心;) F.
回答by Martian
This is how I do it:
First line convert all jpg files to pdf it is using convert command.
Second line is merging all pdf files to one single as pdf per page. This is using gs ((PostScript and PDF language interpreter and previewer))
我是这样做的:
第一行将所有 jpg 文件转换为 pdf,它使用 convert 命令。
第二行将所有 pdf 文件合并为一个单独的 pdf 每页。这是使用 gs((PostScript 和 PDF 语言解释器和预览器))
for i in $(find . -maxdepth 1 -name "*.jpg" -print); do convert $i ${i//jpg/pdf}; done
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=merged_file.pdf -dBATCH `find . -maxdepth 1 -name "*.pdf" -print"`
回答by Juan Lagos
Mixing first idea with their reply, I think this code maybe satisfactory
将第一个想法与他们的回复混合在一起,我认为这段代码可能令人满意
jpgs2pdf.sh
#!/bin/bash
cd
FILES=$( find . -type f -name "*jpg" | cut -d/ -f 2)
mkdir temp > /dev/null
cd temp
for file in $FILES; do
BASE=$(echo $file | sed 's/.jpg//g');
convert ../$BASE.jpg $BASE.pdf;
done &&
pdftk `ls -v *pdf` cat output ../`basename `.pdf
cd ..
rm -rf temp
回答by Lukas
You could use
你可以用
convert '%d.jpg[1-132]' file.pdf
via https://www.imagemagick.org/script/command-line-processing.php:
通过https://www.imagemagick.org/script/command-line-processing.php:
Another method of referring to other image files is by embedding a formatting character in the filename with a scene range. Consider the filename
image-%d.jpg[1-5]
. The command
magick image-%d.jpg[1-5]
causes ImageMagick to attempt to read images with these filenames:
image-1.jpg image-2.jpg image-3.jpg image-4.jpg image-5.jpg
引用其他图像文件的另一种方法是在具有场景范围的文件名中嵌入格式化字符。考虑文件名
image-%d.jpg[1-5]
。命令
magick image-%d.jpg[1-5]
导致 ImageMagick 尝试读取具有以下文件名的图像:
image-1.jpg image-2.jpg image-3.jpg image-4.jpg image-5.jpg
回答by Gregor Sturm
All of the above answers failed for me, when I wanted to merge many high-resolution jpeg images (from a scanned book).
当我想合并许多高分辨率 jpeg 图像(来自扫描的书)时,上述所有答案对我来说都失败了。
Imagemagick tried to load all files into RAM, I therefore used the following two-step approach:
Imagemagick 尝试将所有文件加载到 RAM 中,因此我使用了以下两步方法:
find -iname "*.JPG" | xargs -I'{}' convert {} {}.pdf
pdfunite *.pdf merged_file.pdf
Note that with this approach, you can also use GNU parallel to speed up the conversion:
请注意,通过这种方法,您还可以使用 GNU 并行来加速转换:
find -iname "*.JPG" | parallel -I'{}' convert {} {}.pdf