Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort files before converting to pdf in imagemagick

I have a folder full of image files I need to convert to a pdf. I used wget to download them. The problem is the ordering linux gives the files isn't the actual order of the pages, this is an example of the file ordering:

100-52b69f4490.jpg
101-689eb36688.jpg
10-1bf275d638.jpg
102-6f7dc2def9.jpg
103-2da8842faf.jpg
104-9b01a64111.jpg
105-1d5e3862d8.jpg
106-221412a767.jpg
...

I can convert these images to a pdf using imagemagick, with the command convert *.jpg output.pdf

but it'll put the pages into that pdf in the above order, not in human readable numerical order 1-blahblahblah.jpg, 2-blahblahblah.jpg, 3-blahblahblah.jpg etc.

Is the easiest way to do this pipe the output of sort to convert? or to pipe my wget to add each file as I'm getting it to a pdf file?

like image 581
Paxana Non Grata Avatar asked Oct 15 '25 15:10

Paxana Non Grata


2 Answers

convert $(ls -1v *.jpg) book.pdf

worked for me

like image 194
Qwentin Avatar answered Oct 18 '25 08:10

Qwentin


There are several options:

The simplest is as follows, but may overflow your command-line length if you have too many pages:

convert $(ls *jpg | sort -n) result.pdf

Next up is feeding the list of files on stdin like this:

ls *jpg | sort -n | convert @- result.pdf
like image 34
Mark Setchell Avatar answered Oct 18 '25 08:10

Mark Setchell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!