welpo/png-to-pt-embedding
A standalone Python tool for converting PNG image embeddings into PyTorch .pt files for Stable Diffusion models.
PNG Embeddings to PyTorch Converter
This is a standalone Python script for converting PNG image embeddings generated by AUTOMATIC1111/stable-diffusion-webui into PyTorch .pt files.
Installation
-
Clone or download this repository to your local machine.
-
Open a terminal or command prompt and navigate to the directory containing the repository.
-
Install the required packages by running the following command:
pip install -r requirements.txtUsage
-
Navigate to the directory containing the script (alternatively, you can place the script in your system's
$PATHto run it from anywhere). -
Run the script using the following command:
python png_embedding_to_pt.py <path/to/image1.png> <path/to/image2.png> … -o <output_dir>For example:
python png_embedding_to_pt.py embeddings/1.png embeddings/2.png -o pt_embeddingsThis will save the .pt files to the pt_embeddings directory.
How it works
This script does the following:
- Loads the input PNG images
- Crops out any black borders
- Splits the least significant 4 bits of the RGB values into upper and lower data blocks, with columns of all black pixels separating them
- XORs the upper and lower data blocks with pseudo-random blocks to reveal the original data
- Combines the upper and lower blocks into a full data block
- Decompresses the data block using zlib to get the raw JSON data
- Decodes the JSON using a custom JSON decoder to convert PyTorch Tensors
- Saves the final embedding Torch Tensors to .pt files
Code adapted from AUTOMATIC1111/stable-diffusion-webui.
License
This script is licensed under the GNU General Public License version 3.