PDF to PNGs: get output filenames

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
peterpham
Posts: 5
Joined: 2010-10-24T18:29:17-07:00
Authentication code: 8675308

PDF to PNGs: get output filenames

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNGs: get output filenames

Post 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
peterpham
Posts: 5
Joined: 2010-10-24T18:29:17-07:00
Authentication code: 8675308

Re: PDF to PNGs: get output filenames

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNGs: get output filenames

Post 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
peterpham
Posts: 5
Joined: 2010-10-24T18:29:17-07:00
Authentication code: 8675308

Re: PDF to PNGs: get output filenames

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNGs: get output filenames

Post 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.
Post Reply