示例#1
0
def StitchPatches(hdf5_file_path, downscale=16, draw_grid=False, bg_color=(0,0,0), alpha=-1):
    file = h5py.File(hdf5_file_path, 'r')
    dset = file['imgs']
    coords = file['coords'][:]
    if 'downsampled_level_dim' in dset.attrs.keys():
        w, h = dset.attrs['downsampled_level_dim']
    else:
        w, h = dset.attrs['level_dim']
    print('original size: {} x {}'.format(w, h))
    w = w // downscale
    h = h //downscale
    coords = (coords / downscale).astype(np.int32)
    print('downscaled size for stiching: {} x {}'.format(w, h))
    print('number of patches: {}'.format(len(dset)))
    img_shape = dset[0].shape
    print('patch shape: {}'.format(img_shape))
    downscaled_shape = (img_shape[1] // downscale, img_shape[0] // downscale)

    if w*h > Image.MAX_IMAGE_PIXELS: 
        raise Image.DecompressionBombError("Visualization Downscale %d is too large" % downscale)
    
    if alpha < 0 or alpha == -1:
        heatmap = Image.new(size=(w,h), mode="RGB", color=bg_color)
    else:
        heatmap = Image.new(size=(w,h), mode="RGBA", color=bg_color + (int(255 * alpha),))
    
    heatmap = np.array(heatmap)
    heatmap = DrawMap(heatmap, dset, coords, downscaled_shape, indices=None, draw_grid=draw_grid)
    
    file.close()
    return heatmap
示例#2
0
def StitchPatches(hdf5_file_path,
                  pred_path,
                  downscale=4,
                  draw_grid=False,
                  bg_color=(0, 0, 0),
                  alpha=-1):
    file = h5py.File(hdf5_file_path, 'r')
    files = []
    dset = file['imgs']
    print(len(dset))
    start_dir = pred_path
    pattern = "*.png"
    for dir, _, _ in os.walk(start_dir):
        files.extend(glob(os.path.join(dir, pattern)))
    print(len(files))
    files.sort(key=lambda f: int(re.sub('\D', '', f)))

    images = ones((len(files), 512, 512, 3))
    for i, load in enumerate(files):
        print(load)
        images[i] = (load_image(load))
    print(images[0].dtype)
    #dset=files
    coords = file['coords'][:]
    if 'downsampled_level_dim' in dset.attrs.keys():
        w, h = dset.attrs['downsampled_level_dim']
    else:
        w, h = dset.attrs['level_dim']
    print('original size: {} x {}'.format(w, h))
    w = w // downscale
    h = h // downscale
    coords = (coords / downscale).astype(np.int32)
    print('downscaled size for stiching: {} x {}'.format(w, h))
    print('number of patches: {}'.format(len(dset)))
    img_shape = dset[0].shape
    print('patch shape: {}'.format(img_shape))
    downscaled_shape = (img_shape[1] // downscale, img_shape[0] // downscale)

    if w * h > Image.MAX_IMAGE_PIXELS:
        raise Image.DecompressionBombError(
            "Visualization Downscale %d is too large" % downscale)

    if alpha < 0 or alpha == -1:
        heatmap = Image.new(size=(w, h), mode="RGB", color=bg_color)
    else:
        heatmap = Image.new(size=(w, h),
                            mode="RGBA",
                            color=bg_color + (int(255 * alpha), ))

    heatmap = np.array(heatmap)
    heatmap = DrawMap(heatmap,
                      images,
                      coords,
                      downscaled_shape,
                      indices=None,
                      draw_grid=draw_grid)

    file.close()
    return heatmap
示例#3
0
 def test_convert2png_large_image_file_security_error(self, mk_open):
     mk_open.side_effect = Image.DecompressionBombError("ERROR!")
     text_file_path = os.path.join(self.extracted_package, "file.txt")
     web_image_generator = utils.WebImageGenerator("file.txt",
                                                   self.extracted_package)
     with self.assertRaises(exceptions.WebImageGeneratorError) as exc_info:
         web_image_generator.convert2png()
     self.assertIn("Error opening image file ", str(exc_info.exception))
     self.assertIn("file.txt", str(exc_info.exception))
示例#4
0
 def test__get_image_object_large_image_file_security_error(
         self, mk_img_parser_feed):
     mk_img_parser_feed.side_effect = Image.DecompressionBombError("ERROR!")
     mocked_image_io = io.BytesIO()
     mocked_image = Image.new("RGB", (10, 10))
     mocked_image.save(mocked_image_io, "TIFF")
     web_image_generator = utils.WebImageGenerator("image_tiff_2.tiff", ".")
     with self.assertRaises(exceptions.WebImageGeneratorError) as exc_info:
         web_image_generator._get_image_object(mocked_image_io.getvalue())
     self.assertEqual(
         str(exc_info.exception),
         'Error reading image "image_tiff_2.tiff": ERROR!',
     )
示例#5
0
def StitchCoords(hdf5_file_path,
                 wsi_object,
                 downscale=16,
                 draw_grid=False,
                 bg_color=(0, 0, 0),
                 alpha=-1):
    wsi = wsi_object.getOpenSlide()
    vis_level = wsi.get_best_level_for_downsample(downscale)
    file = h5py.File(hdf5_file_path, 'r')
    dset = file['coords']
    coords = dset[:]
    w, h = wsi.level_dimensions[0]

    print('start stitching {}'.format(dset.attrs['name']))
    print('original size: {} x {}'.format(w, h))

    w, h = wsi.level_dimensions[vis_level]

    print('downscaled size for stiching: {} x {}'.format(w, h))
    print('number of patches: {}'.format(len(coords)))

    patch_size = dset.attrs['patch_size']
    patch_level = dset.attrs['patch_level']
    print('patch size: {}x{} patch level: {}'.format(patch_size, patch_size,
                                                     patch_level))
    patch_size = tuple((np.array((patch_size, patch_size)) *
                        wsi.level_downsamples[patch_level]).astype(np.int32))
    print('ref patch size: {}x{}'.format(patch_size, patch_size))

    if w * h > Image.MAX_IMAGE_PIXELS:
        raise Image.DecompressionBombError(
            "Visualization Downscale %d is too large" % downscale)

    if alpha < 0 or alpha == -1:
        heatmap = Image.new(size=(w, h), mode="RGB", color=bg_color)
    else:
        heatmap = Image.new(size=(w, h),
                            mode="RGBA",
                            color=bg_color + (int(255 * alpha), ))

    heatmap = np.array(heatmap)
    heatmap = DrawMapFromCoords(heatmap,
                                wsi_object,
                                coords,
                                patch_size,
                                vis_level,
                                indices=None,
                                draw_grid=draw_grid)

    file.close()
    return heatmap
示例#6
0
    def test_create_thumbnail_large_image_file_security_error(self, mk_open):
        mk_open.side_effect = Image.DecompressionBombError("ERROR!")
        text_file_path = os.path.join(self.extracted_package, "file.txt")
        with open(text_file_path, "w") as fp:
            fp.write("Text file content.")

        web_image_generator = utils.WebImageGenerator("file.txt",
                                                      self.extracted_package)
        with self.assertRaises(exceptions.WebImageGeneratorError) as exc_info:
            web_image_generator.create_thumbnail()
        self.assertIn("Error opening image file ", str(exc_info.exception))
        self.assertIn("file.txt", str(exc_info.exception))
        self.assertFalse(
            os.path.exists(os.path.join(self.extracted_package, "file.png")))
示例#7
0
def test_get_image_from_url_decompression_bomb(mocker):
    session_mock = mocker.Mock()
    response_mock = mocker.Mock()
    mocker.patch("robotoff.utils.Image",
                 **{"open.side_effect": Image.DecompressionBombError()})
    response_mock.content = generate_image()
    response_mock.ok = True
    response_mock.status_code = 200
    session_mock.get.return_value = response_mock

    with pytest.raises(ImageLoadingException,
                       match="Decompression bomb error for image MOCK_URL"):
        get_image_from_url("MOCK_URL", error_raise=True, session=session_mock)

    image = get_image_from_url("MOCK_URL",
                               error_raise=False,
                               session=session_mock)
    assert image is None