__setitem__ working for writing an entire image at once
This commit is contained in:
parent
a580fe5097
commit
b1cd14c6a5
3 changed files with 36 additions and 8 deletions
|
|
@ -759,6 +759,22 @@ class Jp2k(Jp2kBox):
|
|||
|
||||
return boxes
|
||||
|
||||
def __setitem__(self, index, data):
|
||||
"""
|
||||
Slicing protocol.
|
||||
"""
|
||||
if isinstance(index, slice) and (
|
||||
index.start == None and
|
||||
index.stop == None and
|
||||
index.step == None):
|
||||
# Case of jp2[:] = data, i.e. write the entire image.
|
||||
#
|
||||
# Should have a slice object where start = stop = step = None
|
||||
self.write(data)
|
||||
else:
|
||||
msg = "Images currently must be written entirely at once."
|
||||
raise TypeError(msg)
|
||||
|
||||
def __getitem__(self, pargs):
|
||||
"""
|
||||
Slicing protocol.
|
||||
|
|
|
|||
|
|
@ -75,38 +75,50 @@ class TestSliceProtocolBaseWrite(SliceProtocolBase):
|
|||
with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile:
|
||||
j = Jp2k(tfile.name, 'wb')
|
||||
j[:] = self.j2k_data
|
||||
expected = j.read()
|
||||
actual = j.read()
|
||||
|
||||
np.testing.assert_array_equal(actual, expected)
|
||||
|
||||
def test_cannot_write_with_non_default_single_slice(self):
|
||||
with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile:
|
||||
j = Jp2k(tfile.name, 'wb')
|
||||
with self.assertRaises(TypeError):
|
||||
j[slice(None, 0)] = self.j2k_data
|
||||
with self.assertRaises(TypeError):
|
||||
j[slice(0, None)] = self.j2k_data
|
||||
with self.assertRaises(TypeError):
|
||||
j[slice(0, 0, None)] = self.j2k_data
|
||||
with self.assertRaises(TypeError):
|
||||
j[slice(0, 640)] = self.j2k_data
|
||||
|
||||
def test_cannot_write_a_row(self):
|
||||
with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile:
|
||||
j = Jp2k(tfile.name, 'wb')
|
||||
with self.assertRaises(IOError):
|
||||
with self.assertRaises(TypeError):
|
||||
j[5] = self.j2k_data
|
||||
|
||||
def test_cannot_write_a_pixel(self):
|
||||
with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile:
|
||||
j = Jp2k(tfile.name, 'wb')
|
||||
with self.assertRaises(IOError):
|
||||
with self.assertRaises(TypeError):
|
||||
j[25, 35] = self.j2k_data[25, 35]
|
||||
|
||||
def test_cannot_write_a_column(self):
|
||||
with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile:
|
||||
j = Jp2k(tfile.name, 'wb')
|
||||
with self.assertRaises(IOError):
|
||||
with self.assertRaises(TypeError):
|
||||
j[:, 25, :] = self.j2k_data[:, :25, :]
|
||||
|
||||
def test_cannot_write_a_band(self):
|
||||
with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile:
|
||||
j = Jp2k(tfile.name, 'wb')
|
||||
with self.assertRaises(IOError):
|
||||
with self.assertRaises(TypeError):
|
||||
j[:, :, 0] = self.j2k_data[:, :, 0]
|
||||
|
||||
def test_cannot_write_a_subarray(self):
|
||||
with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile:
|
||||
j = Jp2k(tfile.name, 'wb')
|
||||
with self.assertRaises(IOError):
|
||||
with self.assertRaises(TypeError):
|
||||
j[:25, :45, :] = self.j2k_data[:25, :25, :]
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue