Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Save generation parameters in PNG metadata #69

Open
tildebyte opened this issue Jul 21, 2022 · 2 comments
Open

FEAT: Save generation parameters in PNG metadata #69

tildebyte opened this issue Jul 21, 2022 · 2 comments

Comments

@tildebyte
Copy link

tildebyte commented Jul 21, 2022

This is something I added to my (heavily modified) 'CLIP_Guided_Diffusion_HQ_256x256' notebook last year.

Basically, just as $topic - store all of the parameters used to generate an image in the metadata of the image itself.

Example:
a beautiful painting of a flamingo zombie in the style of Kandinsky 0 7, the color from Accent in Rose 0 1, the pose from Lawrence's Pinkie 0 2

AFAICT, GitHub doesn't munge the image in any way, so opening it with something like TweakPNG (no affiliation) will let you easily read all of the metadata.

The code is pretty straightforward, something like this should suffice

# Required
from PIL.PngImagePlugin import PngInfo

# For example (not necessary)
from torchvision.transforms import functional as TF

# For e.g.
global_config = {
    'clip_guidance_scale': 1000,
    'perlin_init': True,
    'perlin_mode': 'gray',
    # etc.
}


# This stores each param as a separate metadata entry, but dumping the
# whole thing into one field would work just as well
def create_png_metadata(cfg):
    metadata = PngInfo()
    for key, value in cfg.items():
        try:
            metadata.add_text(f'AI_{key}', str(value))
        except UnicodeEncodeError:
            pass
    return metadata


this_metadata = create_png_metadata(global_config)
this_metadata.add_text('AI_this_filename', filename)

# Obviously this relies on tensorflow's image writing...
TF.to_pil_image(image).save(str(filename), pnginfo=this_metadata)

My first thought was to open a PR, but for the life of me, I can't find the image writing code... 😆

@hanxiao
Copy link
Member

hanxiao commented Jul 22, 2022

make sense, will add it as an option

@OverStruck
Copy link

Indeed it's a nice idea. I was thinking of doing the same thing but casually decided to see if any one was talking about this and found this! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants