Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function GlevConvertSequential() {
- #harcoding some stuff that's needed
- $script:maxw=3840
- $ref = "C:\users\USERNAME\desktop\zzGlev\Data\Tools\GlevLoadScreenMaker\reference\loadscreenref_"
- #Gets all files that match Stuff (.*) a period (\.) image extensions (jpg|jpeg|...)
- #Grave accent (`) just lets you space things across multiple lines so it's easier to read. if you get really long commands
- $imgs = Get-ChildItem -Path "C:\Users\USERNAME\Desktop\IMGs\static" | Where-Object {$_.Name -match ".*\.(jpg|jpeg|jif|jiff|gif|png|tif|tiff)"} | `
- Select BaseName,FullName,Extension
- $outfolder = "C:\Users\USERNAME\Desktop\zzOutput"
- $identloc = "C:\Users\USERNAME\Desktop\zzGlev\data\Tools\GlevLoadScreenMaker\reference\ImageMagickPortable\identifyx64.exe"
- $convertloc = "C:\Users\USERNAME\Desktop\zzGlev\data\Tools\GlevLoadScreenMaker\reference\ImageMagickPortable\convertx64.exe"
- foreach ($file in $imgs) {
- #Instead of building your own random filenames, you could use GUIDs. Almost impossible to get duplicates, and just one line
- #and built in to windows through .Net libraries
- $guidName = [guid]::NewGuid().ToString()
- $newName = $guidname + $file.Extension
- #Note that if you are testing this in ISE, Start-Process is bugged and doesn't actually return the output to console
- #The below doesn't work in ISE, but would work if just running as a script
- #$output = Start-Process -FilePath $identloc -ArgumentList "-format %wx%h `"$($file.FullName)`"" -NoNewWindow -Wait
- Start-Process -FilePath $identloc -ArgumentList "-format %wx%h `"$($file.FullName)`"" -NoNewWindow -Wait -RedirectStandardOutput "$env:temp\$guidName.txt"
- $output = Get-Content "$env:temp\$guidName.txt"
- Remove-Item "$env:temp\$guidName.txt"
- #No real changes here
- $srcimgsize = $output.Split('x')
- [int]$imgw = $srcimgsize[0]
- [int]$imgh = $srcimgsize[1]
- $offstw = 0
- $offsth = 82 #WAS 75, but 82 seems perfect
- $rezw = 960
- $rezh = 540
- while ($imgw -gt $rezw -Or $imgh -gt $rezh){
- if($rezw -eq $script:maxw ){
- break
- }
- $rezw = $rezw * 2
- $rezh = $rezh * 2
- if ($offstw -eq 0){
- $offstw = $offstw +1
- }
- else{
- $offstw = $offstw * 2
- }
- $offsth = $offsth * 2
- }
- [string]$rez = [string]$rezw + 'x' + [string]$rezh
- [string]$offset ='+'+[string]$offstw+'-'+[string]$offsth
- write-host 'Resizing to :'$rez'...'
- #Double quote strings will auto-expand variables, you don't need to add them, unless they aren't strings, or they are objects
- $pngref = "$ref$rez.png"
- #Save our temporary jpg to the actual temp directory, so if we fail to delete it, Windows will eventually clean it up
- #Using $($variable.Property) allows you to expand object properties inside of a double quoted string. Looks kind of ugly, but if you
- #have multiple strings in a string, can make it easier to just be one line.
- #Also, when passing anything file related, assuming there will be spaces, so make sure you escape the inner quotes, so when you pass the
- #filepath + file to imagemagick, it's surrounded by double quotes (single quotes won't work)
- Start-Process -FilePath $convertloc -ArgumentList "-resize $rez `"$($file.FullName)`" `"$env:temp\$guidName.jpg`"" -NoNewWindow -Wait
- write-host 'Creating reference image...'
- Start-Process -FilePath $convertloc -ArgumentList "`"$pngref`" `"$env:temp\$guidName.jpg`" -gravity center -geometry $offset -compose over -composite `"$env:temp\$guidName.jpg`"" -NoNewWindow -Wait
- write-host 'Converting to DDS...'
- #Define the compression type once, change if you have compression, then use that string
- #Rather than call two different functions in an if statement taht do basically the same thing
- $compression = "dds:compression=none"
- if ($compress) {
- $compression = "dds:compression=a8r8g8b8"
- }
- Start-Process -FilePath $convertloc -ArgumentList "-format dds -define dds:mipmaps=0 -define $compression -define dds:cluster-fit=true `"$env:temp\$guidName.jpg`" `"$outfolder\$guidName.dds`"" -NoNewWindow -Wait
- Remove-Item "$env:temp\$guidName.jpg"
- }
- }
- #Not mine, from here.
- #http://dille.name/blog/2015/09/11/displaying-progress-from-powershell-jobs/
- function Show-JobProgress {
- [CmdletBinding()]
- param(
- [Parameter(Mandatory,ValueFromPipeline)]
- [ValidateNotNullOrEmpty()]
- [System.Management.Automation.Job[]]
- $Job
- ,
- [Parameter()]
- [ValidateNotNullOrEmpty()]
- [scriptblock]
- $FilterScript
- )
- Process {
- $Job.ChildJobs | ForEach-Object {
- if (-not $_.Progress) {
- return
- }
- $LastProgress = $_.Progress
- if ($FilterScript) {
- $LastProgress = $LastProgress | Where-Object -FilterScript $FilterScript
- }
- $LastProgress | Group-Object -Property Activity,StatusDescription | ForEach-Object {
- $_.Group | Select-Object -Last 1
- } | ForEach-Object {
- $ProgressParams = @{}
- if ($_.Activity -and $_.Activity -ne $null) { $ProgressParams.Add('Activity', $_.Activity) }
- if ($_.StatusDescription -and $_.StatusDescription -ne $null) { $ProgressParams.Add('Status', $_.StatusDescription) }
- if ($_.CurrentOperation -and $_.CurrentOperation -ne $null) { $ProgressParams.Add('CurrentOperation', $_.CurrentOperation) }
- if ($_.ActivityId -and $_.ActivityId -gt -1) { $ProgressParams.Add('Id', $_.ActivityId) }
- if ($_.ParentActivityId -and $_.ParentActivityId -gt -1) { $ProgressParams.Add('ParentId', $_.ParentActivityId) }
- if ($_.PercentComplete -and $_.PercentComplete -gt -1) { $ProgressParams.Add('PercentComplete', $_.PercentComplete) }
- if ($_.SecondsRemaining -and $_.SecondsRemaining -gt -1) { $ProgressParams.Add('SecondsRemaining', $_.SecondsRemaining) }
- Write-Progress @ProgressParams
- }
- }
- }
- }
- function GlevConvertmultithread() {
- #Gets all files that match Stuff (.*) a period (\.) image extensions (jpg|jpeg|...)
- #Grave accent (`) just lets you space things across multiple lines so it's easier to read. if you get really long commands
- $imgs = Get-ChildItem -Path "C:\Users\USERNAME\Desktop\IMGs\static" | Where-Object {$_.Name -match ".*\.(jpg|jpeg|jif|jiff|gif|png|tif|tiff)"} | `
- Select BaseName,FullName,Extension
- foreach ($file in $imgs) {
- Start-Job -ScriptBlock {param($fullfilepath, $fileextension)
- #Start of Script Block
- #harcoding some stuff that's needed
- $script:maxw=3840
- $ref = "C:\users\USERNAME\desktop\zzGlev\Data\Tools\GlevLoadScreenMaker\reference\loadscreenref_"
- $outfolder = "C:\Users\USERNAME\Desktop\zzOutput"
- $identloc = "C:\Users\USERNAME\Desktop\zzGlev\data\Tools\GlevLoadScreenMaker\reference\ImageMagickPortable\identifyx64.exe"
- $convertloc = "C:\Users\USERNAME\Desktop\zzGlev\data\Tools\GlevLoadScreenMaker\reference\ImageMagickPortable\convertx64.exe"
- Write-Host "Starting Job $jobnum"
- #Instead of building your own random filenames, you could use GUIDs. Almost impossible to get duplicates, and just one line
- #and built in to windows through .Net libraries
- $guidName = [guid]::NewGuid().ToString()
- $newName = $guidname + $fileextension
- #Note that if you are testing this in ISE, Start-Process is bugged and doesn't actually return the output to console
- #The below doesn't work in ISE, but would work if just running as a script
- #$output = Start-Process -FilePath $identloc -ArgumentList "-format %wx%h `"$($file.FullName)`"" -NoNewWindow -Wait
- Start-Process -FilePath $identloc -ArgumentList "-format %wx%h `"$fullfilepath`"" -NoNewWindow -Wait -RedirectStandardOutput "$env:temp\$guidName.txt"
- $output = Get-Content "$env:temp\$guidName.txt"
- Remove-Item "$env:temp\$guidName.txt"
- #No real changes here
- $srcimgsize = $output.Split('x')
- [int]$imgw = $srcimgsize[0]
- [int]$imgh = $srcimgsize[1]
- $offstw = 0
- $offsth = 82 #WAS 75, but 82 seems perfect
- $rezw = 960
- $rezh = 540
- while ($imgw -gt $rezw -Or $imgh -gt $rezh){
- if($rezw -eq $script:maxw ){
- break
- }
- $rezw = $rezw * 2
- $rezh = $rezh * 2
- if ($offstw -eq 0){
- $offstw = $offstw +1
- }
- else{
- $offstw = $offstw * 2
- }
- $offsth = $offsth * 2
- }
- [string]$rez = [string]$rezw + 'x' + [string]$rezh
- [string]$offset ='+'+[string]$offstw+'-'+[string]$offsth
- #write-host 'Resizing to :'$rez'...'
- #Double quote strings will auto-expand variables, you don't need to add them, unless they aren't strings, or they are objects
- $pngref = "$ref$rez.png"
- #Save our temporary jpg to the actual temp directory, so if we fail to delete it, Windows will eventually clean it up
- #Using $($variable.Property) allows you to expand object properties inside of a double quoted string. Looks kind of ugly, but if you
- #have multiple strings in a string, can make it easier to just be one line.
- #Also, when passing anything file related, assuming there will be spaces, so make sure you escape the inner quotes, so when you pass the
- #filepath + file to imagemagick, it's surrounded by double quotes (single quotes won't work)
- Start-Process -FilePath $convertloc -ArgumentList "-resize $rez `"$fullfilepath`" `"$env:temp\$guidName.jpg`"" -NoNewWindow -Wait
- #write-host 'Creating reference image...'
- Start-Process -FilePath $convertloc -ArgumentList "`"$pngref`" `"$env:temp\$guidName.jpg`" -gravity center -geometry $offset -compose over -composite `"$env:temp\$guidName.jpg`"" -NoNewWindow -Wait
- #write-host 'Converting to DDS...'
- #Define the compression type once, change if you have compression, then use that string
- #Rather than call two different functions in an if statement taht do basically the same thing
- $compression = "dds:compression=none"
- if ($compress) {
- $compression = "dds:compression=a8r8g8b8"
- }
- Start-Process -FilePath $convertloc -ArgumentList "-format dds -define dds:mipmaps=0 -define $compression -define dds:cluster-fit=true `"$env:temp\$guidName.jpg`" `"$outfolder\$guidName.dds`"" -NoNewWindow -Wait
- Remove-Item "$env:temp\$guidName.jpg"
- #End of Job ScriptBLock
- Write-Host "Completing Job $jobnum"
- } -ArgumentList $file.Fullname,$file.Extension
- }
- #Job Monitoring
- While ($(Get-Job -State Running).count -gt 0) {
- $ComputersStillRunning = ""
- Write-Progress -Activity "Waiting for Jobs to finish" -Status "$($(Get-Job -State Running).count) images remaining" -PercentComplete ($(Get-Job -State Completed).count / $imgs.count * 100)
- Start-Sleep -Seconds 3
- }
- Write-Progress -Activity "Waiting for Jobs to finish" -Status "0 images remaining" -Completed
- #When done, kill all jobs, just in case
- Get-Job | Remove-Job -Force
- }
- Measure-Command {GlevConvertSequential}
- #converting 39 images, random sizes, took 210-230 seconds processing sequentially
- Measure-Command {GlevConvertmultithread}
- #converting 39 (of the same) images, took 40-120 seconds processing using jobs, huge differences were probably due to other background stuff
- #Your mileage may vary
- #In case you run the script itself for testing, you won't lose the results
- pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement