IT Cooking

Success is just one script away

ComfyUI : Convert PNG to AVIF/WEBP and Keep Exif Prompt Metadata!

4 min read
Windows script to convert ComfyUI PNG to AVIF or WebP + keep the Prompt and Workflow inside! Needs Imagick and Exiftool.
comfyui convert png to avif or webp and keep exif prompt metadata! featured meme

comfyui convert png to avif or webp and keep exif prompt metadata! featured meme

Tired of storing Gigabytes of dumb PNG produced by ComfyUI or Stable Diffusion? Want to leverage the smooth, sweet low size of WebP or AVIF? ComfyUI is now able to load WebP and AVIF natively. No excuses to not convert all those dumb PNG to AVIF! But how do you keep the ConfyUI Prompt and ConfyUI Workflow from the PNG during conversion? Here is how (on Windows).

The Batch Script to Convert PNG to AVIF or WebP

Quick start for those who know what they are doing: here is the 2AVIF.cmd script:

@echo OFF
:: adjust where you Magick location is here. you can also just update the %PATH% variable. You also need exiftool.exe in the PATH anyway
pushd %~sdp0..\PortableApps\Magick

:: change to outputExt=webp for webp and rename the script. that's it!
set outputExt=avif
:: for similar visual quality: WebP=90 and AVIF=60
set quality=60

for %%f in (%*) DO call :convert %%f
goto :end

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:convert
IF /I "%~x1"==".%outputExt%" exit /b 0

magick convert -define %outputExt%:exif-properties=true -quality %quality% %1 "%~sdpn1.%outputExt%"

IF /I "%~x1"==".png" (
  call :extractTag Prompt %1   && call :addExifTag Prompt Make %1
  call :extractTag Workflow %1 && call :addExifTag Workflow ImageDescription %1
)

goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:extractTag ComfyTag file
echo :extractTag %*

exiftool -struct -%1 -s3 %2 | busybox grep { >%TMP%\%1.txt
set CR=%ERRORLEVEL%

IF DEFINED DEBUG type %TMP%\%1.txt & pause

exit /b %CR%

goto :EOF


:addExifTag ComfyTag ExifTag file
echo :addExifTag  %*

<NUL set /p =%1: >%TMP%\%2.txt
type %TMP%\%1.txt | busybox tr -d '\r\n' >>%TMP%\%2.txt

exiftool -%2^<=%TMP%\%2.txt "%~sdpn3.%outputExt%"

:: delete _original backup created by exiftool
del /f /q "%~sdpn3.%outputExt%_original"

exiftool -struct "%~sdpn3.%outputExt%"
goto :EOF


:end

  • for WebP: rename it 2WEBP.cmd and change set outputExt=webp and that’s it
  • you need exiftool.exe accessible in the PATH, or simple update the script with full path to exiftool
  • Same for magick: either update the PATH variable, or change the location on the second line

Requirements

How to Use

  1. create a shortcut to each of the 2 scripts
  2. move the shortcuts to the sendTo folder
    1. to access the sendTo folder, type shell:sendto in a run box:
      run shell sendTo
  3. Then, simply select one or more PNG files, and right-click sendto > 2AVIF.cmd and boom done!
    windows right click send png 2avif
    windows right click send png 2avif

     

Explanations

for the curious mind, here is what’s happening under the hood.

PNG to AVIF conversion

The act of converting an image format to anopther is rather simple. The only quirks is the adding of quality compression settings, and keeping existing exif tags if any:

magick convert -define avif:exif-properties=true -quality 60 image.png image.avif

For WebP, simply swap avif for webp and that’s it. Magick cannot keep those Prompt and Workflow non-standard tags because, well, they are non-standard. It was a mistake from the start but since ComfyUI has become so huge, it’s now impossible to force the developer to start using Exif tags instead of those non-standard ones.

Retro-compatibility is a huge thing. They could simply keep them, and add actual Exif tags on top, but then it’s not my decision.

 

ComfyUI PNG Metadata to Exif Tag

When you generate an image, ComfyUI stores the JSON dump of its workflow and the prompt in 2 separate, non-standard custom PNG tags: Prompt and Workflow

comfyui prompt and workflow png tags with exiftool
comfyui prompt and workflow png tags with exiftool

If you know anything about Exif tags, you know these are non-standard and impossible to replicate in other formats. Other formats can only accept  3 kinds of metadata that I know of:

The easiest way to keep ComfyUI PNG metadata is to extract Those Prompt and Workflow tags with exiftool, and insert them as actual Exif tags in the new file. IFD (tag code) values should be next to each other. Why next to each other? Because otherwise they could be separated with other tags that will give ComfyUI a hard time to read through, when you load those images back into ComfyUI. More on how ComfyUI reads AVIF files with drag and drop here.

Which Exif IFD tag to use?

The maker of WAS Node Suite and Myself for 💾 Save Image Extended , have chosen those:

How To Extract Non-Standard PNG Metadata Tags?

Simply extract both Prompt and Workflow in a flat file:

exiftool -struct -Prompt -s3 image.png | busybox grep { >%TMP%\image.txt
exiftool -struct -Workflow -s3 image.png | busybox grep { >%TMP%\image.txt

The grep part ensure we only keep the line with the JSON. Exiftool is a bit verbose.

How to Write Exif IFD Metadata into AVIF file?

We need to inject potentially 2 exif tags (workflow images don’t have the Prompt part) and these tags must be a flat JSON string, no carriage returns, no extra curly braces, no extra double quotes: Prompt: {value}

When we extracted the JSON from the non-standard PNG tags, we only got the value part. So we first create a blank file with the key part, and also avoid adding a carriage return at the end:

<NUL set /p =Prompt: >%TMP%\Make.txt

Next, we remove all carriage returns from the value we extracted prior, and add it to the whole tag we want to inject:

type %TMP%\Prompt.txt | busybox tr -d '\r\n' >>%TMP%\Make.txt

Next, we inject this tag into the file, in place, like so:

exiftool -Make^<=%TMP%\Make.txt image.avif

Finally, we delete the backup created by exiftool:

del /f /q image.avif_original

 

Any questions?

Leave a Reply

Your email address will not be published. Required fields are marked *