Skip to content

Conversation

@sfisher
Copy link
Contributor

@sfisher sfisher commented Aug 16, 2025

I hope this is helpful and I know at least a few other people are interested, but feel free to tell me to get lost if it's not something you can deal with and I'll just keep updating it for myself after every new release.

I find it useful to save the images from the generation thumbnails (select thumbnails and right-click or click "..." and save image). Since I may come back to the PNGs later, it's very helpful to have some generation metadata in there (and currently none that I could see for files saved this way).

I've been running some code locally for about 4 months and haven't had any failures with thousands of images saved (also tested some UTF-8 characters like 4-bytle emoji and other characters in the metadata and it all seems to work).

I was originally going to use Pillow to do this, but there is a whole nightmare waiting with Pillow since it requires per-platform binaries and makes things heavier, so found out that writing the headers into existing PNG files is relatively straight-forward so could just include a method to do it without all the overhead.

I added most of the code to the util class, but don't know if it belongs there or elsewhere (though they are metadata/file rewriting utils).

Trying to catch a plethora of exceptions so if something goes wrong it just saves the file and doesn't save the metadata and also tried to make minimal changes to existing code.

I looked into contributing guidelines, added some tests, ruff passes.

I think I had some trouble running tests which may require additional server setup or something, sorry I'm a bit lost about how to do that.

So I don't know if this interferes with any of the other tests but the util tests pass, since I'm not sure I have a complete testing setup.

LMK what you think and I'm happy to try to make additional changes if this is something you're interested in including. I think it makes the PNGs a lot nicer so you have some idea how they were generated.

@Acly
Copy link
Owner

Acly commented Aug 17, 2025

We can include it. There's a few changes I'd like to see:

  • Add an option in Interface settings to enable/disable metadata. There are some people who don't want their "prompts leaked" when they share images... see (Suggesion) Saving images with the generation metadata #660
  • Instead of writing PNG to file, then reading the file back into memory, use Image.to_bytes to do it directly
  • Reorganize the code a bit:
    • image.py: put image saving code here, maybe Image.save_with_metadata(metadata: str) or similar
    • text.py: the code that converts job info into metadata could be put here, it already has lots of similar code

@sfisher
Copy link
Contributor Author

sfisher commented Aug 18, 2025

Thanks for the good feedback. I'll see what I can do. It may take me a little time because of some other things going on, but it's very helpful.

@sfisher
Copy link
Contributor Author

sfisher commented Aug 24, 2025

Ok. I did a round of refactoring. Have the PNG being saved from memory rather than writing to the file system and added the setting to save the metadata under the "interface" options, added translations and moved tests around and made sure they pass as well as linting with ruff, etc.

It all seems to work fine to me. Give it a try and see what you think.

I decided not to make full PNG writing code from scratch since there is already code to create PNGs that handles any extra complications about different color formats, compression, etc, and figured it was easier to leave it alone to just add the headers needed for the metadata.

Copy link
Owner

@Acly Acly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good, I have a few smaller suggestions, then this should be ready

@sfisher
Copy link
Contributor Author

sfisher commented Aug 26, 2025

This is not something you need to worry about. The default is off but can be changed to suit a user's preferences. There are both reasons to want and reasons not to want to export this, which I understand.

Personally, I may create other variations and different prompts that may not be saved within the KRZ file and it's useful for me to be able to track the generations I export. Other people enjoy sharing some of their process or have other reasons to want or not want this.

I also have additional workflows (scaling/image enhancement) where this information is not passed along.

Be aware this ONLY applies to saving a generated png directly from the generation thumbnails and not exporting through the Krita File > Export menu which is maybe what you're doing if you're spending weeks. It also follows the preference (off is default) as mentioned above which does not change the current behavior unless changed by the user.

@sfisher
Copy link
Contributor Author

sfisher commented Aug 26, 2025

I believe this is ready since I made those changes, fixed a test and everything is still working afaict. Thanks. This will be nice to have the option without having to maintain my own branch or other things.

@Streamtabulous
Copy link

i certainly don't want my meta in my images, I'll spend a week on some prompt tuning etc and the output image is a passion of time and love, iv had works from other output taken and passed of as there own. that's the worst feeling in the world.

if implemented the default should be off, for myself i save the .krz for work flows and data information.
i don't want it in png, jpg, etc

@Streamtabulous
Copy link

This is not something you need to worry about. The default is off but can be changed to suit a user's preferences. There are both reasons to want and reasons not to want to export this, which I understand.

Personally, I may create other variations and different prompts that may not be saved within the KRZ file and it's useful for me to be able to track the generations I export. Other people enjoy sharing some of their process or have other reasons to want or not want this.

I also have additional workflows (scaling/image enhancement) where this information is not passed along.

Be aware this ONLY applies to saving a generated png directly from the generation thumbnails and not exporting through the Krita File > Export menu which is maybe what you're doing if you're spending weeks. It also follows the preference (off is default) as mentioned above which does not change the current behavior unless changed by the user.

thanks, where does right clicking the generated thumb and saving it go to, i saved but can't find it to test it, iv been away for a year lot of new stuff

@sfisher
Copy link
Contributor Author

sfisher commented Aug 26, 2025

When saving generated images from the thumbnails they are saved in the same directory where the original file is saved. I believe the option is grayed out if the file is not saved anywhere (such as a new image not saved yet).

@Acly Acly merged commit 0977a72 into Acly:main Aug 26, 2025
2 checks passed
@Acly
Copy link
Owner

Acly commented Aug 26, 2025

Thanks, works fine for me

One thing I noticed is that it starts metadata with "Prompt: ", whereas A1111 starts with the actual prompt directly (at least some 1+year old images I had lying around do). It doesn't particularly matter to me, but maybe it won't be read correctly in some viewers.

@sfisher
Copy link
Contributor Author

sfisher commented Aug 27, 2025

Thank you for catching that. I could do a tiny PR to change that or you're welcome to change it if you'd rather.

Thanks for your help!

@Acly
Copy link
Owner

Acly commented Aug 27, 2025

changed it: 11cd888

@nolan778
Copy link
Contributor

Was it expected that this would not include the style influence on the metadata saved to the PNG? Seems like all that is missing, meaning the workflow opened in ComfyUI using the image cannot reproduce Krita AI Diffusion images.

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

Successfully merging this pull request may close these issues.

4 participants