0

I want to convert a bunch of jpeg files to one pdf file. The files are in a numbered format:

gls -v 2014-2015-2016_R_TR-4_all_170213*.jpeg

gives:

'2014-2015-2016_R_TR-4_all_170213 1.jpeg'  
'2014-2015-2016_R_TR-4_all_170213 2.jpeg'
'2014-2015-2016_R_TR-4_all_170213 3.jpeg'
'2014-2015-2016_R_TR-4_all_170213 4.jpeg'
'2014-2015-2016_R_TR-4_all_170213 5.jpeg'
'2014-2015-2016_R_TR-4_all_170213 6.jpeg'
'2014-2015-2016_R_TR-4_all_170213 7.jpeg'
'2014-2015-2016_R_TR-4_all_170213 8.jpeg' 
'2014-2015-2016_R_TR-4_all_170213 9.jpeg'
'2014-2015-2016_R_TR-4_all_170213 10.jpeg'

This is the order the jpegs should be shown in the pdf.

ls -l

gives

-rw-r--r--  1 rziege  staff   662186 13 Feb 16:06 2014-2015-2016_R_TR-4_all_170213 1.jpeg
-rw-r--r--  1 rziege  staff   867422 13 Feb 16:06 2014-2015-2016_R_TR-4_all_170213 10.jpeg
-rw-r--r--  1 rziege  staff   642198 13 Feb 16:06 2014-2015-2016_R_TR-4_all_170213 2.jpeg
-rw-r--r--  1 rziege  staff  1110602 13 Feb 16:06 2014-2015-2016_R_TR-4_all_170213 3.jpeg
-rw-r--r--  1 rziege  staff  1068419 13 Feb 16:06 2014-2015-2016_R_TR-4_all_170213 4.jpeg
-rw-r--r--  1 rziege  staff  1326947 13 Feb 16:06 2014-2015-2016_R_TR-4_all_170213 5.jpeg
-rw-r--r--  1 rziege  staff  1248453 13 Feb 16:06 2014-2015-2016_R_TR-4_all_170213 6.jpeg
-rw-r--r--  1 rziege  staff  1106629 13 Feb 16:06 2014-2015-2016_R_TR-4_all_170213 7.jpeg
-rw-r--r--  1 rziege  staff  1152435 13 Feb 16:06 2014-2015-2016_R_TR-4_all_170213 8.jpeg
-rw-r--r--  1 rziege  staff  1300553 13 Feb 16:06 2014-2015-2016_R_TR-4_all_170213 9.jpeg
-rw-r--r--  1 rziege  staff   867422 13 Feb 16:06 2014-2015-2016_R_TR-4_dir_170213 10.jpeg
-rw-r--r--  1 rziege  staff   642718 13 Feb 16:06 2014-2015-2016_R_TR-4_dir_170213 2.jpeg
-rw-r--r--  1 rziege  staff  1118243 13 Feb 16:06 2014-2015-2016_R_TR-4_dir_170213 3.jpeg
-rw-r--r--  1 rziege  staff  1074675 13 Feb 16:06 2014-2015-2016_R_TR-4_dir_170213 4.jpeg

...

I tried this by

convert "$file"*.jpeg "$file".pdf

with

$file=2014-2015-2016_R_TR-4_all_170213

which produces the pdf file in the the wrong order (1, 10, 2, 3, ...).

My alternative approaches

gls -v "$file"*.jpeg | convert "$file".pdf

and

convert $(gls -v "$file"*.jpeg) "$file".pdf

do not work either. Does anybody have a clue how I get this to work?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ruediger Ziege
  • 320
  • 3
  • 18

2 Answers2

2

Updated Answer

Mmmm, the spaces and the fact that your numbers are not zero-padded are combining to do you in! Might I suggest, if you generate the images yourself, that you name them without spaces and that you pad their numbers so that they come out in order without needing special treatment? E.g. file-0001.jpg, file-002.jpg etc - it is normally just a case of using sprintf("%03d"...) when generating filenames.

Anyway, to the case in hand, I think we'll need some awkward syntax to do it:

gls "$file*.jpg" | while read f; do
   convert "$f" miff:-
done | convert miff:- Something.pdf

Original Answer

Try:

gls -v | convert @- something.pdf
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • For this I get: convert: unable to open image `2014-2015-2016_R_TR-4_all_170213': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/504. convert: unable to open image `1.jpeg': No such file or directory @ error/blob.c/OpenBlob/2701. convert: unable to open image `2014-2015-2016_R_TR-4_all_170213': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/504. convert: unable to open image `2.jpeg': No suc... – Ruediger Ziege Feb 16 '17 at 13:35
  • 1
    Ok, please check your question - one of your filenames is shown with single quotes and the rest are not. Please also show a few lines of an `ls -l` listing so I can see what you really have. You will need to click `edit` under your question to update it. – Mark Setchell Feb 16 '17 at 13:47
  • Sorry, that was my mistake. gls -v output was with quotes. I edited my question – Ruediger Ziege Feb 16 '17 at 13:51
  • Something seemed to have one wrong with the first gls -v command I posted. I fixed this now. – Ruediger Ziege Feb 16 '17 at 14:01
  • Thanks for upadting your answer! I still get an error `gls: cannot access '2014-2015-2016_R_TR-4_all_170213*.jpg': No such file or directory`. Seems the wildcard does not work anymore. However, your right I should try to rename the files without spaces and change the numbering (this format came out of an automator skript and therefore I have to do it afterwards). – Ruediger Ziege Feb 17 '17 at 08:24
  • Try removing the double quotes around `"$f"` so it becomes just `$f`. – Mark Setchell Feb 17 '17 at 08:30
  • Same error again :-( I upvoted your answer nonetheless for suggesting to try to rename the files before converting. I will try to accomplish this, in case no solution is found. – Ruediger Ziege Feb 17 '17 at 08:53
1

The command you want to run is basically

convert '2014-2015-2016_R_TR-4_all_170213 1.jpeg' '2014-2015-2016_R_TR-4_all_170213 2.jpeg' '2014-2015-2016_R_TR-4_all_170213 3.jpeg' '2014-2015-2016_R_TR-4_all_170213 4.jpeg' '2014-2015-2016_R_TR-4_all_170213 5.jpeg' '2014-2015-2016_R_TR-4_all_170213 6.jpeg' '2014-2015-2016_R_TR-4_all_170213 7.jpeg' '2014-2015-2016_R_TR-4_all_170213 8.jpeg' '2014-2015-2016_R_TR-4_all_170213 9.jpeg' '2014-2015-2016_R_TR-4_all_170213 10.jpeg' '2014-2015-2016_R_TR-4_all_170213.pdf'

instead of

convert '2014-2015-2016_R_TR-4_all_170213 1.jpeg' '2014-2015-2016_R_TR-4_all_170213 10.jpeg' '2014-2015-2016_R_TR-4_all_170213 2.jpeg' '2014-2015-2016_R_TR-4_all_170213 3.jpeg' '2014-2015-2016_R_TR-4_all_170213 4.jpeg' '2014-2015-2016_R_TR-4_all_170213 5.jpeg' '2014-2015-2016_R_TR-4_all_170213 6.jpeg' '2014-2015-2016_R_TR-4_all_170213 7.jpeg' '2014-2015-2016_R_TR-4_all_170213 8.jpeg' '2014-2015-2016_R_TR-4_all_170213 9.jpeg' '2014-2015-2016_R_TR-4_all_170213.pdf'

I am not familiar with, but i assume gls -v is generating output with quotes

so:

gls -v "$file"*.jpeg|tr '\n' ' ';

should output:

'2014-2015-2016_R_TR-4_all_170213 1.jpeg' '2014-2015-2016_R_TR-4_all_170213 2.jpeg' '2014-2015-2016_R_TR-4_all_170213 3.jpeg' '2014-2015-2016_R_TR-4_all_170213 4.jpeg' '2014-2015-2016_R_TR-4_all_170213 5.jpeg' '2014-2015-2016_R_TR-4_all_170213 6.jpeg' '2014-2015-2016_R_TR-4_all_170213 7.jpeg' '2014-2015-2016_R_TR-4_all_170213 8.jpeg' '2014-2015-2016_R_TR-4_all_170213 9.jpeg' '2014-2015-2016_R_TR-4_all_170213 10.jpeg'

If that is the case, this should work fine:

eval 'convert '"$(gls -v "$file"*.jpeg|tr '\n' ' ')"' outfile.pdf'

but just to be sure, try this first and check whether the output is a valid shell command:

echo 'convert '"$(gls -v "$file"*.jpeg|tr '\n' ' ')"' outfile.pdf'

if there are no blanks of special chars in $file this is valid as well:

echo 'convert '"$(gls -v $file*.jpeg|tr '\n' ' ')"' '$file.pdf
Ruediger Ziege
  • 320
  • 3
  • 18
Samuel Kirschner
  • 1,135
  • 1
  • 12
  • 18
  • Thanks a lot for your answer! `gls -v "$file"*.jpeg|tr '\n' ' ';` outputs: `2014-2015-2016_R_TR-4_all_170213.jpeg 2014-2015-2016_R_TR-4_all_170213 2.jpeg 2014-2015-2016_R_TR-4_all_170213 3.jpeg 2014-2015-2016_R_TR-4_all_170213 4.jpeg 2014-2015-2016_R_TR-4_all_170213 5.jpeg 2014-2015-2016_R_TR-4_all_170213 6.jpeg 2014-2015-2016_R_TR-4_all_170213 7.jpeg 2014-2015-2016_R_TR-4_all_170213 8.jpeg 2014-2015-2016_R_TR-4_all_170213 9.jpeg 2014-2015-2016_R_TR-4_all_170213 10.jpeg` all without quotes. – Ruediger Ziege Feb 21 '17 at 08:29
  • And `echo 'convert '"$(gls -v "$file"*.jpeg|tr '\n' ' ')"' outfile.pdf'`outputs `convert 2014-2015-2016_R_TR-4_all_170213 1.jpeg 2014-2015-2016_R_TR-4_all_170213 2.jpeg 2014-2015-2016_R_TR-4_all_170213 3.jpeg 2014-2015-2016_R_TR-4_all_170213 4.jpeg 2014-2015-2016_R_TR-4_all_170213 5.jpeg 2014-2015-2016_R_TR-4_all_170213 6.jpeg 2014-2015-2016_R_TR-4_all_170213 7.jpeg 2014-2015-2016 ... outfile.pdf` This seems to be the problem. gls -v does not output with quotes. Any idea for a workaround for this issue? – Ruediger Ziege Feb 21 '17 at 08:31
  • Your answer worked after renaming the files in order to remove the whitespaces with `for f in *\ *; do mv "$f" "${f// /_}"; done` [(see here)](http://stackoverflow.com/questions/2709458/how-to-replace-spaces-in-file-names-using-a-bash-script). I accepted your answer, but maybe you want to include this in your answer in order to make it really work. – Ruediger Ziege Feb 22 '17 at 14:51