3

I'm trying to write a bash scrip that uses Imagemagick to convert PDF's to images in a given directory (passed as an argument), and then save that conversion into a sub-folder of that directory. I have a script the works perfectly fine for converting and saving into the same directory, but when I try to add the path I'd like to converted files to be saved at the script breaks.

Here's what I have so far that works:

DIR="$@"
EXT="*.pdf"

mkdir -p "${DIR}images"
shopt -s nullglob
for pdffile in "$DIR"*.pdf; do
    convert -density 300 -depth 8 -quality 90 -trim "${pdffile}" "${pdffile%.*}".jpeg
done

Here's what I've tried for saving into a sub-folder. If I add "images/..." however to the last argument in converted like so:

DIR="$@"
EXT="*.pdf"

mkdir -p "${DIR}images"
shopt -s nullglob
for pdffile in "$DIR"*.pdf; do
    convert -density 300 -depth 8 -quality 90 -trim "${pdffile}" "images/${pdffile%.*}".jpeg
done

This is what happens when I execute the script:

$ ls foo
bar.pdf  baz.pdf  biz.pdf
$ ./pdf-to-img.sh foo/
convert-im6.q16: unable to open image `images/foo/bar.jpeg': No such file or directory @ error/blob.c/OpenBlob/2701.
convert-im6.q16: unable to open image `images/foo/baz.jpeg': No such file or directory @ error/blob.c/OpenBlob/2701.
convert-im6.q16: unable to open image `images/foo/biz.jpeg': No such file or directory @ error/blob.c/OpenBlob/2701.

What am I doing wrong?

  • Remember that ${pdffile%.*} will include the DIR prefix. Also please edit your question to show exactly how you're running the script - and the complete (unedited) error. – steeldriver May 12 '17 at 19:01
  • Question has been edited to reflect the complete error – Jon.H May 12 '17 at 19:30
  • 1
    So the error should be obvious now, no? you need to remove the DIR component (in this case foo) from ${pdffile%.*}" – steeldriver May 12 '17 at 19:39
  • I've tried that, but unfortunately I'm still getting that error. – Jon.H May 13 '17 at 0:23
  • You don't need a loop, you can do this in one go if you use mogrify with -path – don_crissti May 14 '17 at 1:06
1

Corrected code:

DIR="$@"
EXT="*.pdf"

mkdir -p "${DIR}/images"
shopt -s nullglob
for pdffile in "$DIR"*.pdf; do
    convert -density 300 -depth 8 -quality 90 -trim "${pdffile}" "$(dirname "${pdffile}")/images/$(basename "${pdffile%.*}").jpeg"
done

It should transform this path images/foo/bar.jpeg, which causes error, to this foo/images/bar.jpeg

Explanation:

You have value like this "foo/bar.pdf" in the ${pdffile} variable

dirname ${pdffile} - remove last non-slash component from path, leaving this: "foo"

basename ${pdffile%} - conversely, remove all except file name, leaving this: "bar.pdf"

Finally, inserting subdirctory name $images in between this parts and get needed path for storing images - foo/images/bar.jpeg.

  • Thank's for the answer. So the $ in $images was typo and not actually in my script, so it's just images/; sorry about that :( That being said however, I tried your other recommendation but that didn't work. The folder name has spaces in it, and it looks like the spaces are causing a problem. Lets say the real folder name is "PDFs For Courts", the error message I'm getting is: basename: extra operand ‘For’ Try 'basename --help' for more information. convert-im6.q16: no images defined ``../../data/entities . . Courts//.jpeg' @ error/convert.c/ConvertImageCommand/3258. – Jon.H May 13 '17 at 22:43
  • If images not variable, dollar sign should be removed. And I add double quotas for spaces handling. Try this string: $(dirname "${pdffile}")/images/$(basename "${pdffile%.*}").jpeg If this will work, I will edit my answer. – MiniMax May 13 '17 at 23:11
  • Outer quotas for whole string don't needed in this case, don't add them. – MiniMax May 13 '17 at 23:24
  • I have added whole corrected code to my answer. – MiniMax May 13 '17 at 23:34
  • 1
    I had to add outer quotes for on the output part so that it looked like: "$(dirname "${pdffile}")/images/$(basename "${pdffile%.*}").jpeg" in order to get it to work (otherwise it still choked on the spaces in the directory name) but it works! Thank you. – Jon.H May 14 '17 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.