File size: 1,762 Bytes
70bfe23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random

import gradio as gr
from PIL.Image import Image
from loadimg import load_img

from daggr import GradioNode, Graph, FnNode

glm_image = GradioNode(
    "hf-applications/Z-Image-Turbo",
    api_name="/generate_image",
    inputs={
        "prompt": gr.Textbox(  # An input node is created for the prompt
            label="Prompt",
            value="A cheetah in the grassy savanna.",
            lines=3,
        ),
        "height": 1024,  # Fixed value (does not appear in the canvas)
        "width": 1024,  # Fixed value (does not appear in the canvas)
        "seed": random.random,  # Functions are rerun every time the workflow is run (not shown in the canvas)
    },
    outputs={
        "image": gr.Image(
            label="Image"  # Display original image
        ),
    },
)

background_remover = GradioNode(
    "hf-applications/background-removal",
    api_name="/image",
    inputs={
        "image": glm_image.image,
    },
    postprocess=lambda _, final: final,
    outputs={
        "image": gr.Image(label="Final Image"),  # Display only final image
    },
)


def crop_alpha(image: Image) -> Image:
    """crops image keep only the RGB channels"""

    # convert from str to PIL Image
    image = load_img(image).convert("RGBA")
    bbox = image.getbbox(alpha_only=True)
    image = image.crop(bbox)
    # store as str and pass as path
    return load_img(image, output_type="str")


cropper = FnNode(
    fn=crop_alpha,
    inputs={
        "image": background_remover.image,
    },
    outputs={
        "image": gr.Image(label="crops image to fit by removing alpha border"),
    },
)


graph = Graph(
    name="Transparent Background Image Generator",
    nodes=[glm_image, background_remover, cropper],
)

graph.launch()