Add a 'None' option to the gradio.Image component to disable image_m… (#9225)

* Add a 'None' option to the gradio.Image component to disable image_mode conversion behavior

* Set None option to None Keyword

* add changeset

* changes

* format

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
SK 2024-08-31 05:54:11 +08:00 committed by GitHub
parent 284fafb215
commit 5f2e047c2c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": minor
---
feat: Add a 'None' option to the gradio.Image component to disable image_m…

View File

@ -52,7 +52,8 @@ class Image(StreamingInput, Component):
width: int | str | None = None,
image_mode: Literal[
"1", "L", "P", "RGB", "RGBA", "CMYK", "YCbCr", "LAB", "HSV", "I", "F"
] = "RGB",
]
| None = "RGB",
sources: list[Literal["upload", "webcam", "clipboard"]]
| Literal["upload", "webcam", "clipboard"]
| None = None,
@ -83,7 +84,7 @@ class Image(StreamingInput, Component):
format: File format (e.g. "png" or "gif") to save image if it does not already have a valid format (e.g. if the image is being returned to the frontend as a numpy array or PIL Image). The format should be supported by the PIL library. This parameter has no effect on SVG files.
height: The height of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.
width: The width of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.
image_mode: "RGB" if color, or "L" if black and white. See https://pillow.readthedocs.io/en/stable/handbook/concepts.html for other supported image modes and their meaning. This parameter has no effect on SVG or GIF files.
image_mode: "RGB" if color, or "L" if black and white. See https://pillow.readthedocs.io/en/stable/handbook/concepts.html for other supported image modes and their meaning. This parameter has no effect on SVG or GIF files. If set to None, the image_mode will be inferred from the image file.
sources: List of sources for the image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "clipboard" allows users to paste an image from the clipboard. If None, defaults to ["upload", "webcam", "clipboard"] if streaming is False, otherwise defaults to ["webcam"].
type: The format the image is converted before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image. If the image is SVG, the `type` is ignored and the filepath of the SVG is returned. To support animated GIFs in input, the `type` should be set to "filepath" or "pil".
label: The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.
@ -199,7 +200,8 @@ class Image(StreamingInput, Component):
if suffix.lower() != "gif" and im is not None:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
im = im.convert(self.image_mode)
if self.image_mode is not None:
im = im.convert(self.image_mode)
return image_utils.format_image(
im,
cast(Literal["numpy", "pil", "filepath"], self.type),