Page 1 of 1

PDF to PNGs: get output filenames

Posted: 2011-06-07T00:27:31-07:00
by peterpham
I wrote a bash script to watch for pdf in a directory and convert them into png files.
Each pdf may have more than one pages.
Is there an easy way to grab for exactly converted png files?

I used command as

Code: Select all

convert -density 400 myfile.pdf myfile-%03d.png 
so png files will be:
  • myfile-000.png
    myfile-001.png
    myfile-002.png
    ...
Can I somehow grab these output filenames into a variable?
The reasons why I want to do this are:
  • -Log generated output files
    -Continue to do image optimize on each pdf

Re: PDF to PNGs: get output filenames

Posted: 2011-06-07T11:05:52-07:00
by fmw42
filename1=`convert myfile-000.png -format "%t" info:` gives the top of the filename (no suffix)
filename1=`convert myfile-000.png -format "%f" info:` gives the full filename with suffix

see
http://www.imagemagick.org/script/escape.php

Re: PDF to PNGs: get output filenames

Posted: 2011-06-07T16:30:59-07:00
by peterpham
fmw42 wrote:filename1=`convert myfile-000.png -format "%t" info:` gives the top of the filename (no suffix)
filename1=`convert myfile-000.png -format "%f" info:` gives the full filename with suffix

see
http://www.imagemagick.org/script/escape.php
This won't work as I don't know what the PNG will be.
As I said, I convert PDF to PNG and it can produce multiple PNGs file.

I think using "-format" is the right direction, but not sure how to make it work.
I have tried, for example, following

Code: Select all

convert -density 400 myfile.pdf myfile-%03d.png -format "%f" info:
However, it would give me (say, my PDF has 3 pages)

Code: Select all

myfile.pdf myfile.pdf myfile.pdf 
while I actually want this

Code: Select all

myfile-000.png myfile-001.png myfile-002.png

Re: PDF to PNGs: get output filenames

Posted: 2011-06-07T17:32:46-07:00
by fmw42
peterpham wrote:
fmw42 wrote:filename1=`convert myfile-000.png -format "%t" info:` gives the top of the filename (no suffix)
filename1=`convert myfile-000.png -format "%f" info:` gives the full filename with suffix

see
http://www.imagemagick.org/script/escape.php
This won't work as I don't know what the PNG will be.
As I said, I convert PDF to PNG and it can produce multiple PNGs file.

I think using "-format" is the right direction, but not sure how to make it work.
I have tried, for example, following

Code: Select all

convert -density 400 myfile.pdf myfile-%03d.png -format "%f" info:
However, it would give me (say, my PDF has 3 pages)

Code: Select all

myfile.pdf myfile.pdf myfile.pdf 
while I actually want this

Code: Select all

myfile-000.png myfile-001.png myfile-002.png

try

convert -density 400 myfile.pdf myfile-%03d.png | convert myfile-*.png -format "%f" info:
myfile-000.png
myfile-001.png
myfile-002.png

Re: PDF to PNGs: get output filenames

Posted: 2011-06-07T19:13:02-07:00
by peterpham
fmw42 wrote: try

convert -density 400 myfile.pdf myfile-%03d.png | convert myfile-*.png -format "%f" info:
myfile-000.png
myfile-001.png
myfile-002.png
It doesn't work if the script runs for the first time.
I receive an error saying
unable to open image `myfile-*.png': No such file or directory @ error/blob.c/OpenBlob/2572.
So probably the png weren't generated at the time when the second convert runs.

When I run this script for the second time, yes, indeed, I get
  • myfile-000.png
    myfile-001.png
    myfile-002.png

Re: PDF to PNGs: get output filenames

Posted: 2011-06-07T19:20:19-07:00
by fmw42
You are right, but this seems to work the first time (after I deleted the images created earlier so as to test again for a first time run)


convert -density 400 myfile.pdf myfile-%03d.png
convert myfile-*.png -format "%f" info:

You might also try some kind of delay in the pipe stream, thought I am not enough a unix person to say what that would be.