Home > Software engineering >  Converting TIF to PDF using ImageMagick and Powershell
Converting TIF to PDF using ImageMagick and Powershell

Time:09-07

I have a PowerShell (below) that will take a group of directories and combine the files and then rename the file the same as the directory. I know need to modify the script where it will take the files in ONE directory and just convert them from TIF to PDF utilizing ImageMagick. The files should not be combined. Is there a way to easily modify the script to do this?

$srcfolder = "C:\Work\SW\Powershell\TIF\"
$combine = "C:\`'Program Files`'\ImageMagick\convert.exe "
$arg1 = " -compress Group4 "

#-------------------------------------------------------------------
Write-Host "Start"
foreach ($srcitem in -Include ('*.tif') | Select -ExpandProperty DirectoryName -Unique)
 {
    $cmdline= $combine   "'" $srcitem "\*'" $arg1  "'" $srcitem ".pdf'"
    Write-Host $cmdline
    invoke-expression -Command $cmdline
    Remove-Item -Path $srcitem -Recurse
 }
#----------------------------------------------

CodePudding user response:

I don't have ImageMagick, but you should be able to do something similar to:

$srcfolder = 'C:\Work\SW\Powershell\TIF'
$convert   = 'C:\Program Files\ImageMagick\convert.exe'

#-------------------------------------------------------------------
Write-Host "Start"
# get an array of tif file FullNames
$tifFiles = (Get-ChildItem -Path $srcfolder -Filter '*.tif' -File).FullName
# loop over the list, change the extension to .pdf for the output file and convert
foreach ($srcitem in $tifFiles) {
    $destItem = [System.IO.Path]::ChangeExtension($srcitem, ".pdf")
    & $convert $srcitem $destItem
}
  • Related