import numpy as np from PIL import Image from image_prediction.info import Info def random_single_color_image_from_metadata(metadata): image = Image.new( "RGB", (metadata[Info.WIDTH], metadata[Info.HEIGHT]), color=tuple(map(int, np.random.uniform(size=3) * 255)) ) return image def gray_image_from_metadata(metadata): image = Image.new("RGB", (metadata[Info.WIDTH], metadata[Info.HEIGHT]), color=(100, 100, 100)) return image def array_to_image(array): assert np.all(array <= 1) assert np.all(array >= 0) if array.shape[-1] == 3: mode = "RGB" elif array.shape[-1] == 4: mode = "RGBA" else: raise ValueError(f"Unexpected number of channels {array.shape[-1]}. Expected 3 or 4.") # noinspection PyTypeChecker return Image.fromarray(np.uint8(array * 255), mode=mode)