Python imageio.imsave() Examples

The following are 30 code examples of imageio.imsave(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module imageio , or try the search function .
Example #1
Source File: collected_dataset.py    From NeuralSceneDecomposition with GNU General Public License v3.0 6 votes vote down vote up
def computeAndCacheBG(self, seqi, cami):
        bg_file_name = self.getBackgroundName(seqi, cami)
        bg_path = '/'.join(bg_file_name.split('/')[:-1])

        num_samples = 50
        import os
        names = [os.path.join(bg_path, file) for file in os.listdir(bg_path)]
        names_subsampled = names[0::len(names)//num_samples]
        image_batch = [np.array(imageio.imread(name), dtype='float32') for name in names_subsampled]
        image_batch = np.array(image_batch)
        print("Computing median of {} images".format(len(image_batch)))
        image_median = np.median(image_batch, axis=0)
        imageio.imsave(bg_file_name, image_median)
        print("Saved background image to {}".format(bg_file_name))

# training: 87395
# validation:
# testing: 28400 
Example #2
Source File: visualization_examples.py    From habitat-api with MIT License 6 votes vote down vote up
def example_pointnav_draw_target_birdseye_view():
    goal_radius = 0.5
    goal = NavigationGoal(position=[10, 0.25, 10], radius=goal_radius)
    agent_position = np.array([0, 0.25, 0])
    agent_rotation = -np.pi / 4

    dummy_episode = NavigationEpisode(
        goals=[goal],
        episode_id="dummy_id",
        scene_id="dummy_scene",
        start_position=agent_position,
        start_rotation=agent_rotation,
    )
    target_image = maps.pointnav_draw_target_birdseye_view(
        agent_position,
        agent_rotation,
        np.asarray(dummy_episode.goals[0].position),
        goal_radius=dummy_episode.goals[0].radius,
        agent_radius_px=25,
    )

    imageio.imsave(
        os.path.join(IMAGE_DIR, "pointnav_target_image.png"), target_image
    ) 
Example #3
Source File: utils.py    From dhSegment with GNU General Public License v3.0 6 votes vote down vote up
def save_and_resize(img: np.array,
                    filename: str,
                    size=None,
                    nearest: bool=False) -> None:
    """
    Resizes the image if necessary and saves it. The resizing will keep the image ratio

    :param img: the image to resize and save (numpy array)
    :param filename: filename of the saved image
    :param size: size of the image after resizing (in pixels). The ratio of the original image will be kept
    :param nearest: whether to use nearest interpolation method (default to False)
    :return:
    """
    if size is not None:
        h, w = img.shape[:2]
        ratio = float(np.sqrt(size/(h*w)))
        resized = cv2.resize(img, (int(w*ratio), int(h*ratio)),
                             interpolation=cv2.INTER_NEAREST if nearest else cv2.INTER_LINEAR)
        imsave(filename, resized)
    else:
        imsave(filename, img) 
Example #4
Source File: image2.py    From ASR33 with MIT License 6 votes vote down vote up
def main(filename, width, invert, gamma, indent, chars1, chars2, title, output):
    # Aspect ratio is determined by the input image.
    # Width is determined here.
    img = load_image(filename, width, invert, gamma)
    # imageio.imsave("test.jpg", img)

    # Analyze the image
    hog_fd = process(img)

    if title:
        # title is a string
        # center it, and make bytes
        title = " " * int(indent + (width - len(title))/2) + title
        title = title.encode("utf-8")

    # Map to ASCII
    if not output:
        output = filename + ".txt"
    render(hog_fd, output, chars1, chars2, indent, title) 
Example #5
Source File: cluster_images.py    From NucleiDetectron with Apache License 2.0 6 votes vote down vote up
def visualize_clusters_on_disk(img_df):
    CLUSTER_FOLDER = 'clusters30_agg_avg'

    os.mkdir((ROOT_DIR / 'clustering' / CLUSTER_FOLDER).as_posix())

    for i in range(-1, NUM_CLUSTERS + 1):
        os.mkdir((ROOT_DIR / 'clustering' / CLUSTER_FOLDER / str(i)).as_posix())

    img_df.apply(lambda x:
                 imageio.imsave((ROOT_DIR / 'clustering' / CLUSTER_FOLDER / x['cluster-id'] /
                                 (x['ImageId'])).as_posix(), x['images']), axis=1)

    counts = img_df.groupby(['cluster-id']).size().sort_values(ascending=False)
    print(counts)

    C = list(counts.items())
    C_Sorted = sorted(C, key=lambda x: x[1]) 
Example #6
Source File: image.py    From MANet_for_Video_Object_Detection with Apache License 2.0 6 votes vote down vote up
def check_movements(ims, bef_ims, aft_ims, processed_roidb, delta_bef_roi, delta_aft_roi):
    save_name = '/home/wangshiyao/Documents/testdata/'+processed_roidb[0]['image'].split('/')[-1]
    print 'saving images to '+save_name
    boxes = processed_roidb[0]['boxes']
    ims.squeeze().transpose(1, 2, 0).astype(np.int8)
    bef_ims.squeeze().transpose(1, 2, 0).astype(np.int8)
    aft_ims.squeeze().transpose(1, 2, 0).astype(np.int8)
    delta_bef_roi = np.array(delta_bef_roi).transpose(1, 0, 2)
    delta_aft_roi = np.array(delta_aft_roi).transpose(1, 0, 2)
    for i in range(boxes.shape[0]):
        cv2.rectangle(ims, (int(boxes[i][0]), int(boxes[i][1])),(int(boxes[i][2]), int(boxes[i][3])),(55, 255, 155),5)
        bef_box = bbox_pred(boxes[i].reshape(1, -1), delta_bef_roi[i])
        cv2.rectangle(bef_ims, (int(bef_box[0][0]), int(bef_box[0][1])),(int(bef_box[0][2]), int(bef_box[0][3])),(55, 255, 155),5)
        aft_box = bbox_pred(boxes[i].reshape(1, -1), delta_aft_roi[i])
        cv2.rectangle(aft_ims, (int(aft_box[0][0]), int(aft_box[0][1])),(int(aft_box[0][2]), int(aft_box[0][3])),(55, 255, 155),5)

    imageio.imsave(save_name, ims)
    imageio.imsave(save_name.split('.')[-2]+'_bef'+'.JPEG', bef_ims)
    imageio.imsave(save_name.split('.')[-2]+'_aft'+'.JPEG', aft_ims) 
Example #7
Source File: rgb2label.py    From deepglobe_land_cover_classification_with_deeplabv3plus with MIT License 6 votes vote down vote up
def color2annotation(input_path, output_path):

    # image = scipy.misc.imread(input_path)
    # imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead.
    image = imageio.imread(input_path)
    image = (image >= 128).astype(np.uint8)
    image = 4 * image[:, :, 0] + 2 * image[:, :, 1] + image[:, :, 2]
    cat_image = np.zeros((2448,2448), dtype=np.uint8)
    cat_image[image == 3] = 0  # (Cyan: 011) Urban land
    cat_image[image == 6] = 1  # (Yellow: 110) Agriculture land
    cat_image[image == 5] = 2  # (Purple: 101) Rangeland
    cat_image[image == 2] = 3  # (Green: 010) Forest land
    cat_image[image == 1] = 4  # (Blue: 001) Water
    cat_image[image == 7] = 5  # (White: 111) Barren land
    cat_image[image == 0] = 6  # (Black: 000) Unknown


    # scipy.misc.imsave(output_path, cat_image)
    imageio.imsave(output_path, cat_image)
    pass 
Example #8
Source File: request_model.py    From Deep-Learning-Tinder with MIT License 6 votes vote down vote up
def call_model(image, api_host='', model_id=''):
    tmp_filename = str(uuid.uuid4()) + '.png'
    imageio.imsave(tmp_filename, image)

    path = '/models/images/classification/classify_one.json'
    files = {'image_file': open(tmp_filename, 'rb')}

    try:
        r = post(api_host + path, files=files, params={'job_id': model_id})
    finally:
        os.remove(tmp_filename)
        time.sleep(2)  # wait 2 seconds.

    result = r.json()
    if result.get('error'):
        raise Exception(result.get('error').get('description'))

    for res_element in result['predictions']:
        if 'LIKE' in res_element[0]:
            print(result)
            return res_element[1]
    return 0.0 
Example #9
Source File: io.py    From napari with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def imsave(filename: str, data: np.ndarray):
    """Custom implementation of imsave to avoid skimage dependency.

    Parameters
    ----------
    filename : string
        The path to write the file to.
    data : np.ndarray
        The image data.
    """
    ext = os.path.splitext(filename)[1]
    if ext in [".tif", ".tiff"]:
        import tifffile

        tifffile.imsave(filename, data)
    else:
        import imageio

        imageio.imsave(filename, data) 
Example #10
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #11
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #12
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #13
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #14
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #15
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #16
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #17
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #18
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #19
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #20
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #21
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #22
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #23
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #24
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #25
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #26
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #27
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #28
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #29
Source File: utility.py    From U-Net-Fixed-Point-Quantization-for-Medical-Image-Segmentation with MIT License 5 votes vote down vote up
def save_image_to_path(img, image_name, path):
    # import ipdb as pdb; pdb.set_trace()
    if not os.path.exists(path):
        print ("Path {0} does not exist".format(path))
        try:  
            os.mkdir(path)
        except OSError:  
            print ("Creation of the directory {0} failed".format(path))
        else:  
            print ("Successfully created the directory {0}".format(path))
    path = path+image_name
    img = np.asarray( img, dtype="uint8" )
    imageio.imsave(path, img, "PNG") 
Example #30
Source File: image.py    From catalyst with Apache License 2.0 5 votes vote down vote up
def imsave(**kwargs):
    """
    ``imwrite(uri, im, format=None, **kwargs)``

    Write an image to the specified file.
    Alias for ``imageio.imsave``.

    Args:
        **kwargs: parameters for ``imageio.imsave``

    Returns:
        image save result
    """
    return imageio.imsave(**kwargs)