Last active
June 7, 2023 18:26
-
-
Save peaeater/9f5851028d51a5bc9c1c to your computer and use it in GitHub Desktop.
Converts PDF pages to PNGs with imagemagick.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# convert pdf to png | |
# requires imagemagick w/ ghostscript | |
param ( | |
[Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0)] | |
[ValidateScript({[System.IO.Path]::GetExtension($_) -eq ".pdf"})] | |
[string]$in, | |
[string]$magick = "C:\utils\imagemagick\ImageMagick-7.1.1-Q16-HDRI\magick.exe" | |
) | |
process { | |
$basePath = split-path $script:myinvocation.mycommand.path | |
$file = new-object System.IO.FileInfo([System.IO.Path]::Combine($basePath, $in)) | |
$input = ('{0}' -f $file.FullName) | |
$outdir = ('{0}\png' -f $file.DirectoryName) | |
if (!(test-path $outdir)) { | |
mkdir $outdir | |
} | |
$o = ('{0}\{1}.png' -f $outdir, 'page') | |
$arguments = "convert -verbose -density 300 `"$input`" `"$o`"" | |
start-process $magick $arguments -wait -NoNewWindow | |
# rename and renumber (from 1 instead of 0) | |
$pages = get-childitem "$outdir\*-*.*" -include *.png | |
foreach ($page in $pages) { | |
$m = ([regex]"^(.+\-)(\d+)(\.png)$").matches($page.name) | |
$title = $m.Groups[1].Value | |
$num = $m.Groups[2].Value | |
$ext = $m.Groups[3].Value | |
$nextNum = 1 + $num | |
$newName = $("{0}{1}" -f $nextNum, $ext) | |
move-item $page "$outdir\$newName" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment