File size: 2,387 Bytes
baa8e90 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
from PIL import Image
from PIL.PngImagePlugin import PngInfo
import numpy as np
import json
import re
import time
import os
if __name__ == os.path.splitext(os.path.basename(__file__))[0] :
from ConsoleColor import print, console
from mypath import *
else:
from .ConsoleColor import print, console
from .mypath import *
#print(__file__)
#print(os.path.basename(__file__))
class SaveImageSimple:
def __init__(self):
self.type = "output"
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"images": ("IMAGE", ),
"filename_prefix": ("STRING", {"default": ""})
},
"hidden": {
"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"
},
}
RETURN_TYPES = ()
FUNCTION = "save_images"
OUTPUT_NODE = True
CATEGORY = "image"
def save_images(self, images, filename_prefix="", prompt=None, extra_pnginfo=None):
outputdir=os.path.join(mainfolder, "output")
#print("outputdir : " + outputdir , Colors.CYAN)
#print("len(images) : " + str(len(images)) , Colors.CYAN)
filename_prefix=re.sub(r"[*]", "",filename_prefix)
filename_prefix+=time.strftime('_%Y%m%d_%H%M%S')
results = list()
cnt=1
for image in images :
i = 255. * image.cpu().numpy()
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
metadata = PngInfo()
if prompt is not None:
metadata.add_text("prompt", json.dumps(prompt))
if extra_pnginfo is not None:
#print("extra_pnginfo : " + json.dumps(extra_pnginfo) , Colors.CYAN)
for x in extra_pnginfo:
metadata.add_text(x, json.dumps(extra_pnginfo[x]))
if not os.path.exists(outputdir):
print("makedirs : " + outputdir )
os.makedirs(outputdir)
filename=filename_prefix+f"_{cnt:05}_.png"
filename=os.path.join(outputdir, filename)
img.save(filename, pnginfo=metadata, optimize=True)
results.append({
"filename": filename,
"subfolder": subfolder,
"type": self.type
});
cnt+=1
return { "ui": { "images": results } } |