Home > Software engineering >  ImageMagik Combining JPGS in folders and subfolders into PDFs
ImageMagik Combining JPGS in folders and subfolders into PDFs

Time:01-06

I have a script that, when I right click on a folder, combines all pngs/jpgs/tifs inside the folder into a PDF and renames the PDF to the name of the folder it resides in.

cd %~dpnx1
for %%a in (.) do set currentfolder=%%~na
start cmd /k magick "*.{png,jpg,tif}" "%currentfolder%.pdf"

However, I have quite a lot of folders and currently have to do this one by one. How can I create a function where I can right click on a folder, which searches subfolders and combines the jpgs to PDF? So in the example below, Im wanting to create 3 PDFS (Folder A, Folder B and Folder C) by right clicking and running batch on the parent folder.

Example:

  • Parent folder (one that I would right click and run script from)
  • |- Folder A
  • ||- test1.jpg
  • ||- test2.jpg
  • ||- test3.jpg
  • |- Folder B
  • ||- example1.jpg
  • || - example2.jpg
  • |- Folder C
  • || Folder D
  • |||- temp.jpg
  • |||- temp2.jpg

I have attempted myself with the following, but no luck:

    cd %~dpnx1
FOR /R %%a in (.) DO (
   PUSHD %%a
   set currentfolder=%%~na
   magick "*.{png,jpg,tif}" "%currentfolder%.pdf"
   POPD
)

I'm on Mac. Hope you can help. Thank you.

CodePudding user response:

For bash, try this (tested on Linux):

for d in */; do convert "$d"/*.{png,jpg,tif} "$d/${d%/}.pdf" ; done

In slo-mo:

  • for d in */: loop on all directories in current directory (the / restrict matches to directories)). Variable $d contains the directory name, and name has a final /.
  • "$d"/*.{png,jpg,tif}: all files with png, jpg or tif extension in the directory "$d"
  • "$d/${d%/}.pdf": the directory name, a slash, the directory name with the ending slash removed, and .pdf`

If you look carefully, the explicit / in the code aren't necessary since there is already one at the end of "$", but leaving them in makes the code a bit more readable and the multiple '//' as coalesced into a single one.

This code may however complain that there are no png/jpg/tif. A slightly different form makes it behave more nicely:

shopt -s extglob # this is possibly already set by default
for d in */; do convert "$d"/*.@(png|jpg|tif) "$d/${d%/}".pdf ; done

CodePudding user response:

There are several aspects to this question, and judging by your attempted solution, they will all be non-trivial for you. It is more than a normal question so I'll just give you an outline so you can tackle it in chunks. You'll need to:

  • install homebrew
  • install ImageMagick
  • use Automator to make a workflow for right-click
  • learn some bash scripting to recurse through directories
  • learn some ImageMagick to make PDFs

I'll give notes for each item, and maybe fill them out if I have time.


Install homebrew

Go to here and follow instructions to install homebrew. I am not repeating the instructions here as they may change.

You'll likely need to install Xcode command-line tools using xcode-select

You'll need to set your PATH properly afterwards. Don't omit this step.


Install ImageMagick

You'll need to do:

brew install imagemagick

Setup workflow with Automator for right-click

I'll cover this later.


bash script to recurse directories

I'll cover this later.


ImageMagick script to make PDF

I'll cover this later. It'll be like:

magick *.tif *.png *.jpg DOCUMENT.PDF
  • Related