This commit is contained in:
Ali Abid 2020-08-16 15:01:29 -07:00
commit d8a00eca55

View File

@ -241,11 +241,12 @@ class Image(InputComponent):
Input type: Union[numpy.array, PIL.Image, str]
"""
def __init__(self, shape=None, image_mode='RGB', source="upload", type="numpy", label=None):
def __init__(self, shape=None, image_mode='RGB', invert_colors=False, source="upload", type="numpy", label=None):
'''
Parameters:
shape (Tuple[int, int]): shape to crop and resize image to; if None, matches input image size.
image_mode (str): "RGB" if color, or "L" if black and white.
invert_colors (bool): whether to invert the image as a preprocessing step.
source (str): Source of image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "canvas" defaults to a white image that can be edited and drawn upon with tools.
type (str): Type of value to be returned by component. "numpy" returns a numpy array with shape (width, height, 3), "pil" returns a PIL image object, "file" returns a temporary file object whose path can be retrieved by file_obj.name.
label (str): component name in interface.
@ -254,6 +255,7 @@ class Image(InputComponent):
self.image_mode = image_mode
self.source = source
self.type = type
self.invert_colors = invert_colors
super().__init__(label)
@classmethod
@ -261,7 +263,7 @@ class Image(InputComponent):
return {
"image": {},
"webcam": {"source": "webcam"},
"sketchpad": {"image_mode": "L", "source": "canvas"},
"sketchpad": {"image_mode": "L", "source": "canvas", "shape": (28, 28), "invert_colors": True},
}
def get_template_context(self):
@ -279,6 +281,8 @@ class Image(InputComponent):
if self.shape is not None:
im = processing_utils.resize_and_crop(
im, (self.shape[0], self.shape[1]))
if self.invert_colors:
im = PIL.ImageOps.invert(im)
if self.type == "pil":
return im
elif self.type == "numpy":