1313# limitations under the License.
1414
1515import warnings
16- from typing import List , Optional , Union
16+ from typing import List , Optional , Tuple , Union
1717
1818import numpy as np
1919import PIL .Image
@@ -126,14 +126,14 @@ def pt_to_numpy(images: torch.FloatTensor) -> np.ndarray:
126126 return images
127127
128128 @staticmethod
129- def normalize (images ) :
129+ def normalize (images : Union [ np . ndarray , torch . Tensor ]) -> Union [ np . ndarray , torch . Tensor ] :
130130 """
131131 Normalize an image array to [-1,1].
132132 """
133133 return 2.0 * images - 1.0
134134
135135 @staticmethod
136- def denormalize (images ) :
136+ def denormalize (images : Union [ np . ndarray , torch . Tensor ]) -> Union [ np . ndarray , torch . Tensor ] :
137137 """
138138 Denormalize an image array to [0,1].
139139 """
@@ -159,10 +159,10 @@ def convert_to_grayscale(image: PIL.Image.Image) -> PIL.Image.Image:
159159
160160 def get_default_height_width (
161161 self ,
162- image : [PIL .Image .Image , np .ndarray , torch .Tensor ],
162+ image : Union [PIL .Image .Image , np .ndarray , torch .Tensor ],
163163 height : Optional [int ] = None ,
164164 width : Optional [int ] = None ,
165- ):
165+ ) -> Tuple [ int , int ] :
166166 """
167167 This function return the height and width that are downscaled to the next integer multiple of
168168 `vae_scale_factor`.
@@ -202,12 +202,24 @@ def get_default_height_width(
202202
203203 def resize (
204204 self ,
205- image : [PIL .Image .Image , np .ndarray , torch .Tensor ],
205+ image : Union [PIL .Image .Image , np .ndarray , torch .Tensor ],
206206 height : Optional [int ] = None ,
207207 width : Optional [int ] = None ,
208- ) -> [PIL .Image .Image , np .ndarray , torch .Tensor ]:
208+ ) -> Union [PIL .Image .Image , np .ndarray , torch .Tensor ]:
209209 """
210210 Resize image.
211+
212+ Args:
213+ image (`PIL.Image.Image`, `np.ndarray` or `torch.Tensor`):
214+ The image input, can be a PIL image, numpy array or pytorch tensor.
215+ height (`int`, *optional*, defaults to `None`):
216+ The height to resize to.
217+ width (`int`, *optional*`, defaults to `None`):
218+ The width to resize to.
219+
220+ Returns:
221+ `PIL.Image.Image`, `np.ndarray` or `torch.Tensor`:
222+ The resized image.
211223 """
212224 if isinstance (image , PIL .Image .Image ):
213225 image = image .resize ((width , height ), resample = PIL_INTERPOLATION [self .config .resample ])
@@ -227,7 +239,15 @@ def resize(
227239
228240 def binarize (self , image : PIL .Image .Image ) -> PIL .Image .Image :
229241 """
230- create a mask
242+ Create a mask.
243+
244+ Args:
245+ image (`PIL.Image.Image`):
246+ The image input, should be a PIL image.
247+
248+ Returns:
249+ `PIL.Image.Image`:
250+ The binarized image. Values less than 0.5 are set to 0, values greater than 0.5 are set to 1.
231251 """
232252 image [image < 0.5 ] = 0
233253 image [image >= 0.5 ] = 1
@@ -327,7 +347,23 @@ def postprocess(
327347 image : torch .FloatTensor ,
328348 output_type : str = "pil" ,
329349 do_denormalize : Optional [List [bool ]] = None ,
330- ):
350+ ) -> Union [PIL .Image .Image , np .ndarray , torch .FloatTensor ]:
351+ """
352+ Postprocess the image output from tensor to `output_type`.
353+
354+ Args:
355+ image (`torch.FloatTensor`):
356+ The image input, should be a pytorch tensor with shape `B x C x H x W`.
357+ output_type (`str`, *optional*, defaults to `pil`):
358+ The output type of the image, can be one of `pil`, `np`, `pt`, `latent`.
359+ do_denormalize (`List[bool]`, *optional*, defaults to `None`):
360+ Whether to denormalize the image to [0,1]. If `None`, will use the value of `do_normalize` in the
361+ `VaeImageProcessor` config.
362+
363+ Returns:
364+ `PIL.Image.Image`, `np.ndarray` or `torch.FloatTensor`:
365+ The postprocessed image.
366+ """
331367 if not isinstance (image , torch .Tensor ):
332368 raise ValueError (
333369 f"Input for postprocessing is in incorrect format: { type (image )} . We only support pytorch tensor"
@@ -390,7 +426,7 @@ def __init__(
390426 super ().__init__ ()
391427
392428 @staticmethod
393- def numpy_to_pil (images ) :
429+ def numpy_to_pil (images : np . ndarray ) -> List [ PIL . Image . Image ] :
394430 """
395431 Convert a NumPy image or a batch of images to a PIL image.
396432 """
@@ -406,7 +442,7 @@ def numpy_to_pil(images):
406442 return pil_images
407443
408444 @staticmethod
409- def rgblike_to_depthmap (image ) :
445+ def rgblike_to_depthmap (image : Union [ np . ndarray , torch . Tensor ]) -> Union [ np . ndarray , torch . Tensor ] :
410446 """
411447 Args:
412448 image: RGB-like depth image
@@ -416,7 +452,7 @@ def rgblike_to_depthmap(image):
416452 """
417453 return image [:, :, 1 ] * 2 ** 8 + image [:, :, 2 ]
418454
419- def numpy_to_depth (self , images ) :
455+ def numpy_to_depth (self , images : np . ndarray ) -> List [ PIL . Image . Image ] :
420456 """
421457 Convert a NumPy depth image or a batch of images to a PIL image.
422458 """
@@ -441,7 +477,23 @@ def postprocess(
441477 image : torch .FloatTensor ,
442478 output_type : str = "pil" ,
443479 do_denormalize : Optional [List [bool ]] = None ,
444- ):
480+ ) -> Union [PIL .Image .Image , np .ndarray , torch .FloatTensor ]:
481+ """
482+ Postprocess the image output from tensor to `output_type`.
483+
484+ Args:
485+ image (`torch.FloatTensor`):
486+ The image input, should be a pytorch tensor with shape `B x C x H x W`.
487+ output_type (`str`, *optional*, defaults to `pil`):
488+ The output type of the image, can be one of `pil`, `np`, `pt`, `latent`.
489+ do_denormalize (`List[bool]`, *optional*, defaults to `None`):
490+ Whether to denormalize the image to [0,1]. If `None`, will use the value of `do_normalize` in the
491+ `VaeImageProcessor` config.
492+
493+ Returns:
494+ `PIL.Image.Image`, `np.ndarray` or `torch.FloatTensor`:
495+ The postprocessed image.
496+ """
445497 if not isinstance (image , torch .Tensor ):
446498 raise ValueError (
447499 f"Input for postprocessing is in incorrect format: { type (image )} . We only support pytorch tensor"
0 commit comments