Skip to content

Commit abd3ad2

Browse files
committed
Fixing file filter to read jpg only.
1 parent a41c3bf commit abd3ad2

File tree

2 files changed

+136
-6
lines changed

2 files changed

+136
-6
lines changed
0 Bytes
Binary file not shown.

GoogleImageSearch/GoogleImageSearch.psm1

Lines changed: 136 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$ImagePath
99
)
1010

11-
# Extract the image file name, without path.
11+
# Extract the image file name, without Path.
1212
$fileName = Split-Path $imagePath -Leaf
1313
Write-Verbose -Message "The image name for search: $fileName"
1414

@@ -92,7 +92,7 @@ function Search-WindowsLockScreenWallpapers
9292
Specifies a switch if wallpaper images should be dumped or not. A default value is False.
9393
9494
.PARAMETER DumpPath
95-
Specifies a path to a folder where the wallpapers should be dumped. The input can be a string or a valid instance of the System.IO.FileInfo class. A default value is 'C:\Temp'. The input directory is created if doesn't exist.
95+
Specifies a Path to a folder where the wallpapers should be dumped. The input can be a string or a valid instance of the System.IO.FileInfo class. A default value is 'C:\Temp'. The input directory is created if doesn't exist.
9696
9797
.EXAMPLE
9898
Search-WindowsLockScreenWallpapers
@@ -112,7 +112,7 @@ function Search-WindowsLockScreenWallpapers
112112
113113
.NOTES
114114
- This cmdlet uses non-standard way how to query Google Image Search. Do not use it for automatization.
115-
- A dump path is created if it doesn't exist.
115+
- A dump Path is created if it doesn't exist.
116116
117117
.LINK
118118
https://github.com/KUTlime/PowerShell-GoogleImageSearch-Module
@@ -155,7 +155,7 @@ function Search-WindowsLockScreenWallpapers
155155
# Select Images
156156
####################################################
157157
$Images = Get-ChildItem -Path $env:LOCALAPPDATA\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets -ErrorAction Continue |
158-
Where-Object { $_.Length / 1KB -gt 200 }
158+
Where-Object { (Get-KnownFileHeader -Path $_.FullName) -eq 'jpg'}
159159
$Images = $Images | Get-Image | Where-Object { $_.Width -gt $_.Height } | Sort-Object { $_.CreationTimeUtc } -Descending | Select-Object -First $NumberOfImages
160160
####################################################
161161

@@ -182,11 +182,11 @@ function Search-Image
182182
The Search-Image cmdlet reads the image and use this image to query Google Image Search. This cmdlet can be piped to the Get-ChildItem cmdlet for a batch processing.
183183
184184
.PARAMETER ImagePath
185-
Specifies a path to a image file. The input can be a string or a valid instance of the System.IO.FileInfo class.
185+
Specifies a Path to a image file. The input can be a string or a valid instance of the System.IO.FileInfo class.
186186
187187
.EXAMPLE
188188
Search-Image -ImagePath C:\Pictures\SomeImageToSearch.jpg
189-
Reads the image SomeImageToSearch.jpg in C:\Pictures path and use this image to query Google Image Search.
189+
Reads the image SomeImageToSearch.jpg in C:\Pictures Path and use this image to query Google Image Search.
190190
191191
.EXAMPLE
192192
Get-ChildItem -Path 'C:\Pictures' -Filter '*.jpg' | Search-Image
@@ -214,4 +214,134 @@ function Search-Image
214214
$ImagePath
215215
)
216216
Start-Process -FilePath (Get-GoogleImageSearchUrl -ImagePath $ImagePath.FullName)
217+
}
218+
219+
function Get-KnownFileHeader
220+
{
221+
[OutputType([String])]
222+
Param (
223+
[Parameter(Mandatory = $true,
224+
ValueFromPipeline = $true)]
225+
[ValidateNotNullOrEmpty()]
226+
[ValidateScript( { Test-Path $_ })]
227+
[System.IO.FileInfo]
228+
$Path
229+
)
230+
Begin
231+
{
232+
# Hexadecimal signatures for expected files
233+
$known = @'
234+
"Extension","Header"
235+
"3gp","66 74 79 70 33 67"
236+
"7z","37 7A BC AF 27 1C"
237+
"8sv","38 53 56 58"
238+
"8svx","46 4F 52 4D nn nn nn nn"
239+
"acbm","46 4F 52 4D nn nn nn nn"
240+
"aif","41 49 46 46"
241+
"aiff","46 4F 52 4D nn nn nn nn"
242+
"anbm","46 4F 52 4D nn nn nn nn"
243+
"anim","46 4F 52 4D nn nn nn nn "
244+
"asf","30 26 B2 75 8E 66 CF 11"
245+
"avi","52 49 46 46 nn nn nn nn "
246+
"bac","42 41 43 4B 4D 49 4B 45"
247+
"bpg","42 50 47 FB"
248+
"cab","4D 53 43 46"
249+
"cin","80 2A 5F D7"
250+
"class","CA FE BA BE"
251+
"cmus","46 4F 52 4D nn nn nn nn"
252+
"cr2","49 49 2A 00 10 00 00 00"
253+
"crx","43 72 32 34"
254+
"cwk","05 07 00 00 42 4F 42 4F"
255+
"cwk","06 07 E1 00 42 4F 42 4F"
256+
"dat","50 4D 4F 43 43 4D 4F 43"
257+
"DBA","BE BA FE CA"
258+
"DBA","00 01 42 44"
259+
"dex","64 65 78 0A 30 33 35 00"
260+
"djvu","41 54 26 54 46 4F 52 4D nn nn nn nn 44 4A 56"
261+
"dmg","78 01 73 0D 62 62 60"
262+
"doc","D0 CF 11 E0 A1 B1 1A E1"
263+
"dpx","53 44 50 58"
264+
"exr","76 2F 31 01"
265+
"fax","46 41 58 58"
266+
"faxx","46 4F 52 4D nn nn nn nn"
267+
"fh8","41 47 44 33"
268+
"fits","53 49 4D 50 4C 45 20 20"
269+
"flac","66 4C 61 43"
270+
"flif","46 4C 49 46"
271+
"ftxt","46 4F 52 4D nn nn nn nn"
272+
"gif","47 49 46 38 37 61"
273+
"ico","00 00 01 00"
274+
"idx","49 4E 44 58"
275+
"iff","41 43 42 4D"
276+
"iff","41 4E 42 4D"
277+
"iff","41 4E 49 4D"
278+
"iff","46 4F 52 4D nn nn nn nn"
279+
"ilbm","46 4F 52 4D nn nn nn nn"
280+
"iso","43 44 30 30 31"
281+
"jpg","FF D8 FF DB"
282+
"jpg","FF D8 FF E0"
283+
"lbm","49 4C 42 4D"
284+
"lz","4C 5A 49 50"
285+
"lz4","04 22 4D 18"
286+
"mid","4D 54 68 64"
287+
"mkv","1A 45 DF A3"
288+
"MLV","4D 4C 56 49"
289+
"mus","43 4D 55 53"
290+
"nes","4E 45 53 1A"
291+
"ods","50 4B 05 06"
292+
"ogg","4F 67 67 53"
293+
"PDB","00 00 00 00 00 00 00 00"
294+
"pdf","25 50 44 46"
295+
"png","89 50 4E 47 0D 0A 1A 0A"
296+
"ps","25 21 50 53"
297+
"psd","38 42 50 53"
298+
"rar","52 61 72 21 1A 07 00"
299+
"rar","52 61 72 21 1A 07 01 00"
300+
"smu","53 4D 55 53"
301+
"smus","46 4F 52 4D nn nn nn nn"
302+
"stg","4D 49 4C 20"
303+
"tar","75 73 74 61 72 00 30 30"
304+
"TDA","00 01 44 54"
305+
"tif","49 49 2A 00"
306+
"toast","45 52 02 00 00 00"
307+
"tox","74 6F 78 33"
308+
"txt","46 54 58 54"
309+
"vsdx","50 4B 07 08"
310+
"wav","52 49 46 46 nn nn nn nn"
311+
"wma","A6 D9 00 AA 00 62 CE 6C"
312+
"xar","78 61 72 21"
313+
"yuv","59 55 56 4E"
314+
"yuvn","46 4F 52 4D nn nn nn nn"
315+
"zip","50 4B 03 04"
316+
"epub","50 4B 03 04 0A 00 02 00"
317+
'@ | ConvertFrom-Csv | Sort-Object { $_.header.Length } -Descending
318+
319+
$known | ForEach-Object { $_.header = $_.header -replace '\s' }
320+
}
321+
Process
322+
{
323+
$HeaderAsHexString = New-Object System.Text.StringBuilder
324+
if ($PSVersionTable.PSVersion.Major -gt 5)
325+
{
326+
$bytes = [Byte[]](Get-Content -Path $Path.FullName -TotalCount 4 -AsByteStream -ErrorAction:Continue)
327+
}
328+
else
329+
{
330+
$bytes = [Byte[]](Get-Content -Path $Path.FullName -TotalCount 4 -Encoding:Byte -ErrorAction:Continue)
331+
}
332+
$bytes |
333+
ForEach-Object `
334+
{
335+
if (("{0:X}" -f $_).Length -eq 1)
336+
{
337+
$HeaderAsHexString.Append('0{0:X}' -f $_) | Write-Verbose
338+
}
339+
else
340+
{
341+
$HeaderAsHexString.Append('{0:X}' -f $_) | Write-Verbose
342+
}
343+
}
344+
345+
$known | Where-Object { $_.Header.StartsWith($HeaderAsHexString.ToString()) } | Select-Object -First 1 | ForEach-Object { $_.Extension }
346+
}
217347
}

0 commit comments

Comments
 (0)