def generate_image()

in ludwig/data/dataset_synthesizer.py [0:0]


def generate_image(feature):
    try:
        from skimage.io import imsave
    except ImportError:
        logger.error(
            ' scikit-image is not installed. '
            'In order to install all image feature dependencies run '
            'pip install ludwig[image]'
        )
        sys.exit(-1)

    # Read num_channels, width, height
    num_channels = feature['preprocessing']['num_channels']
    width = feature['preprocessing']['width']
    height = feature['preprocessing']['height']
    image_dest_folder = feature['destination_folder']

    if width <= 0 or height <= 0 or num_channels < 1:
        raise ValueError('Invalid arguments for generating images')

    # Create a Random Image
    if num_channels == 1:
        img = np.random.rand(width, height) * 255
    else:
        img = np.random.rand(width, height, num_channels) * 255.0

    # Generate a unique random filename
    image_filename = uuid.uuid4().hex[:10].upper() + '.jpg'

    # Save the image to disk either in a specified location/new folder
    try:
        if not os.path.exists(image_dest_folder):
            os.makedirs(image_dest_folder)

        image_dest_path = os.path.join(image_dest_folder, image_filename)
        imsave(image_dest_path, img.astype('uint8'))

    except IOError as e:
        raise IOError('Unable to create a folder for images/save image to disk.'
                      '{0}'.format(e))

    return image_dest_path