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?
1 Answer 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 justimages/
; 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– Jon.H2017年05月13日 22:43:40 +00:00Commented May 13, 2017 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– MiniMax2017年05月13日 23:11:43 +00:00Commented May 13, 2017 at 23:11
-
Outer quotas for whole string don't needed in this case, don't add them.MiniMax– MiniMax2017年05月13日 23:24:32 +00:00Commented May 13, 2017 at 23:24
-
I have added whole corrected code to my answer.MiniMax– MiniMax2017年05月13日 23:34:34 +00:00Commented May 13, 2017 at 23:34
-
1I 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– Jon.H2017年05月14日 01:27:08 +00:00Commented May 14, 2017 at 1:27
${pdffile%.*}
will include theDIR
prefix. Also please edit your question to show exactly how you're running the script - and the complete (unedited) error.DIR
component (in this casefoo
) from${pdffile%.*}"
mogrify
with-path