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
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
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:
The solution is to pad the filenames with zeros as required so they're the same length before running your convert command:
Now the files will be matched by the wildcard in the correct order, ready for the convert command:
Or just read the
ls
manual and see :So, doing what we need in single command.
Have fun ;) F.
You could use
via https://www.imagemagick.org/script/command-line-processing.php:
See also https://www.imagemagick.org/script/convert.php
All of the above answers failed for me, when I wanted to merge many high-resolution jpeg images (from a scanned book).
Imagemagick tried to load all files into RAM, I therefore used the following two-step approach:
Note that with this approach, you can also use GNU parallel to speed up the conversion:
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))
Mixing first idea with their reply, I think this code maybe satisfactory