Created
August 16, 2023 15:38
-
-
Save cpietsch/0b8731f304980b17bdd26e352707a847 to your computer and use it in GitHub Desktop.
Here we convert all the automatic1111 pngs into jpgs and add the custom metadata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from PIL import Image | |
from PIL import ExifTags | |
import glob | |
def convert_png_to_jpg(png_path, jpg_path): | |
png_files = glob.glob(os.path.join(png_path, '**/*.png'), recursive=True) #[:1] | |
# print(png_files) | |
for png_file in png_files: | |
relative_path = os.path.relpath(png_file, png_path) | |
output_folder = os.path.join(jpg_path, os.path.dirname(relative_path)) | |
os.makedirs(output_folder, exist_ok=True) | |
jpg_file = os.path.join(output_folder, os.path.splitext(os.path.basename(png_file))[0] + '.jpg') | |
img = Image.open(png_file) | |
img = img.convert('RGB') | |
parameters = img.info.get("parameters", "") | |
exif_ifd = {piexif.ExifIFD.UserComment: parameters.encode()} | |
exif_dict = {"0th": {}, "Exif": exif_ifd, "1st": {}, | |
"thumbnail": None, "GPS": {}} | |
exif_dat = piexif.dump(exif_dict) | |
img.save(jpg_file, format='JPEG', exif=exif_dat) | |
print(f"Converted: {jpg_file}") | |
os.remove(png_file) | |
print(f"Removed: {png_file}") | |
# Set the input and output paths as relative paths | |
input_folder = '/home/jovyan/stable-diffusion-webui/outputs/txt2img-images/' | |
output_folder = '/home/jovyan/stable-diffusion-webui/outputs/txt2img-images-jpg/' | |
convert_png_to_jpg(input_folder, output_folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment