Created
January 11, 2017 14:55
-
-
Save SingingBush/cbb5106db91908064250f39b1f799fe3 to your computer and use it in GitHub Desktop.
Export VARBINARY images from a SQL Server query to jpeg files
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
<# | |
.SYNOPSIS | |
Script generating images from HEX values | |
.DESCRIPTION | |
Given a CSV dump of id's, usernames, and image data (HEX value of a VARBINARY field in SQL Server), generate a jpeg image for each row of csv data and also create a new csv file that lists the path of each image. | |
.EXAMPLE | |
hexPhotos.ps1 -f '.\db-dump.csv' | |
.NOTES | |
Author: SingingBush | |
Date: 11 Jan 2017 | |
#> | |
#[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true, HelpMessage="Path to CSV file of image data")] | |
[ValidateScript({Test-Path $_})] | |
[alias("f", "file")] | |
[string]$csvPath | |
) | |
$data = Import-CSV $csvPath -delimiter "`t" | |
## Create an 'output' folder if not already exists: | |
New-Item output -type directory -Force | |
$data | % { | |
write-host "Generating image for " -nonewline | |
write-host $_.username -nonewline -foregroundcolor cyan; | |
[string]$filename = "{0}.jpg" -f $_.id; | |
write-host ":`t" -nonewline | |
write-host $filename -foregroundcolor yellow; | |
$bytes = $_.Image.replace("0x", "") -split '(..)' | ? { $_ } | FOREACH { | |
[BYTE][CHAR]([CONVERT]::toint16($_, 16)) | |
} | |
[IO.File]::WriteAllBytes( ("{0}\output\{1}" -f $PWD, $filename), $bytes) | |
# return an object that consists of username and the file path | |
New-Object PSObject -Property @{ | |
username = $_.username; | |
image = "output\{0}" -f $filename; | |
} | |
} | Export-Csv 'profile_pics.csv' -Encoding UTF8 -Delimiter "," -Force -NoTypeInformation | |
Write-Host "images written to output folder" -foregroundcolor Magenta; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment