Skip to content

Instantly share code, notes, and snippets.

@hassan00dev
Last active November 29, 2024 21:01
Show Gist options
  • Select an option

  • Save hassan00dev/4ae4c8eece81b61ca698831c3027b08a to your computer and use it in GitHub Desktop.

Select an option

Save hassan00dev/4ae4c8eece81b61ca698831c3027b08a to your computer and use it in GitHub Desktop.
ComfyUI Custom Node - Upload generated output image to Firebase Storage
import numpy as np
from PIL import Image
import io
import firebase_admin
from firebase_admin import credentials, storage
from datetime import datetime
class SaveImageFirebase:
@classmethod
def INPUT_TYPES(s):
return {
"required": {"image": ("IMAGE",)}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "upload_image_to_firebase"
CATEGORY = "api/image"
OUTPUT_NODE = True
# Initialize Firebase
cred = credentials.Certificate("/home/clete/test-comfyui-firebase-adminsdk-jaeup-dd5e733146.json")
firebase_admin.initialize_app(cred, {
"storageBucket": "test-comfyui.firebasestorage.app"
})
def upload_image_to_firebase(self, image):
"""Uploads the given image to Firebase Storage and returns the URL."""
bucket = storage.bucket()
blob = bucket.blob("upload-saved/" + datetime.now().strftime("%Y%m%d%H%M%S") + "_comfyui_output.png")
# Convert image tensor to a PNG file-like object
buffered = io.BytesIO()
image = Image.fromarray(
np.clip(255.0 * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8)
)
image.save(buffered, format="PNG")
buffered.seek(0)
# Upload to Firebase
blob.upload_from_file(buffered, content_type="image/png")
blob.make_public() # Make the image publicly accessible
print("Image uploaded : ", blob.public_url)
return blob.public_url
NODE_CLASS_MAPPINGS = {"SaveImageFirebase": SaveImageFirebase}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment