__setitem__ working for writing an entire image at once

This commit is contained in:
John Evans 2014-09-19 08:17:15 -04:00
commit b1cd14c6a5
3 changed files with 36 additions and 8 deletions

View file

@ -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.

View file

@ -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.

View file

@ -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, :]