Batch convert pdfs in place (solved)

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
q00u
Posts: 1
Joined: 2019-03-28T21:25:38-07:00
Authentication code: 1152

Batch convert pdfs in place (solved)

Post by q00u »

In case anyone else ends up in a similar situation and is searching this forum like I was, the solution turned out to be:
  1. Use batch for-loops
  2. Extract images with pdfimages (from xpdf tools)
  3. Convert extracted images with imagemagick
I used a batch file like:

Code: Select all

for /r %%i in (*.pdf) do pdfimages "%%i" "%%i"
for /r %%i in (*.pgm) do magick "%%i" "%%i.png"
for /r %%i in (*.ppm) do magick "%%i" "%%i.png"
and optionally (since pgm/ppm files are gigantic) clean-up with:

Code: Select all

for /r %%i in (*.pgm) do del "%%i"
for /r %%i in (*.ppm) do del "%%i"
for /r %%i in (*.pdf) do del "%%i"
This resulted in perfectly clear images at maximum quality, better than when I was trying to convert directly from pdf.

They do have long xx.pdf-0000.ppm.png filenames now, but the important information is still there, and they're still in the right locations.



Original message was:

ImageMagick 7.0.8-33

I have several directories of pdfs, each consisting of a single image. In order to use them as the images they are, I'm trying to convert them to pngs. But since some are organized by filename and some by folder location:
  1. They have to keep their current name (but with .png instead of .pdf)
  2. They have to stay in the same directory
All of my searching has returned conflicting information. Much of the instructions were for previous versions of imagemagick, and some new ones said just use "magick mogrify", but others said just use "magick" without mogrify or convert anymore.

Here are some of the things I can recall trying so far:

Code: Select all

magick mogrify -density 300 *.pdf -format png
This was a disaster. It overwrote all the pdfs with lower-res versions of the same pdfs (good thing I had a backup). That's not what I wanted at all, haha. But, at least it kept the filenames, right?

Code: Select all

magick convert -density 300 *.pdf -quality 100 *.png
This one was real close! It successfully created png versions of all the pdfs! But, they were all named <last file I made with imagemagic>-###.png

Trying to run this one again (to confirm this was the command I'm remembering) now only gives me "WriteBlob Failed `*-##.png' @ error/png.c/MagickPNGErrorHandler/1714." So maybe I had used a different command? Then, why did it work that one time? I'm so easily confused.

I feel like there must be some easy indicator to tell ImageMagick that I want the output file to match each input file, but I can't find it.
Post Reply