Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ svo_fps
- Add ``get_filter_metadata`` to allow retrieval of filter metadata. [#3528]
- Add ``get_zeropoint`` to allow retrieval of filter zeropoints and allow kwarg passing to ``get_filter_metadata``. [#3545]

hips2fits
^^^^^^^^^

- ``Pillow`` is now an optional dependency: importing ``astroquery.hips2fits`` no
longer crashes when Pillow is not installed. A clear ``ImportError`` is raised
only when a jpg/png response is actually decoded. [#3619]

heasarc
^^^^^^^
- Add ``query_constraints`` to allow querying of different catalog columns. [#3403]
Expand Down
9 changes: 7 additions & 2 deletions astroquery/hips2fits/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from astropy.io import fits
from astropy.coordinates import Angle
import astropy.units as u
from PIL import Image

from astropy.utils.exceptions import AstropyUserWarning

from ..query import BaseQuery
Expand Down Expand Up @@ -325,6 +323,13 @@ def _parse_result(self, response, verbose, format):
return hdul
else:
# jpg/png formats
try:
from PIL import Image
except ImportError:
raise ImportError(
"Pillow is required to decode jpg/png responses from hips2fits. "
"Install it with: pip install Pillow"
)
bytes = io.BytesIO(bytes_str)
im = Image.open(bytes)
data = np.asarray(im)
Expand Down
25 changes: 25 additions & 0 deletions astroquery/hips2fits/tests/test_hips2fits.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import sys
from unittest.mock import MagicMock, patch

import pytest
from astropy import wcs as astropy_wcs
from matplotlib.colors import Colormap
from astropy.coordinates import Angle, Longitude, Latitude
Expand All @@ -7,6 +11,27 @@
from ..core import hips2fits


class TestHips2fits:

def test_jpg_format_requires_pillow(self):
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = b'fake_image_bytes'

with patch.dict(sys.modules, {'PIL': None, 'PIL.Image': None}):
with pytest.raises(ImportError, match="Pillow is required"):
hips2fits._parse_result(mock_response, verbose=False, format='jpg')

def test_png_format_requires_pillow(self):
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = b'fake_image_bytes'

with patch.dict(sys.modules, {'PIL': None, 'PIL.Image': None}):
with pytest.raises(ImportError, match="Pillow is required"):
hips2fits._parse_result(mock_response, verbose=False, format='png')


class TestHips2fitsRemote:

# Create a new WCS astropy object
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ all = [
"boto3",
"botocore",
"regions>=0.5",
"Pillow",
]

[build-system]
Expand Down
Loading