def extract_img(image_file: str) -> Image: """Extracts and returns embedded image from PDS3 IMG files as PIL Image. Args: image_file (str): path to .IMG file. Returns: PIL.Image """ # Parsing label label = pvl.load(image_file) # load label from .IMG file image_data = label['IMAGE'] # getting image object info h_img, w_img = image_data[0][-1], image_data[1][-1] # real image sizes pref, suff = image_data[6][-1], image_data[7][-1] # buffer pixels margins w_total = w_img + pref + suff # width with margins offset = label['^IMAGE'].value # pointer where image is located size = label['^GAP_TABLE'].value - label[ '^IMAGE'].value # image size (in bytes) # Now getting back to file with open(image_file, "rb") as f: container = ContainerIO.ContainerIO(f, offset - 1, size) data = container.read() # reading image bytes img = Image.frombytes('L', (w_total, h_img), data, "raw") # decoding img = img.crop((pref, 0, w_img + pref, h_img)) # cropping margins return img
def test_readline(): # Arrange with open(TEST_FILE) as fh: container = ContainerIO.ContainerIO(fh, 0, 120) # Act data = container.readline() # Assert assert data == "This is line 1\n"
def test_readline(self): # Arrange with open(TEST_FILE) as fh: container = ContainerIO.ContainerIO(fh, 0, 120) # Act data = container.readline() # Assert self.assertEqual(data, "This is line 1\n")
def test_read_eof(): # Arrange with open(TEST_FILE) as fh: container = ContainerIO.ContainerIO(fh, 22, 100) # Act container.seek(100) data = container.read() # Assert assert data == ""
def test_read_n0(): # Arrange with open(TEST_FILE) as fh: container = ContainerIO.ContainerIO(fh, 22, 100) # Act container.seek(81) data = container.read() # Assert assert data == "7\nThis is line 8\n"
def test_read_n0(self): # Arrange with open(TEST_FILE) as fh: container = ContainerIO.ContainerIO(fh, 22, 100) # Act container.seek(81) data = container.read() # Assert self.assertEqual(data, "7\nThis is line 8\n")
def test_read_eof(self): # Arrange with open(TEST_FILE) as fh: container = ContainerIO.ContainerIO(fh, 22, 100) # Act container.seek(100) data = container.read() # Assert self.assertEqual(data, "")
def test_seek_mode_2(): # Arrange mode = 2 with open(TEST_FILE) as fh: container = ContainerIO.ContainerIO(fh, 22, 100) # Act container.seek(33, mode) container.seek(33, mode) # Assert assert container.tell() == 100
def test_seek_mode_2(self): # Arrange mode = 2 with open(TEST_FILE) as fh: container = ContainerIO.ContainerIO(fh, 22, 100) # Act container.seek(33, mode) container.seek(33, mode) # Assert self.assertEqual(container.tell(), 100)
def test_readline(): # Arrange for bytesmode in (True, False): with open(TEST_FILE, "rb" if bytesmode else "r") as fh: container = ContainerIO.ContainerIO(fh, 0, 120) # Act data = container.readline() # Assert if bytesmode: data = data.decode() assert data == "This is line 1\n"
def test_read_eof(): # Arrange for bytesmode in (True, False): with open(TEST_FILE, "rb" if bytesmode else "r") as fh: container = ContainerIO.ContainerIO(fh, 22, 100) # Act container.seek(100) data = container.read() # Assert if bytesmode: data = data.decode() assert data == ""
def test_readlines(self): # Arrange expected = [ "This is line 1\n", "This is line 2\n", "This is line 3\n", "This is line 4\n", "This is line 5\n", "This is line 6\n", "This is line 7\n", "This is line 8\n" ] with open(TEST_FILE) as fh: container = ContainerIO.ContainerIO(fh, 0, 120) # Act data = container.readlines() # Assert self.assertEqual(data, expected)
def test_readlines(): # Arrange for bytesmode in (True, False): expected = [ "This is line 1\n", "This is line 2\n", "This is line 3\n", "This is line 4\n", "This is line 5\n", "This is line 6\n", "This is line 7\n", "This is line 8\n", ] with open(TEST_FILE, "rb" if bytesmode else "r") as fh: container = ContainerIO.ContainerIO(fh, 0, 120) # Act data = container.readlines() # Assert if bytesmode: data = [line.decode() for line in data] assert data == expected
def test_isatty(): with hopper() as im: container = ContainerIO.ContainerIO(im, 0, 0) assert container.isatty() is False
def test_isatty(self): im = hopper() container = ContainerIO.ContainerIO(im, 0, 0) self.assertEqual(container.isatty(), 0)
def test_isatty(self): with hopper() as im: container = ContainerIO.ContainerIO(im, 0, 0) self.assertFalse(container.isatty())
def read_merged_file(file_name): """ Given the name of a merged png file this will read each image and return a list of numpy arrays containing the images. This is the slow way to read a merged file. Use the load module if you're looking for a faster way to do this. """ try: from PIL import Image, ContainerIO except: import Image import ContainerIO imgs = [] with open(file_name, 'rb') as in_file: in_file.seek(0, 2) end_pos = in_file.tell() in_file.seek(0) num_imgs = struct.unpack('i', in_file.read(4))[0] it = 0 labels = [] while in_file.tell() < end_pos: nbytes, minval, maxval = struct.unpack('iff', in_file.read(3 * 4)) if minval > maxval: minval, maxval = maxval, minval factor = (maxval - minval) / 255.0 curr_pos = in_file.tell() # load image into np array try: img = np.asarray( Image.open( ContainerIO.ContainerIO(in_file, curr_pos, nbytes))) except IOError: in_file.seek(curr_pos) labels.append(int(struct.unpack('f', in_file.read(4))[0] + .5)) curr_pos = in_file.tell() img = np.asarray( Image.open( ContainerIO.ContainerIO(in_file, curr_pos, nbytes))) # re-whiten the image img = img * factor + minval # add to list imgs.append(img) # seek past image in_file.seek(curr_pos + nbytes) it += 1 # ensure we reached end of file curr_pos = in_file.tell() in_file.seek(0, 2) assert (curr_pos == in_file.tell()) # if these do not match throw warning if it != num_imgs: from warnings import warn warn('File %s reported %d images but only had %d' % (file_name, num_imgs, it)) return imgs, labels