BuccoBruce

SD JPG

Jan 15th, 2023 (edited)
1,007
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 4.00 KB | Software | 0 0
  1. #note, requires imagemagick's "convert" and "identify" in your PATH, as well as "exiftool"
  2. #CHATGPT RE-WROTE NOVELAI METADATA TO VOLDY'S, KEK
  3. #no it didn't, chatgpt forgot the seed, and the cfg, and a bunch of quotes
  4. #but it helped
  5. Get-ChildItem .\ -Filter *.png | ForEach-Object -Parallel { #If you are not using PowerShell 7, Remove "-Parallel"
  6. #Scan current directory for PNGs, does not recurse subdirectories because lol variable
  7.     magick convert .\$_ -quality 88 -define preserve-timestamp=true .\$($_.BaseName).jpg #Converts to JPG at 88 quality. Raise or lower as you see fit.
  8.     #following code block eventually sets the date created/modified of your new JPG to that of the original PNG at the very end
  9.     $PNG = Get-Item -LiteralPath $_
  10.     $JPG = Get-Item -LiteralPath .\$($_.BaseName).jpg
  11.     #Uses ImageMagick "identify" to read webui prompt
  12.     $comments = magick identify -format '%[Parameters]' -verbose $_.FullName 2> $null
  13.     $NAIerror = magick identify -format '%[Description]' -verbose $_.FullName 2> $null
  14.     #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
  15.     if(-Not $NAIerror -And -Not $comments){
  16.         Write-Host -ForegroundColor Red "Unicode Parsing Error For " -NoNewline; Write-Host -ForegroundColor Yellow $_
  17.         Write-Host -ForegroundColor Green "Renaming JPG, not deleting PNG"
  18.         Write-Host -ForegroundColor DarkRed "Fuck you ImageMagick, it's 2023, support Unicode properly"
  19.         ren -LiteralPath .\$($_.BaseName).jpg .\UTF-Error-$($_.BaseName).jpg
  20.         }
  21.     #copy webui prompt into JPG, delete temporary text file, delete png, set date created and modified to match input PNG
  22.     elseif($comments){
  23.         $comments | Out-File .\$($_.BaseName).txt -encoding utf8
  24.         exiftool -P "-ExifIFD:UserComment<=.\$($_.BaseName).txt" .\$($_.BaseName).jpg -overwrite_original
  25.         $JPG.CreationTime = $PNG.CreationTime
  26.         $JPG.LastWriteTime = $PNG.LastWriteTime
  27.         del -LiteralPath $_ # IT DELETES THE ORIGINAL PNG - REMOVE THIS ENTIRE LINE TO KEEP THE ORIGINAL PNG
  28.         del -LiteralPath .\$($_.BaseName).txt
  29.         }
  30.     #convert NovelAI tags to single "Comment" webui tag, do the same actions as above
  31.     else{
  32.         mogrify -strip .\$($_.BaseName).jpg
  33.         $NAIcomments = magick identify -format '%[Title]\n%[Description]\n%[Software]\n%[Source]\n%[Comment]' -verbose $_.FullName 2> $null
  34.         $NAIresolution = magick identify -format '%wx%h' $_.FullName
  35.         $output = ''
  36.         $NAIComments= $NAIComments -split "`n"
  37.         $output += $NAIComments[1] + "`n"
  38.         $jsonData = (ConvertFrom-Json $NAIComments[4])
  39.         $output += "Negative prompt: " + $jsonData.uc + "`n"
  40.         $output += "Steps: " + $jsonData.steps + ", "
  41.         $sampler = $jsonData.sampler
  42.         if ($sampler -eq "k_euler_ancestral") {
  43.             $output += "Sampler: Euler a, "
  44.         }
  45.         elseif ($sampler -eq "k_euler") {
  46.             $output += "Sampler: Euler, "
  47.         }
  48.         else {
  49.             $output += "Sampler: " + $sampler + ", "
  50.         }
  51.         $output += "CFG scale: " + $jsonData.scale + ", Seed: " + $jsonData.seed + ", Size: " + $NAIresolution + ", "
  52.         $stableDiff = $NAIComments[3].Split(" ")[2]
  53.         if ($stableDiff -eq "1D44365E") {
  54.         $output += "Model hash: 1d4a34af, Model: animesfw-final-pruned"
  55.         }
  56.         elseif ($stableDiff -eq "81274D13") {
  57.         $output += "Model hash: 925997e9, Model: animefull-final-pruned"
  58.         }
  59.         else {$output += "Model hash: Not specified, Model: Not specified"}
  60.         $output += ", Clip skip: 2, ENSD: 31337"
  61.         $output | Out-File -LiteralPath .\$($_.BaseName).txt -encoding utf8
  62.         exiftool -P "-ExifIFD:UserComment<=.\$($_.BaseName).txt" .\$($_.BaseName).jpg -overwrite_original
  63.         $JPG.CreationTime = $PNG.CreationTime
  64.         $JPG.LastWriteTime = $PNG.LastWriteTime
  65.         del -LiteralPath $_ # IT DELETES THE ORIGINAL PNG - REMOVE THIS ENTIRE LINE TO KEEP THE ORIGINAL PNG
  66.         del -LiteralPath .\$($_.BaseName).txt
  67.     }
  68. }
Comments
  • BuccoBruce
    1 year
    # text 0.86 KB | 0 0
    1. lmao, thanks chatgpt
    2. > Alternatively, you can cast the variable to a string with [string]::Concat()
    3. No. No no no.
    4. > 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.
    5. >
    6. > 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.
    7. >
    8. > You can use the -LiteralPath parameter when calling Get-Item and magick convert commands, like this:
    9. >
    10. >Copy code
    11. >$PNG = Get-Item -LiteralPath $_
    12. > magick convert -LiteralPath .\$_ -quality 88 -define preserve-timestamp=true .\$($_.BaseName).jpg
    13. 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