Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #note, requires imagemagick's "convert" and "identify" in your PATH, as well as "exiftool"
- #CHATGPT RE-WROTE NOVELAI METADATA TO VOLDY'S, KEK
- #no it didn't, chatgpt forgot the seed, and the cfg, and a bunch of quotes
- #but it helped
- Get-ChildItem .\ -Filter *.png | ForEach-Object -Parallel { #If you are not using PowerShell 7, Remove "-Parallel"
- #Scan current directory for PNGs, does not recurse subdirectories because lol variable
- magick convert .\$_ -quality 88 -define preserve-timestamp=true .\$($_.BaseName).jpg #Converts to JPG at 88 quality. Raise or lower as you see fit.
- #following code block eventually sets the date created/modified of your new JPG to that of the original PNG at the very end
- $PNG = Get-Item -LiteralPath $_
- $JPG = Get-Item -LiteralPath .\$($_.BaseName).jpg
- #Uses ImageMagick "identify" to read webui prompt
- $comments = magick identify -format '%[Parameters]' -verbose $_.FullName 2> $null
- $NAIerror = magick identify -format '%[Description]' -verbose $_.FullName 2> $null
- #ImageMagick can't into UTF-8/iTXt in AD 2023, this will rename the JPG so that you know it is not tagged, and will not delete the PNG
- if(-Not $NAIerror -And -Not $comments){
- Write-Host -ForegroundColor Red "Unicode Parsing Error For " -NoNewline; Write-Host -ForegroundColor Yellow $_
- Write-Host -ForegroundColor Green "Renaming JPG, not deleting PNG"
- Write-Host -ForegroundColor DarkRed "Fuck you ImageMagick, it's 2023, support Unicode properly"
- ren -LiteralPath .\$($_.BaseName).jpg .\UTF-Error-$($_.BaseName).jpg
- }
- #copy webui prompt into JPG, delete temporary text file, delete png, set date created and modified to match input PNG
- elseif($comments){
- $comments | Out-File .\$($_.BaseName).txt -encoding utf8
- exiftool -P "-ExifIFD:UserComment<=.\$($_.BaseName).txt" .\$($_.BaseName).jpg -overwrite_original
- $JPG.CreationTime = $PNG.CreationTime
- $JPG.LastWriteTime = $PNG.LastWriteTime
- del -LiteralPath $_ # IT DELETES THE ORIGINAL PNG - REMOVE THIS ENTIRE LINE TO KEEP THE ORIGINAL PNG
- del -LiteralPath .\$($_.BaseName).txt
- }
- #convert NovelAI tags to single "Comment" webui tag, do the same actions as above
- else{
- mogrify -strip .\$($_.BaseName).jpg
- $NAIcomments = magick identify -format '%[Title]\n%[Description]\n%[Software]\n%[Source]\n%[Comment]' -verbose $_.FullName 2> $null
- $NAIresolution = magick identify -format '%wx%h' $_.FullName
- $output = ''
- $NAIComments= $NAIComments -split "`n"
- $output += $NAIComments[1] + "`n"
- $jsonData = (ConvertFrom-Json $NAIComments[4])
- $output += "Negative prompt: " + $jsonData.uc + "`n"
- $output += "Steps: " + $jsonData.steps + ", "
- $sampler = $jsonData.sampler
- if ($sampler -eq "k_euler_ancestral") {
- $output += "Sampler: Euler a, "
- }
- elseif ($sampler -eq "k_euler") {
- $output += "Sampler: Euler, "
- }
- else {
- $output += "Sampler: " + $sampler + ", "
- }
- $output += "CFG scale: " + $jsonData.scale + ", Seed: " + $jsonData.seed + ", Size: " + $NAIresolution + ", "
- $stableDiff = $NAIComments[3].Split(" ")[2]
- if ($stableDiff -eq "1D44365E") {
- $output += "Model hash: 1d4a34af, Model: animesfw-final-pruned"
- }
- elseif ($stableDiff -eq "81274D13") {
- $output += "Model hash: 925997e9, Model: animefull-final-pruned"
- }
- else {$output += "Model hash: Not specified, Model: Not specified"}
- $output += ", Clip skip: 2, ENSD: 31337"
- $output | Out-File -LiteralPath .\$($_.BaseName).txt -encoding utf8
- exiftool -P "-ExifIFD:UserComment<=.\$($_.BaseName).txt" .\$($_.BaseName).jpg -overwrite_original
- $JPG.CreationTime = $PNG.CreationTime
- $JPG.LastWriteTime = $PNG.LastWriteTime
- del -LiteralPath $_ # IT DELETES THE ORIGINAL PNG - REMOVE THIS ENTIRE LINE TO KEEP THE ORIGINAL PNG
- del -LiteralPath .\$($_.BaseName).txt
- }
- }
Comments
-
- lmao, thanks chatgpt
- > Alternatively, you can cast the variable to a string with [string]::Concat()
- No. No no no.
- > The script you've provided uses the $_ variable to reference the current item in the pipeline during the ForEach-Object loop. This variable contains the current file name, so you can use it to reference the file in your commands.
- >
- > If the filenames contain special characters such as { or [, you need to make sure that PowerShell treats them as literal strings and not as special characters.
- >
- > You can use the -LiteralPath parameter when calling Get-Item and magick convert commands, like this:
- >
- >Copy code
- >$PNG = Get-Item -LiteralPath $_
- > magick convert -LiteralPath .\$_ -quality 88 -define preserve-timestamp=true .\$($_.BaseName).jpg
- I'm pretty sure magick convert isn't a PowerShell commandlet, but it's got the spirit. It works now, lmao.
Add Comment
Please, Sign In to add comment