| 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( |
| label="Prompt", |
| value="A cheetah in the grassy savanna.", |
| lines=3, |
| ), |
| "height": 1024, |
| "width": 1024, |
| "seed": random.random, |
| }, |
| outputs={ |
| "image": gr.Image( |
| label="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"), |
| }, |
| ) |
|
|
|
|
| def crop_alpha(image: Image) -> Image: |
| """crops image keep only the RGB channels""" |
|
|
| |
| image = load_img(image).convert("RGBA") |
| bbox = image.getbbox(alpha_only=True) |
| image = image.crop(bbox) |
| |
| 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() |
|
|