-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathConvert graphics files.ps1
114 lines (86 loc) · 3.19 KB
/
Convert graphics files.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<#
.SYNOPSIS
Convert graphics files to other graphics formats
.DESCRIPTION
Some code from https://hazzy.techanarchy.net/posh/powershell/bmp-to-jpg-the-powershell-way/
.PARAMETER path
Directory containing the files to convert
.PARAMETER destFormat
The graphics format to convert the files to
.PARAMETER sourceFormat
The graphics format to convert the files from - will only act on files of this type found in the folder specified via -path
.PARAMETER destPath
Save destination file to this folder rather than the same folder as the source file
.PARAMETER recurse
Recurse the folder hierarchy specified via -path otherwise just operate on files in the specified folder
.EXAMPLE
& '.\Convert graphics files.ps1' -path "C:\Silly Screen Savers" -destFormat jpeg -sourceFormat bitmap
Find all bmp files in the folder "C:\Silly Screen Savers" and produce the correpsonding jpg file
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,HelpMessage="Directory containing files to be converted")]
[string]$path ,
[validateset( “bitmap”,”emf”,”exif”,”gif”,”icon”,”jpeg”,”png”,”tiff”,”wmf” )]
[string]$destFormat = 'jpeg' ,
[string]$destPath ,
[validateset( “bitmap”,”emf”,”exif”,”gif”,”icon”,”jpeg”,”png”,”tiff”,”wmf” )]
[string]$sourceFormat = 'bitmap' ,
[switch]$recurse
)
[hashtable]$formatToExtension = @{
“bitmap” = '.bmp'
”emf” = '.emf'
”exif” = '.exif'
”gif” = '.gif'
”icon” = '.ico'
”jpeg” = '.jpg'
”png” = '.png'
”tiff” = '.tif'
”wmf” = '.wmf'
}
if( $sourceFormat -eq $destFormat )
{
Throw "Source and destination formats are the same"
}
[string]$oldExtension = $formatToExtension[ $sourceFormat ]
[string]$newExtension = $formatToExtension[ $destFormat ]
if( [string]::IsNullOrEmpty( $oldExtension ) )
{
Throw "Unknown source format $sourceFormat"
}
if( [string]::IsNullOrEmpty( $newExtension ) )
{
Throw "Unknown destination format $destFormat"
}
[void][Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms' )
Get-ChildItem -Path $path -Recurse:$recurse -Force -Filter "*$oldExtension" | ForEach-Object `
{
Write-Verbose "Converting `"$($_.FullName)`""
[string]$newFile = $(if( $PSBoundParameters[ 'destPath' ] )
{
if( ! ( Test-Path -Path $destPath -PathType Container -ErrorAction SilentlyContinue) )
{
if( ! ( $newFolder = New-Item -Path $destPath -Force -ItemType Directory ) )
{
Throw "Failed to create destination path `"$destPath`""
}
}
Join-Path -Path $destPath -ChildPath ( $_.BaseName + $newExtension )
}
else
{
Join-Path -Path $_.Directory.FullName -ChildPath ( $_.BaseName + $newExtension )
})
if( Test-Path -Path $newFile -ErrorAction SilentlyContinue )
{
Write-Warning "`"$newFile`" already exists"
}
else
{
$file = New-Object -TypeName System.Drawing.Bitmap( $_.FullName )
$file.Save( $newFile , $destFormat )
$file.Dispose()
}
}