How to access or save the image generated by gradio_client on python

47 views Asked by At
from gradio_client import Client
    
client = Client("ByteDance/SDXL-Lightning")
result = client.predict(
        "astronaut riding a horse realistic",   
        "8-Step",   
        api_name="/generate_image"
)
print(result)

This is my code, it returns me a path in the form of string, so I can't do anything with it:

/tmp/gradio/ecfea853d51fb542f45865d58816480210e58ea3/image.png

How do I display it or save it?

I tried giving download_files = True in Client parameters but didn't see a difference.

2

There are 2 answers

1
Ömer Sezer On

Pls use matplotlib and PIL:

image = Image.open(result)
plt.imshow(image)
plt.axis('off')  
plt.show()
image.save("output_image.png")

Sample code:

from gradio_client import Client
from PIL import Image
import matplotlib.pyplot as plt

client = Client("ByteDance/SDXL-Lightning")
result = client.predict(
        "astronaut riding a horse realistic",   
        "8-Step",   
        api_name="/generate_image"
)

image = Image.open(result)
plt.imshow(image)
plt.axis('off')  
plt.show()
image.save("output_image.png")

Output:

enter image description here

0
LoGo124 On

I think is eassier if u use this skimage

from gradio_client import Client

#Package for image display
from skimage import io

#Package for save the image
import os

client = Client("ByteDance/SDXL-Lightning")
result = client.predict(
        "astronaut riding a horse realistic",   
        "8-Step",   
        api_name="/generate_image"
)

#Move image from temporal place to desired destination
new_path = "./image.png"
os.replace(result, new_path)

#Show the image
img = io.imread(new_path)
io.imshow(img)
io.show()