def test_parse_pointer(): # Example tests/mission_data/1p432690858esfc847p2111l2m1.img assert PDS3Image.parse_pointer(56, 640) == [35200, None] # Example tests/mission_data/W1782844276_1.LBL assert PDS3Image.parse_pointer(["W1782844276_1.IMG", 5], 1024) == [4096, 'W1782844276_1.IMG'] # TODO: Awaiting other known valid examples to implement remaining conditions #print PDS3Image.parse_pointer("W1782844276_1.IMG", 1024) #print PDS3Image.parse_pointer(["W1782844276_1.IMG"], 1024) # ^IMAGE = 101337 <BYTES> assert PDS3Image.parse_pointer(Units(value=101337, units='BYTES'), None) == [101337, None]
def test_generate_pds_with_cahvor_right(): generate_pds.PDSGenerator(filename_right) image = PDS3Image.open(os.path.splitext(filename_right)[0] + '.IMG') assert image.bands == 3 assert image.lines == 2048 assert image.samples == 3072 assert image.format == 'BAND_SEQUENTIAL' assert image.dtype == numpy.dtype('>i2') # Testing .label assert image.label['^IMAGE'] == 2 assert image.label['IMAGE']['SAMPLE_TYPE'] == 'MSB_INTEGER' assert image.label['IMAGE']['MAXIMUM'] == 167.0 assert image.label['IMAGE']['MEDIAN'] == 15.0 assert image.label['IMAGE']['MINIMUM'] == 0.0 assert image.label['IMAGE']['SAMPLE_BITS'] == 16 C = image.label['GEOMETRIC_CAMERA_MODEL']['MODEL_COMPONENT_1'] A = image.label['GEOMETRIC_CAMERA_MODEL']['MODEL_COMPONENT_2'] H = image.label['GEOMETRIC_CAMERA_MODEL']['MODEL_COMPONENT_3'] V = image.label['GEOMETRIC_CAMERA_MODEL']['MODEL_COMPONENT_4'] assert image.data.dtype == numpy.dtype('>i2') assert_almost_equal(C, [-1.4097, -0.7620000000000001, -3.3667]) assert_almost_equal(A, [-0.0014849543217824793, 0.9944904168251026, 0.10481701080309513]) assert_almost_equal(H, [-611.6639911280968, -23503.278833922475, 118.4987568245268]) assert_almost_equal(V, [2443.2223036460596, -23181.3008195100119, 123.0602553862336]) os.remove(os.path.splitext(filename_right)[0] + '.IMG')
def test_generate_pds_with_cahvor_left(): generate_pds.PDSGenerator(filename_left) image = PDS3Image.open(os.path.splitext(filename_left)[0] + '.IMG') assert image.bands == 3 assert image.lines == 1024 assert image.samples == 1536 assert image.format == 'BAND_SEQUENTIAL' assert image.dtype == numpy.dtype('>i2') # Testing .label assert image.label['^IMAGE'] == 2 assert image.label['IMAGE']['SAMPLE_TYPE'] == 'MSB_INTEGER' assert image.label['IMAGE']['MAXIMUM'] == 174.0 assert image.label['IMAGE']['MEDIAN'] == 14.0 assert image.label['IMAGE']['MINIMUM'] == 0.0 assert image.label['IMAGE']['SAMPLE_BITS'] == 16 C = image.label['GEOMETRIC_CAMERA_MODEL']['MODEL_COMPONENT_1'] A = image.label['GEOMETRIC_CAMERA_MODEL']['MODEL_COMPONENT_2'] H = image.label['GEOMETRIC_CAMERA_MODEL']['MODEL_COMPONENT_3'] V = image.label['GEOMETRIC_CAMERA_MODEL']['MODEL_COMPONENT_4'] assert image.data.dtype == numpy.dtype('>i2') assert_almost_equal(C, [1.4097, -0.7619999999999999, -3.3667]) assert_almost_equal(A, [-0.07890237041579115, 0.9965459569473566, -0.025895397943394106]) assert_almost_equal(H, [-3668.464789238566, -23222.553666216594, -218.30723379448355]) assert_almost_equal(V, [-607.2642643405248, -23302.0994264739420, 24.0659112157058]) os.remove(os.path.splitext(filename_left)[0] + '.IMG')
def test_pds3_1band_disk_format(pattern_data): image = PDS3Image(open(filename, 'rb'), memory_layout='DISK') assert image.data.shape == (1, 10, 10) assert image.data.size == 100 assert_almost_equal(image.data[0], pattern_data)
def test_pds3_1band_image_format(pattern_data): image = PDS3Image.open(filename) assert image.data.shape == (10, 10) assert image.data.size == 100 assert_almost_equal(image.data, pattern_data.reshape(10, 10))
def create_image_set(self, filepaths): rgb = ['R', 'G', 'B'] for filepath in filepaths: try: channels = [] pds_image = PDS3Image.open(filepath) bands = pds_image.label['IMAGE']['BANDS'] if bands == 3: for n in range(bands): name = os.path.basename(filepath) + '(%s)' % (rgb[n]) data = pds_image.image[:, :, n] image = ImageStamp( filepath=filepath, name=name, data_np=data, pds_image=pds_image) # self.file_dict[image.image_name] = image channels.append(image) self.images.append(channels) else: name = os.path.basename(filepath) data = pds_image.image image = ImageStamp( filepath=filepath, name=name, data_np=data, pds_image=pds_image) self.images.append([image]) # self.file_dict[image.image_name] = image except: warnings.warn(filepath + " cannnot be opened")
def test_image_stamp(): """Test that ImageStamp sets correct attributes to pds compatible image""" pds_image = PDS3Image.open(FILE_1) test_image = pdsview.ImageStamp(FILE_1, FILE_1, pds_image, pds_image.data) assert test_image.file_name == FILE_1_NAME assert test_image.image_name == FILE_1 assert 'PDS' in test_image.label[0] assert isinstance(test_image.label, list) assert not test_image.cuts assert not test_image.sarr assert not test_image.zoom assert not test_image.rotation assert not test_image.transforms assert test_image.not_been_displayed
def test_pds3_1band_labels(): image = PDS3Image.open(filename) assert image.filename == filename assert image.bands == 1 assert image.lines == 10 assert image.samples == 10 assert image.format == 'BAND_SEQUENTIAL' assert image.pixel_type == numpy.dtype('>i2') assert image.dtype == numpy.dtype('>i2') assert image.start_byte == 640 assert image.shape == (1, 10, 10) assert image.byte_order == '>' assert image.size == 100
def __init__(self, filepath): pds_date = self._convert_date(filepath) print('pds_date:', pds_date) jpg_im = Image.open(filepath) np_ar = np.asarray(jpg_im) dim = np_ar.shape new_ar = np.zeros((dim[2], dim[0], dim[1])) new_ar[0, :, :] = np_ar[:, :, 0] new_ar[1, :, :] = np_ar[:, :, 1] new_ar[2, :, :] = np_ar[:, :, 2] new_ar = new_ar.astype('>i2') self.img = PDS3Image(new_ar) self.filename = os.path.splitext(filepath)[0] if self._label_file_exists(): self.img.label = self._update_label(pds_date) self.img.save(self.filename + '.IMG')
def __init__(self, filename, row, column, data_np=None, metadata=None, logger=None): BaseImage.__init__(self, data_np=data_np, metadata=metadata, logger=logger) self.file_name = filename self.abspath = os.path.abspath(filename) self.basename = os.path.basename(filename) self.row = row self.column = column try: self.pds_image = PDS3Image.open(filename) self.set_data(self.pds_image.data) self.position = (row, column) self.size = (PSIZE, PSIZE) self.selected = False self.pds_compatible = True except: self.pds_compatible = False
def test_generate_pds_with_no_label(): generate_pds.PDSGenerator(filename_nolabel) image = PDS3Image.open(os.path.splitext(filename_nolabel)[0] + '.IMG') assert image.bands == 3 assert image.lines == 1024 assert image.samples == 1536 assert image.format == 'BAND_SEQUENTIAL' assert image.dtype == numpy.dtype('>i2') # Testing .label assert image.label['^IMAGE'] == 2 assert image.label['IMAGE']['SAMPLE_TYPE'] == 'MSB_INTEGER' assert image.label['IMAGE']['MAXIMUM'] == 164.0 assert image.label['IMAGE']['MEDIAN'] == 40.0 assert image.label['IMAGE']['MINIMUM'] == 0.0 assert image.label['IMAGE']['SAMPLE_BITS'] == 16 with pytest.raises(KeyError): image.label['GEOMETRIC_CAMERA_MODEL']['MODEL_COMPONENT_1'] os.remove(os.path.splitext(filename_nolabel)[0] + '.IMG')
def __init__(self, filepath, metadata=None, logger=None, wavelength=float('nan'), unit='nm'): self.pds_image = PDS3Image.open(filepath) data = self.pds_image.image.astype(float) BaseImage.__init__(self, data_np=data, metadata=metadata, logger=logger) self.set_data(data) self.image_name = os.path.basename(filepath) self.seen = False self.cuts = (None, None) self._check_acceptable_unit(unit) if np.isnan(wavelength): wavelength = get_wavelength(self.pds_image.label, unit) unit = astro_units.Unit(unit) self._wavelength = wavelength * unit
def preprocess_image(directory, name): url = 'https://pdsimage2.wr.usgs.gov/archive/mess-e_v_h-mdis-2-edr-rawdata-v1.0/MSGRMDS_1001/DATA/'+ str(directory) file = requests.get(url+ '/' + str(name)) try: img = open("img.txt", "wb+") img.write(file.content) image = PDS3Image.open('img.txt') t = image.image plt.imsave('images/'+ name +'.jpg', t, cmap = 'gray') except: img_data = bytearray() header = [] # print(file.content) # Test takes 2 values: # 0-> Before end of header is reached # 1-> End of header and start of image data test = 0 for line in file.iter_lines(): # Read Image data if test: img_data.extend(line) # End of header is reached elif re.match("^END$", line.decode('utf-8').strip()): test = 1 # Read the Header else: header.append(line.decode('utf-8').strip()) file.close() # print(header) # Flag takes 3 values: # 0-> before required data is encountered # 1-> Reading required data # 2-> after the required data is read flag = 0 for line in header: if re.match('^TARGET_NAME',line): target = line.split('"')[1] # print(target) # Start Byte if re.match('^\^IMAGE',line): n = line.split('=') start_byte = int(n[1].strip()) # End of required data if re.match('^END_OBJECT',line): flag = 2 # Beginning of required data if re.match('^OBJECT',line): flag = 1 # Required data if flag == 1: if re.match('^LINES',line): # Extract no of rows a = line.split() row = int(a[2]) elif re.match('^LINE_SAMPLES',line): # Extract no of columns a = line.split() column = int(a[2]) elif re.match('^SAMPLE_BITS',line): # Extract the type of bit encoding a = line.split() typ = 'uint'+ a[2] # print(row,column,typ) # print(len(img_data.decode())) # b => bit width of data b = int(a[2]) # Numpy array from byte array a = np.frombuffer(img_data[len(img_data)%16:],dtype=typ) # print(a) # s => No of extra bits in the image data = Total no of bits - rows*columns s = (a.size/row -column)*row if s<0: print("Defective Image:",s) return None # t => Array after Trash data is removed t = a[int(s):].copy() # print(t.size) t.resize((row,column)) # Reshaping the array np.divide(t,float(2**(b-8))) t = t.astype('uint8') # Save image plt.imsave('images/'+ name +'.jpg',t,cmap='gray') # plt.show() image = Image.open('images/' + name +'.jpg').convert("L") data = np.array(image) # print(data.shape) crmask,n_cosmic = lacosmic(data, contrast=5.0, cr_threshold=4.5, neighbor_threshold=0.3, error=None, mask=None, background=None, effective_gain=1.0, readnoise=6.5, maxiter=1, border_mode=u'mirror') if (n_cosmic > 0): return data else: print("Cosmic Ray not found")