diff --git a/docs/source/how_do_i.rst b/docs/source/how_do_i.rst index ea48652..a9da7c1 100644 --- a/docs/source/how_do_i.rst +++ b/docs/source/how_do_i.rst @@ -347,7 +347,7 @@ image isn't square. :: >>> alpha[mask] = 0 >>> rgba = np.concatenate((rgb, alpha), axis=2) >>> jp2 = Jp2k('tmp.jp2', 'wb') - >>> jp2.write(rgba) + >>> jp2[:] = rgba Next we need to specify what types of channels we have. The first three channels are color channels, but we identify the fourth as @@ -447,7 +447,7 @@ http://photojournal.jpl.nasa.gov/tiff/PIA17145.tif info JPEG 2000:: >>> image = skimage.io.imread('PIA17145.tif') >>> from glymur import Jp2k >>> jp2 = Jp2k('PIA17145.jp2', 'wb') - >>> jp2.write(image) + >>> jp2[:] = image Next you can extract the XMP metadata. diff --git a/glymur/jp2k.py b/glymur/jp2k.py index 870a6ec..66b79c9 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -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. diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 90717ff..98f4607 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -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, :]