Skip to content

Commit

Permalink
feat: ✨ add Unsplash Image node
Browse files Browse the repository at this point in the history
  • Loading branch information
melMass committed Jul 23, 2023
1 parent abf1e82 commit 8d3cc39
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions nodes/fun.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import qrcode
from ..utils import pil2tensor
from PIL import Image
from ..log import log

# class MtbExamples:
# """MTB Example Images"""
Expand Down Expand Up @@ -50,6 +51,57 @@
# return m.digest().hex()


class UnsplashImage:
"""Unsplash Image given a keyword and a size"""

@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"width": ("INT", {"default": 512, "max": 8096, "min": 0, "step": 1}),
"height": ("INT", {"default": 512, "max": 8096, "min": 0, "step": 1}),
"random_seed": ("INT", {"default": 0, "max": 1e5, "min": 0, "step": 1}),
},
"optional": {
"keyword": ("STRING", {"default": "nature"}),
},
}

RETURN_TYPES = ("IMAGE",)
FUNCTION = "do_unsplash_image"
CATEGORY = "mtb/generate"

def do_unsplash_image(self, width, height, random_seed, keyword=None):
import requests
import io

base_url = "https://source.unsplash.com/random/"

if width and height:
base_url += f"/{width}x{height}"

if keyword:
keyword = keyword.replace(" ", "%20")
base_url += f"?{keyword}&{random_seed}"
else:
base_url += f"?&{random_seed}"
try:
log.debug(f"Getting unsplash image from {base_url}")
response = requests.get(base_url)
response.raise_for_status()

image = Image.open(io.BytesIO(response.content))
return (
pil2tensor(
image,
),
)

except requests.exceptions.RequestException as e:
print("Error retrieving image:", e)
return (None,)


class QrCode:
"""Basic QR Code generator"""

Expand Down Expand Up @@ -112,5 +164,6 @@ def do_qr(self, url, width, height, error_correct, box_size, border, invert):

__nodes__ = [
QrCode,
UnsplashImage
# MtbExamples,
]

0 comments on commit 8d3cc39

Please sign in to comment.