Skip to content

Commit

Permalink
Merge pull request #72 from cdeil/from_numpy
Browse files Browse the repository at this point in the history
Implement HipsTile.from_numpy and test
  • Loading branch information
cdeil committed Jul 17, 2017
2 parents 2bc971f + 1b000d5 commit 2ab7f7a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 11 additions & 4 deletions hips/tiles/tests/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,16 @@ def _read_tile(pars):
filename = get_hips_extra_file(pars['filename'])
return HipsTile.read(meta, filename)

# TODO: implement tests for the `from_numpy` method!
def _test_from_numpy(self):
pass
@requires_hips_extra()
@pytest.mark.parametrize('pars', HIPS_TILE_TEST_CASES)
def test_from_numpy(self, pars):
tile = self._read_tile(pars)
tile2 = HipsTile.from_numpy(tile.meta, tile.data)

# JPEG encoding is lossy. So in that case, output pixel value
# aren't exactly the same as input pixel values
if tile.meta.file_format != 'jpg':
assert_equal(tile.data, tile2.data)

@requires_hips_extra()
@pytest.mark.parametrize('pars', HIPS_TILE_TEST_CASES)
Expand Down Expand Up @@ -114,7 +121,7 @@ def test_write(self, tmpdir, pars):
# Check that tile I/O works, i.e. round-trips on write / read
tile = self._read_tile(pars)

filename = tmpdir / Path(pars['filename']).name
filename = str(tmpdir / Path(pars['filename']).name)
tile.write(filename)
tile2 = HipsTile.read(tile.meta, filename)

Expand Down
8 changes: 6 additions & 2 deletions hips/tiles/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,17 @@ def from_numpy(cls, meta: HipsTileMeta, data: np.ndarray) -> 'HipsTile':
if fmt == 'fits':
hdu = fits.PrimaryHDU(data)
hdu.writeto(bio)
elif fmt in {'jpg', 'png'}:
elif fmt == 'jpg':
image = Image.fromarray(data)
image.save(bio, format='jpeg')
elif fmt == 'png':
image = Image.fromarray(data)
image.save(bio)
image.save(bio, format='png')
else:
raise ValueError(f'Tile file format not supported: {fmt}. '
'Supported formats: fits, jpg, png')

bio.seek(0)
raw_data = bio.read()

return cls(meta, raw_data)
Expand Down

0 comments on commit 2ab7f7a

Please sign in to comment.