def __read_image_files(self, image_stack, h5_main, h5_mean_spec, h5_ronch, image_path, mean_ronch, num_files): """ Read each image from `file_list` and save it in `h5_main`. Parameters ---------- image_stack: :param h5_main: :param h5_mean_spec: :param h5_ronch: :param image_path: :param mean_ronch: :param num_files: :return: """ for ifile, thisfile in enumerate(image_stack): selected = (ifile + 1) % round(num_files / 16) == 0 if selected: print('Processing file...{}% - reading: {}'.format(round(100 * ifile / num_files), thisfile)) image = read_image(os.path.join(image_path, thisfile), greyscale=True) image = self.binning_func(image, self.bin_factor, self.bin_func) image = image.flatten() h5_main[:, ifile] = image h5_mean_spec[ifile] = np.mean(image) mean_ronch += image self.h5_file.flush() h5_ronch[:] = mean_ronch / num_files self.h5_file.flush()
def test_color(self): color_image_path = './tests/io/logo_v01.png' img_obj = Image.open(color_image_path) pillow_obj = read_image(color_image_path, as_numpy_array=False, as_grayscale=False) self.assertEqual(img_obj, pillow_obj)
def test_text_to_numpy_simple(self): img_data = rand_image.astype(np.uint8) img_path = 'image_text.txt' delete_existing_file(img_path) np.savetxt(img_path, img_data) np_data = read_image(image_path, as_numpy_array=True) self.assertIsInstance(np_data, np.ndarray) self.assertTrue(np.allclose(np_data, img_data)) delete_existing_file(img_path)
def _read_data(self, file_list, h5_main, h5_mean_spec, h5_ronch, image_path): """ Iterates over the images in `file_list`, reading each image and downsampling if reqeusted, and writes the flattened image to file. Also builds the Mean_Ronchigram and the Spectroscopic_Mean datasets at the same time. Parameters ---------- file_list : list of str List of all files in `image_path` that will be read h5_main : h5py.Dataset Dataset which will hold the Ronchigrams h5_mean_spec : h5py.Dataset Dataset which will hold the Spectroscopic Mean h5_ronch : h5py.Dataset Dataset which will hold the Mean Ronchigram image_path : str Absolute file path to the directory which hold the images Returns ------- None """ mean_ronch = np.zeros(h5_ronch.shape, dtype=np.float32) num_files = len(file_list) for ifile, thisfile in enumerate(file_list): selected = (ifile + 1) % int(round(num_files / 16)) == 0 if selected: print('Processing file...{}% - reading: {}'.format( round(100 * ifile / num_files), thisfile)) image, _ = read_image(os.path.join(image_path, thisfile), get_parms=False, header=self.image_list_tag) # image, _ = read_image(os.path.join(image_path, thisfile), get_parms=False) image = self.crop_ronc(image) image = self.binning_func(image, self.bin_factor, self.bin_func) image = image.flatten() h5_main[ifile, :] = image h5_mean_spec[ifile] = np.mean(image) mean_ronch += image self.h5_f.flush() h5_ronch[:] = mean_ronch / num_files self.h5_f.flush()
def test_text_to_numpy_complex(self): img_data = np.uint16(np.random.randint(0, high=255, size=(4, 3))) img_path = 'image_text.csv' delete_existing_file(img_path) txt_kwargs = { 'delimiter': ',', 'newline': '\n', 'header': 'cat, dog, cow' } np.savetxt(img_path, img_data, **txt_kwargs) np_data = read_image(img_path, as_numpy_array=True, delimiter=',', skiprows=1) self.assertIsInstance(np_data, np.ndarray) self.assertTrue(np.allclose(np_data, img_data)) delete_existing_file(img_path)
def test_text_complex_to_pillow(self): img_data = np.uint16(np.random.randint(0, high=255, size=(4, 3))) img_path = 'image_text.csv' delete_existing_file(img_path) txt_kwargs = { 'delimiter': ',', 'newline': '\n', 'header': 'cat, dog, cow' } np.savetxt(img_path, img_data, **txt_kwargs) pillow_obj = read_image(img_path, as_grayscale=True, as_numpy_array=False, delimiter=',', skiprows=1) self.assertIsInstance(pillow_obj, Image.Image) self.assertTrue(np.allclose(np.asarray(pillow_obj), img_data)) delete_existing_file(img_path)
def _read_data(self, folder): """ Returns ------- """ print('In folder {}'.format(folder)) file_list = self._parse_file_path(folder, self.image_ext) images = list() for image_file in file_list: image_path = os.path.join(folder, image_file) image = read_image(image_path, as_grayscale=True) image = self.binning_func(image, self.bin_factor, self.bin_func) images.append(image) self.N_x, self.N_y = image.shape self.n_pixels = self.N_x * self.N_y return images
def _getimagesize(image): """ Returns the x and y size of the image in pixels Parameters ------------ image : string / unicode absolute path to the image file Returns ----------- (size, tmp.dtype) : Tuple size : unsigned integer x and y dimenstions of image dtype : data type Datatype of the image """ tmp, parms = read_image(image, get_parms=True) size = tmp.shape return size, tmp.dtype.type, parms
def _getimagesize(image): """ Returns the x and y size of the image in pixels Parameters ------------ image : string / unicode absolute path to the image file Returns ----------- (size, tmp.dtype) : Tuple size : unsigned integer x and y dimenstions of image dtype : data type Datatype of the image """ tmp, parms = read_image(image, get_parms=True) size = tmp.shape return size, tmp.dtype.type, parms
def _read_data(self, folder): """ Returns ------- """ file_list = self._parse_file_path(folder, self.image_ext) images = list() for image_file in file_list: image_path = os.path.join(folder, image_file) image, _ = read_image(image_path, as_grey=True) image = self.binning_func(image, self.bin_factor, self.bin_func) images.append(image) self.N_x, self.N_y = image.shape self.n_pixels = self.N_x * self.N_y return images
def __read_image_files(self, image_stack, h5_main, h5_mean_spec, h5_ronch, image_path, mean_ronch, num_files): """ Read each image from `file_list` and save it in `h5_main`. Parameters ---------- image_stack: :param h5_main: :param h5_mean_spec: :param h5_ronch: :param image_path: :param mean_ronch: :param num_files: :return: """ for ifile, thisfile in enumerate(image_stack): selected = (ifile + 1) % round(num_files / 16) == 0 if selected: print('Processing file...{}% - reading: {}'.format( round(100 * ifile / num_files), thisfile)) image = read_image(os.path.join(image_path, thisfile), greyscale=True) image = self.binning_func(image, self.bin_factor, self.bin_func) image = image.flatten() h5_main[:, ifile] = image h5_mean_spec[ifile] = np.mean(image) mean_ronch += image self.h5_file.flush() h5_ronch[:] = mean_ronch / num_files self.h5_file.flush()
def translate(self, h5_path, image_path, bin_factor=None, bin_func=np.mean, start_image=0, scan_size_x=None, scan_size_y=None, crop_ammount=None, crop_method='percent'): """ Basic method that adds Ptychography data to existing hdf5 thisfile You must have already done the basic translation with BEodfTranslator Parameters ---------------- h5_path : str Absolute path to where the HDF5 file should be located image_path : str Absolute path to folder holding the image files bin_factor : array_like of uint, optional Downsampling factor for each dimension. Default is None. bin_func : callable, optional Function which will be called to calculate the return value of each block. Function must implement an axis parameter, i.e. numpy.mean. Ignored if bin_factor is None. Default is numpy.mean. start_image : int, optional Integer denoting which image in the file path should be considered the starting point. Default is 0, start with the first image on the list. scan_size_x : int, optional Number of Ronchigrams in the x direction. Default is None, value will be determined from the number of images and `scan_size_y` if it is given. scan_size_y : int, optional Number of Ronchigrams in the y direction. Default is None, value will be determined from the number of images and `scan_size_x` if it is given. crop_ammount : uint or list of uints, optional How much should be cropped from the original image. Can be a single unsigned integer or a list of unsigned integers. A single integer will crop the same ammount from all edges. A list of two integers will crop the x-dimension by the first integer and the y-dimension by the second integer. A list of 4 integers will crop the image as [left, right, top, bottom]. crop_method : str, optional Which cropping method should be used. How much of the image is removed is determined by the value of `crop_ammount`. 'percent' - A percentage of the image is removed. 'absolute' - The specific number of pixel is removed. Returns ---------- h5_main : h5py.Dataset HDF5 Dataset object that contains the flattened images """ # Open the hdf5 file and delete any contents if os.path.exists(h5_path): os.remove(h5_path) h5_f = h5py.File(h5_path, 'w') self.h5_f = h5_f self.crop_method = crop_method self.crop_ammount = crop_ammount ''' Get the list of all files with the .tif extension and the number of files in the list ''' image_path = os.path.abspath(image_path) root_file_list, file_list = self._parse_file_path(image_path) size, image_parms = self._getimageparms(file_list[0]) usize, vsize = size self.image_list_tag = image_parms.pop('Image_Tag', None) tmp, _ = read_image(file_list[0]) if crop_ammount is not None: tmp = self.crop_ronc(tmp) usize, vsize = tmp.shape num_files = len(file_list) if scan_size_x is None and scan_size_y is None: scan_size_x = int(np.sqrt(num_files)) scan_size_y = int(num_files / scan_size_x) elif scan_size_x is None: scan_size_x = int(num_files / scan_size_y) elif scan_size_y is None: scan_size_y = int(num_files / scan_size_x) ''' Check if a bin_factor is given. Set up binning objects if it is. ''' if bin_factor is not None: self.rebin = True if isinstance(bin_factor, int): self.bin_factor = (bin_factor, bin_factor) elif len(bin_factor) == 2: self.bin_factor = tuple(bin_factor) else: raise ValueError( 'Input parameter `bin_factor` must be a length 2 array_like or an integer.\n' + '{} was given.'.format(bin_factor)) usize = int(usize / self.bin_factor[0]) vsize = int(vsize / self.bin_factor[1]) self.binning_func = block_reduce self.bin_func = bin_func num_files = scan_size_x * scan_size_y h5_main, h5_mean_spec, h5_ronch = self._setupH5( usize, vsize, np.float32, scan_size_x, scan_size_y, image_parms) for root_file in root_file_list: print('Saving the root image located at {}.'.format(root_file)) self._create_root_image(root_file) self._read_data(file_list[start_image:start_image + num_files], h5_main, h5_mean_spec, h5_ronch, image_path) self.h5_f.close() return
def _read_data(self, file_list, h5_main, h5_mean_spec, h5_ronch, image_path, image_type): """ Iterates over the images in `file_list`, reading each image and downsampling if reqeusted, and writes the flattened image to file. Also builds the Mean_Ronchigram and the Spectroscopic_Mean datasets at the same time. Parameters ---------- file_list : list of str List of all files in `image_path` that will be read h5_main : h5py.Dataset Dataset which will hold the Ronchigrams h5_mean_spec : h5py.Dataset Dataset which will hold the Spectroscopic Mean h5_ronch : h5py.Dataset Dataset which will hold the Mean Ronchigram image_path : str Absolute file path to the directory which hold the images Returns ------- None """ mean_ronch = np.zeros(h5_ronch.shape, dtype=np.float32) num_files = len(file_list) if image_type == '.dm3': for ifile, thisfile in enumerate(file_list): image, _ = read_dm3(thisfile) image = self.binning_func(image, self.bin_factor, self.bin_func) image = image.flatten() h5_main[ifile, :] = image h5_mean_spec[ifile] = np.mean(image) mean_ronch += image self.h5_file.flush() else: for ifile, thisfile in enumerate(file_list): # selected = (ifile + 1) % round(num_files / 16) == 0 # if selected: # print('Processing file...{}% - reading: {}'.format(round(100 * ifile / num_files), thisfile)) image, _ = read_image(os.path.join(image_path, thisfile), as_gray=True) image = self.binning_func(image, self.bin_factor, self.bin_func) image = image.flatten() h5_main[ifile, :] = image h5_mean_spec[ifile] = np.mean(image) mean_ronch += image self.h5_file.flush() h5_ronch[:] = mean_ronch / num_files self.h5_file.flush()
def test_color_to_bw_image(self): color_image_path = './tests/io/logo_v01.png' img_obj = Image.open(color_image_path).convert(mode="L") pillow_obj = read_image(color_image_path, as_numpy_array=False) self.assertEqual(img_obj, pillow_obj)
def test_to_pillow(self): pillow_obj = read_image(image_path, as_numpy_array=False) self.assertIsInstance(pillow_obj, Image.Image) self.assertTrue(np.allclose(np.asarray(pillow_obj), rand_image))
def test_to_numpy(self): np_data = read_image(image_path, as_numpy_array=True) self.assertIsInstance(np_data, np.ndarray) self.assertTrue(np.allclose(np_data, rand_image))