-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathimage_to_base64_node.py
More file actions
43 lines (34 loc) · 1.02 KB
/
image_to_base64_node.py
File metadata and controls
43 lines (34 loc) · 1.02 KB
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
import io
import base64
import torch
import numpy as np
from PIL import Image
class ImageToBase64Node:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "convert_image_to_base64"
CATEGORY = "RequestNode/Converters"
def convert_image_to_base64(self, image):
# Convert tensor to PIL Image
i = 255. * image.cpu().numpy().squeeze()
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
# Save image to a byte buffer
buffer = io.BytesIO()
img.save(buffer, format="PNG")
# Encode the byte buffer to a base64 string
base64_string = base64.b64encode(buffer.getvalue()).decode('utf-8')
return (base64_string,)
NODE_CLASS_MAPPINGS = {
"ImageToBase64Node": ImageToBase64Node
}
NODE_DISPLAY_NAME_MAPPINGS = {
"ImageToBase64Node": "Image to Base64 Node"
}