-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimgbb_uploader.py
73 lines (58 loc) · 2.22 KB
/
imgbb_uploader.py
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
# imgbb_uploader.py
import requests
import base64
from PIL import Image
import io
import numpy as np
class ImgBBUploadJPG:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"api_key": ("STRING", {"default": "", "multiline": False}),
"jpeg_quality": ("INT", {"default": 90, "min": 1, "max": 100, "step": 1}),
"expire": ("BOOLEAN", {"default": False}),
"expiration_time": (
"INT",
{"default": 60, "min": 60, "max": 15552000, "step": 1},
),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("image_url",)
FUNCTION = "upload"
CATEGORY = "image/upload"
def upload(self, image, api_key, jpeg_quality, expire, expiration_time):
"""
Upload an image to ImgBB in JPG format and return the URL.
"""
if not api_key:
raise ValueError("API Key is required")
# Convert image tensor to PIL Image
img = image[0]
img = 255.0 * img.cpu().numpy()
img = Image.fromarray(np.clip(img, 0, 255).astype(np.uint8))
# Save PIL Image to buffer as JPEG
buffer = io.BytesIO()
img.save(buffer, format="JPEG", quality=jpeg_quality)
buffer.seek(0)
base64_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
url = f"https://api.imgbb.com/1/upload?key={api_key}"
if expire:
url += f"&expiration={expiration_time}"
payload = {
"image": base64_image,
}
try:
response = requests.post(url, data=payload)
result = response.json()
if result.get("success"):
return (result["data"]["url"],)
else:
error_message = result.get("error", {}).get("message", "Unknown error")
raise ValueError(f"Error: {error_message}")
except Exception as e:
raise ValueError(f"Error: {str(e)}")
NODE_CLASS_MAPPINGS = {"ImgBBUploadJPG": ImgBBUploadJPG}
NODE_DISPLAY_NAME_MAPPINGS = {"ImgBBUploadJPG": "Upload to ImgBB (JPG)"}