Introducing Gradio Clients
WatchIntroducing Gradio Clients
WatchNew to Gradio? Start here: Getting Started
See the Release History
gradio.Image(···)
numpy.array
, PIL.Image
or str
filepath depending on type
. For SVGs, the type
parameter is ignored and the filepath of the SVG is returned.def predict(
value: np.ndarray | PIL.Image.Image | str | None
)
...
numpy.array
, PIL.Image
, or str
or pathlib.Path
filepath to an image which is displayed.def predict(···) -> np.ndarray | PIL.Image.Image | str | Path | None
...
return value
Class | Interface String Shortcut | Initialization |
---|---|---|
| "image" | Uses default values |
GIF
and SVG
Image FormatsThe gr.Image
component can process or display any image format that is supported by the PIL library, including animated GIFs. In addition, it also supports the SVG image format.
When the gr.Image
component is used as an input component, the image is converted into a str
filepath, a PIL.Image
object, or a numpy.array
, depending on the type
parameter. However, animated GIF and SVG images are treated differently:
GIF
images can only be converted to str
filepaths or PIL.Image
objects. If they are converted to a numpy.array
(which is the default behavior), only the first frame will be used. So if your demo expects an input GIF
image, make sure to set the type
parameter accordingly, e.g.import gradio as gr
demo = gr.Interface(
fn=lambda x:x,
inputs=gr.Image(type="filepath"),
outputs=gr.Image()
)
demo.launch()
SVG
images, the type
parameter is ignored altogether and the image is always returned as an image filepath. This is because SVG
images cannot be processed as PIL.Image
or numpy.array
objects.import numpy as np
import gradio as gr
def sepia(input_img):
sepia_filter = np.array([
[0.393, 0.769, 0.189],
[0.349, 0.686, 0.168],
[0.272, 0.534, 0.131]
])
sepia_img = input_img.dot(sepia_filter.T)
sepia_img /= sepia_img.max()
return sepia_img
demo = gr.Interface(sepia, gr.Image(), "image")
if __name__ == "__main__":
demo.launch()
Event listeners allow you to respond to user interactions with the UI components you've defined in a Gradio Blocks app. When a user interacts with an element, such as changing a slider value or uploading an image, a function is called.
The Image component supports the following event listeners. Each event listener takes the same parameters, which are listed in the Event Parameters table below.
Listener | Description |
---|---|
| This listener is triggered when the user clears the Image using the X button for the component. |
| Triggered when the value of the Image changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See |
| This listener is triggered when the user streams the Image. |
| Event listener for when the user selects or deselects the Image. Uses event data gradio.SelectData to carry |
| This listener is triggered when the user uploads a file into the Image. |
| This listener is triggered when the user changes the value of the Image. |