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
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ 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
^^^^^^^^^

- Fix the import error for not available optional dependency Pillow. 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: # pragma: no cover
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
40 changes: 40 additions & 0 deletions astroquery/hips2fits/tests/test_hips2fits.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import io
from unittest.mock import MagicMock

import numpy as np
import pytest
from astropy import wcs as astropy_wcs
from matplotlib.colors import Colormap
from astropy.coordinates import Angle, Longitude, Latitude
import astropy.units as u

from ..core import hips2fits

try:
from PIL import Image # noqa: F401
HAS_PILLOW = True
except ImportError:
HAS_PILLOW = False


class TestHips2fits:

@pytest.mark.skipif(HAS_PILLOW, reason="Pillow is installed; the missing-Pillow path cannot be exercised")
@pytest.mark.parametrize("image_format", ["jpg", "png"])
def test_image_format_requires_pillow(self, image_format):
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = b'fake_image_bytes'

with pytest.raises(ImportError, match="Pillow is required"):
hips2fits._parse_result(mock_response, verbose=False, format=image_format)

@pytest.mark.skipif(not HAS_PILLOW, reason="Pillow is required to decode jpg/png responses")
@pytest.mark.parametrize("image_format", ["jpg", "png"])
def test_image_format_decoded_with_pillow(self, image_format):
buffer = io.BytesIO()
Image.new('RGB', (4, 3), color=(255, 0, 0)).save(
buffer, format='JPEG' if image_format == 'jpg' else 'PNG')

mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = buffer.getvalue()

data = hips2fits._parse_result(mock_response, verbose=False, format=image_format)

assert isinstance(data, np.ndarray)
assert data.shape == (3, 4, 3)


class TestHips2fitsRemote:

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