def from_gdal_dataset(cls, dataset, band): """ Creates a new 1-band pyvips.Image from `dataset` at `band` dataset: GDAL Dataset band: Number of the band, starting from 1 """ with LibVips.disable_warnings(): filename = dataset.GetFileList()[0] image1 = Image.new_from_file(filename) # Extract the band image2 = image1.extract_band((band - 1), n=1) # Cast to the right datatype, if necessary datatype = dataset.GetRasterBand(band).NumPyDataType if VImageAdapter(image2).NumPyType() == datatype: return image2 types = dict((v, k) for k, v in cls.NUMPY_TYPES.items()) image3 = Image.new_from_memory(image2.write_to_memory(), width=image2.width, height=image2.height, bands=1, format=types[datatype]) return image3
def from_gdal_dataset(cls, dataset, band): """ Creates a new 1-band pyvips.Image from `dataset` at `band` dataset: GDAL Dataset band: Number of the band, starting from 1 """ with LibVips.disable_warnings(): filename = dataset.GetFileList()[0] image1 = Image.new_from_file(filename) # Extract the band image2 = image1.extract_band((band - 1), n=1) # Cast to the right datatype, if necessary datatype = dataset.GetRasterBand(band).NumPyDataType if VImageAdapter(image2).NumPyType() == datatype: return image2 types = dict((v, k) for k, v in cls.NUMPY_TYPES.items()) image3 = Image.new_from_memory(image2.write_to_memory(), width=image2.width, height=image2.height, bands=1, format=types[datatype]) image3._buf = image2 return image3
def numpy_to_vips(np_array: np.ndarray, width: Optional[int] = None, height: Optional[int] = None, n_channels: Optional[int] = None) -> VIPSImage: """ Convert a Numpy array to a VIPS image. Parameters ---------- np_array : array-like Numpy array to convert. If 1D, it is expected it contains flattened image data. width : int (optional) Width of the image, must be given if `np_array` is 1D, otherwise inferred from shape. height : int (optional) Height of the image, must be given if `np_array` is 1D, otherwise inferred from shape. n_channels : int (optional) n_channels of the image, must be given if `np_array` is 1D, otherwise inferred from shape. Returns ------- image VIPS image representation of the array Raises ------ ValueError If it is impossible to convert provided array. """ if not np_array.flags['C_CONTIGUOUS']: np_array = np.ascontiguousarray(np_array) if np_array.ndim > 3: raise NotImplementedError elif np_array.ndim > 1: if np_array.ndim == 2: height_, width_ = np_array.shape n_channels_ = 1 else: height_, width_, n_channels_ = np_array.shape width = width if width is not None else width_ height = height if height is not None else height_ n_channels = n_channels if n_channels is not None else n_channels_ if width * height * n_channels != np_array.size: raise ValueError(f"Cannot convert {np_array} to VIPS image") flat = np_array.reshape(np_array.size) vips_format = dtype_to_vips_format[str(np_array.dtype)] return VIPSImage.new_from_memory(flat.data, width, height, n_channels, vips_format)
def write_buffer(self, image, resolution): if VImageAdapter( image).BufferSize() >= self.IMAGE_BUFFER_DISK_THRESHOLD: logger.debug('Buffering resolution {0} to disk'.format(resolution)) vipsfile = Image.new_temp_file("%s.v") tempfile_image = image.write(vipsfile) return tempfile_image logger.debug('Buffering resolution {0} to memory'.format(resolution)) return Image.new_from_memory(image.write_to_memory(), image.width, image.height, image.bands, 'uchar')
def from_numpy_array(cls, array, width, height, bands, format): """ Returns a new pyvips.Image created from a NumPy `array` of pixel data. array: The NumPy array width: Integer dimension height: Integer dimension bands: Number of bands in the buffer format: Band format (all bands must be the same format) """ array = array.astype(cls.NUMPY_TYPES[format]) buf = memoryview(array) image = Image.new_from_memory(buf, width, height, bands, format) return image
def write_buffer(self, image, resolution): if VImageAdapter(image).BufferSize() >= self.IMAGE_BUFFER_DISK_THRESHOLD: logger.debug( 'Buffering resolution {0} to disk'.format(resolution) ) vipsfile = Image.new_temp_file("%s.v") tempfile_image = image.write(vipsfile) return tempfile_image logger.debug( 'Buffering resolution {0} to memory'.format(resolution) ) return Image.new_from_memory( image.write_to_memory(), image.width, image.height, image.bands, 'uchar' )
def from_numpy_array(cls, array, width, height, bands, format): """ Returns a new pyvips.Image created from a NumPy `array` of pixel data. array: The NumPy array width: Integer dimension height: Integer dimension bands: Number of bands in the buffer format: Band format (all bands must be the same format) """ array = array.astype(cls.NUMPY_TYPES[format]) buf = memoryview(array) image = Image.new_from_memory(buf, width, height, bands, format) # Hold on to the numpy array to prevent garbage collection image._numpy_array = array return image
def run_on_large_image(root_path: str, original_img: str, ckpt: str, refine: bool): '''在一整张图上执行运算 Args: root_path(str): 从大图中提取的小图所在目录 original_img(str): 大图的路径 ckpt(str): 检查点路径 ''' assert os.path.exists(root_path), '路径不存在: {}'.format(root_path) assert os.path.exists(original_img), '路径不存在: {}'.format(original_img) # 配置模型 print('Setting up model.') model, device = setup(DeepLabV3Res101()) print('Loading model from {}.'.format(ckpt)) model, _ = restore_from(model, ckpt) model.eval() file_list = os.listdir(root_path) original_img_array = vipImage.new_from_file(original_img) ow, oh = [getattr(original_img_array, key) for key in ['width', 'height']] large_array = np.zeros((oh, ow), dtype=np.uint8) # 统计尺度 scales = set() for file in file_list: scale, _, _, _ = extract_info_from_filename(file) scales.add(scale) # 使用最大尺度的图片作为索引 max_scale = max(scales) if refine else min(scales) for file in tqdm(file_list, desc='Processing[r={}]'.format(refine)): scale, x, y, _ = extract_info_from_filename(file) if scale != max_scale: continue file_path = os.path.join(root_path, file) if refine: refined_image = run_and_refine_single_image(file_path, ckpt, False, model=model, device=device) else: refined_image = run_on_single_image(file_path, ckpt, model=model, device=device) rh, rw = refined_image.shape # 根据生成图像大小填充最终图像 large_array[x:x + rh, y:y + rw] = refined_image print('Saving to files...') # 为输出图片上色,方便查看 colored_array = large_array * 85 # 输出目录 flag = 'refined' if refine else 'norefine' filename, extension = os.path.basename(original_img).rsplit('.', 1) output_file = os.path.join( './output', '{}_predict_{}_{}.{}'.format(filename, max_scale, flag, extension)) colored_file = os.path.join( './output', '{}_predict_colored_{}_{}.{}'.format(filename, max_scale, flag, extension)) vipImage.new_from_memory(large_array.data, ow, oh, 1, 'uchar').write_to_file(output_file) vipImage.new_from_memory(colored_array.data, ow, oh, 1, 'uchar').write_to_file(colored_file) print('Large Image File has been saved to {} and {}'.format( output_file, colored_file))