File size: 2,810 Bytes
f2dbf59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
import os
import time
import numpy as np
from PIL import Image, ImageSequence, ImageOps

#from load_node import load_image, pil2tensor

def save_image(img, filepath, format, quality):
    try:
        if format in ["jpg", "jpeg"]:
            img.convert("RGB").save(filepath, format="JPEG", quality=quality, subsampling=0)
        elif format == "webp":
            img.save(filepath, format="WEBP", quality=quality, method=6)
        elif format == "bmp":
            img.save(filepath, format="BMP")
        else:
            img.save(filepath, format="PNG", optimize=True)
    except Exception as e:
        print(f"Error saving {filepath}: {str(e)}")
        
class SaveImageNode:
    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "images": ("IMAGE",),
                "path": ("STRING", {"multiline": True, "dynamicPrompts": False}),
                "quality": ([100, 95, 90, 85, 80, 75, 70, 60, 50], {"default": 100}),
            }
        }
    RETURN_TYPES = ()
    FUNCTION = "save_image"
    CATEGORY = "tbox/Image"
    OUTPUT_NODE = True
    
    def save_image(self, images, path, quality):
        filepaht = path.split('\n')[0]
        format = os.path.splitext(filepaht)[1][1:]
        image = images[0] 
        img = Image.fromarray((255. * image.cpu().numpy()).astype(np.uint8))
        save_image(img, filepaht, format, quality)
        return {}
        
class SaveImagesNode:
    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "images": ("IMAGE",),
                "path": ("STRING", {"multiline": False, "dynamicPrompts": False}),
                "prefix": ("STRING", {"default": "image"}),
                "format": (["PNG", "JPG", "WEBP", "BMP"],),
                "quality": ([100, 95, 90, 85, 80, 75, 70, 60, 50], {"default": 100}),
            }
        }
    RETURN_TYPES = ()
    FUNCTION = "save_image"
    CATEGORY = "tbox/Image"
    OUTPUT_NODE = True
    
    def save_image(self, images, path, prefix, format, quality):
        format = format.lower()            
        for i, image in enumerate(images):
            img = Image.fromarray((255. * image.cpu().numpy()).astype(np.uint8))
            filepath = self.generate_filename(path, prefix, i, format)
            save_image(img, filepath, format, quality)
        return {}

    def IS_CHANGED(s, images):
        return time.time()
    
    def generate_filename(self, save_dir, prefix, index, format):
        base_filename = f"{prefix}_{index+1}.{format}"
        filename = os.path.join(save_dir, base_filename)
        counter = 1
        while os.path.exists(filename):
            filename = os.path.join(save_dir, f"{prefix}_{index+1}_{counter}.{format}")
            counter += 1
        return filename