Merge multiple jpg into single pdf in Linux

后端 未结 6 1848
南旧
南旧 2021-01-30 02:25

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

Th

6条回答
  •  爱一瞬间的悲伤
    2021-01-30 02:49

    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
    

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

提交回复
热议问题