-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-Image.ps1
More file actions
131 lines (116 loc) · 5.84 KB
/
Copy pathGet-Image.ps1
File metadata and controls
131 lines (116 loc) · 5.84 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function Get-Image {
<#
.Synopsis
Gets information about images.
.Description
Get-Image gets an object that represents each image file.
The object has many properties and methods that you can use to edit images in Windows PowerShell.
.Notes
Get-Image uses Wia.ImageFile, a Windows Image Acquisition COM object to get image data.
Then it uses the Add-Member cmdlet to add note properties and script methods to the object.
The Resize script method uses the Add-ScaleFilter function. It has the following syntax:
Resize ($width, $height, [switch]$DoNotPreserveAspectRation).
Width and Height can be specified in pixels or percentages.
For a description of these parameters, type "get-help Add-ScaleFilter –par *".
The Crop script method uses the uses the Add-CropFilter function. It has the following syntax:
Crop ([Double]$left, [Double]$top, [Double]$right, [Double]$bottom).
All dimensions are measured in pixels.
For a description of these parameters, type "get-help Add-CropFilter –par *".
The FlipVertical, FlipHorizontal, RotateClockwise and RotateCounterClockwise script methods use the Add-RotateFlipFilter function.
For a description of these parameters, type "get-help Add-RotateFlipFilter –par *".
.Parameter File
[Required] Specifies the image files. Enter the path and file name of an image file, such as $home\pictures\MyPhoto.jpg.
You can also pipe one or more image files to Get-Image, such as those from Get-Item or Get-Childitem (dir).
.Example
Get-Image –file C:\myPics\MyPhoto.jpg
.Example
Get-ChildItem $home\Pictures -Recurse | Get-Image
.Example
(Get-Image –file C:\myPics\MyPhoto.jpg).resize(80, 120)
.Example
# Crops 8 pixels from the top of the image.
$CatPhoto = Get-Image –file $home\Pictures\Cat.jpg
$CatPhoto.crop(0,8,0,0)
.Example
$CatPhoto = Get-Image –file $home\Pictures\Cat.jpg
$CatPhoto.flipvertical()
.Example
dir $home\pictures\Vacation*.jpg | get-image | format-table fullname, horizontalResolution, PixelDepth –autosize
.Link
"Image Manipulation in PowerShell": http://blogs.msdn.com/powershell/archive/2009/03/31/image-manipulation-in-powershell.aspx
.Link
Add-CropFilter
.Link
Add-ScaleFilter
.Link
Add-RotateFlipFilter
.Link
Get-ImageProperties
#>
param(
[Parameter(ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
[Alias('FullName')]
[string]$file)
process {
$realItem = Get-Item $file -ErrorAction SilentlyContinue
if (-not $realItem) { return }
$image = New-Object -ComObject Wia.ImageFile
try {
$image.LoadFile($realItem.FullName)
$image |
Add-Member NoteProperty FullName $realItem.FullName -PassThru |
Add-Member ScriptMethod Resize {
param($width, $height, [switch]$DoNotPreserveAspectRatio)
$image = New-Object -ComObject Wia.ImageFile
$image.LoadFile($this.FullName)
$filter = Add-ScaleFilter @psBoundParameters -passThru -image $image
$image = $image | Set-ImageFilter -filter $filter -passThru
Remove-Item $this.Fullname
$image.SaveFile($this.FullName)
} -PassThru |
Add-Member ScriptMethod Crop {
param([Double]$left, [Double]$top, [Double]$right, [Double]$bottom)
$image = New-Object -ComObject Wia.ImageFile
$image.LoadFile($this.FullName)
$filter = Add-CropFilter @psBoundParameters -passThru -image $image
$image = $image | Set-ImageFilter -filter $filter -passThru
Remove-Item $this.Fullname
$image.SaveFile($this.FullName)
} -PassThru |
Add-Member ScriptMethod FlipVertical {
$image = New-Object -ComObject Wia.ImageFile
$image.LoadFile($this.FullName)
$filter = Add-RotateFlipFilter -flipVertical -passThru
$image = $image | Set-ImageFilter -filter $filter -passThru
Remove-Item $this.Fullname
$image.SaveFile($this.FullName)
} -PassThru |
Add-Member ScriptMethod FlipHorizontal {
$image = New-Object -ComObject Wia.ImageFile
$image.LoadFile($this.FullName)
$filter = Add-RotateFlipFilter -flipHorizontal -passThru
$image = $image | Set-ImageFilter -filter $filter -passThru
Remove-Item $this.Fullname
$image.SaveFile($this.FullName)
} -PassThru |
Add-Member ScriptMethod RotateClockwise {
$image = New-Object -ComObject Wia.ImageFile
$image.LoadFile($this.FullName)
$filter = Add-RotateFlipFilter -angle 90 -passThru
$image = $image | Set-ImageFilter -filter $filter -passThru
Remove-Item $this.Fullname
$image.SaveFile($this.FullName)
} -PassThru |
Add-Member ScriptMethod RotateCounterClockwise {
$image = New-Object -ComObject Wia.ImageFile
$image.LoadFile($this.FullName)
$filter = Add-RotateFlipFilter -angle 270 -passThru
$image = $image | Set-ImageFilter -filter $filter -passThru
Remove-Item $this.Fullname
$image.SaveFile($this.FullName)
} -PassThru
} catch {
Write-Verbose $_
}
}
}