


def 			UpperCAmelCase_				(      UpperCAmelCase__						,							UpperCAmelCase__  ):
					lowercase_  							=			[1]
					for i in range(2						,							UpperCAmelCase__  ):
										factorials.append(factorials[-1] * i  )
					assert 0 <= k < factorials[-1] * n, "k out of bounds"

					lowercase_  							=			[]
					lowercase_  							=			list(range(UpperCAmelCase__  )  )

					# Find permutation
					while factorials:
										lowercase_  							=			factorials.pop()
										lowercase_							,					lowercase_  							=			divmod(UpperCAmelCase__						,							UpperCAmelCase__  )
										permutation.append(elements[number]  )
										elements.remove(elements[number]  )
					permutation.append(elements[0]  )

					return permutation


if __name__ == "__main__":
							import doctest

							doctest.testmod()






import PIL.Image
import PIL.ImageOps
from packaging import version
from PIL import Image


if version.parse(version.parse(PIL.__version__).base_version) >= version.parse('9.1.0'):
							a					  =     {
							    'linear': PIL.Image.Resampling.BILINEAR,
							    'bilinear': PIL.Image.Resampling.BILINEAR,
							    'bicubic': PIL.Image.Resampling.BICUBIC,
							    'lanczos': PIL.Image.Resampling.LANCZOS,
							    'nearest': PIL.Image.Resampling.NEAREST,
							}
else:
							a					  =     {
							    'linear': PIL.Image.LINEAR,
							    'bilinear': PIL.Image.BILINEAR,
							    'bicubic': PIL.Image.BICUBIC,
							    'lanczos': PIL.Image.LANCZOS,
							    'nearest': PIL.Image.NEAREST,
							}


def 			UpperCAmelCase_				(      UpperCAmelCase__  ):
					lowercase_  							=			(images / 2 + 0.5).clamp(0						,							1  )
					lowercase_  							=			images.cpu().permute(0						,							2						,							3						,							1  ).float().numpy()
					lowercase_  							=			numpy_to_pil(UpperCAmelCase__  )
					return images



def 			UpperCAmelCase_				(      UpperCAmelCase__  ):
					if images.ndim == 3:
										lowercase_  							=			images[None, ...]
					lowercase_  							=			(images * 2_5_5).round().astype("""uint8"""  )
					if images.shape[-1] == 1:
										# special case for grayscale (single channel) images
										lowercase_  							=			[Image.fromarray(image.squeeze()						,							mode="""L"""  ) for image in images]
					else:
										lowercase_  							=			[Image.fromarray(UpperCAmelCase__  ) for image in images]

					return pil_images


