在Linux中将多个jpg合并为单个pdf

编程入门 行业动态 更新时间:2024-10-20 21:04:08
本文介绍了在Linux中将多个jpg合并为单个pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用以下命令将目录中的所有jpg文件转换并合并为单个pdf文件.

I used the following command to convert and merge all the jpg files in a directory to a single pdf file.

convert *.jpg file.pdf

目录中的文件从1.jpg到123.jpg编号.转换进行得很好,但是在转换之后,页面都混合了.我希望pdf的页面从1.jpg到123.jpg的命名顺序相同.我也尝试了以下命令:

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:

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

但是仍然没有运气.操作平台Linux.

But still no luck. Operating platform Linux.

推荐答案

问题是因为您的外壳程序以纯字母顺序扩展了通配符,并且由于数字的长度不同,因此顺序不正确:

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:

$ 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

现在,文件将由通配符以正确的顺序进行匹配,并准备好执行convert命令:

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 ...

更多推荐

在Linux中将多个jpg合并为单个pdf

本文发布于:2023-11-22 19:25:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618708.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   并为   中将   Linux   jpg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!