diff --git a/.travis.yml b/.travis.yml index f3d0a6f..b0ff151 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,9 @@ sudo: false python: - 2.7 - - 3.5 - 3.6 + - 3.7 + - 3.8 env: matrix: diff --git a/pyavm/avm.py b/pyavm/avm.py index bccfb17..07823de 100644 --- a/pyavm/avm.py +++ b/pyavm/avm.py @@ -436,7 +436,7 @@ def from_xml(cls, xml): return self - def to_wcs(self, use_full_header=False, target_image=None): + def to_wcs(self, use_full_header=False, target_image=None, target_shape=None): """ Convert AVM projection information into a Astropy WCS object. @@ -451,6 +451,11 @@ def to_wcs(self, use_full_header=False, target_image=None): the AVM was defined. The `target_image` option can be used to pass the path of an image from which the size will be used to re-scale the WCS. + target_shape : tuple of ``(nx: int, ny: int)``, optional + Serves the same function as `target_image`, but for cases where the + target dimensions are known without having to open and read an image + file. If both this and `target_image` are specified, `target_image` + takes precedence. """ if not astropy_installed: @@ -548,17 +553,16 @@ def to_wcs(self, use_full_header=False, target_image=None): # If `target_image` is set, we have to rescale the reference pixel and # the scale if target_image is not None: - - # Find target image size from PIL import Image - nx, ny = Image.open(target_image).size + target_shape = Image.open(target_image).size + if target_shape is not None: if self.Spatial.ReferenceDimension is None: raise ValueError("Spatial.ReferenceDimension should be set in order to determine scale in target image") # Find scale in x and y - scale_x = nx / float(wcs_naxis1) - scale_y = ny / float(wcs_naxis2) + scale_x = target_shape[0] / float(wcs_naxis1) + scale_y = target_shape[1] / float(wcs_naxis2) # Check that scales are consistent if abs(scale_x - scale_y) / (scale_x + scale_y) * 2. < 0.01: @@ -570,8 +574,8 @@ def to_wcs(self, use_full_header=False, target_image=None): wcs.wcs.crpix *= scale if hasattr(wcs, 'naxis1'): # PyWCS and Astropy < 0.4 - wcs.naxis1 = nx - wcs.naxis2 = ny + wcs.naxis1 = target_shape[0] + wcs.naxis2 = target_shape[1] return wcs diff --git a/pyavm/tests/test_main.py b/pyavm/tests/test_main.py index ddac1aa..6078036 100644 --- a/pyavm/tests/test_main.py +++ b/pyavm/tests/test_main.py @@ -58,6 +58,15 @@ def test_to_wcs_target_image(filename, tmpdir): a.to_wcs(target_image=image_file) +@pytest.mark.parametrize('filename', XML_FILES_WCS) +def test_to_wcs_target_shape(filename, tmpdir): + pytest.importorskip('PIL') + pytest.importorskip('astropy') + a = AVM.from_xml_file(filename) + a.Spatial.ReferenceDimension = (30, 30) + a.to_wcs(target_shape=(2, 2)) + + @pytest.mark.parametrize('filename', NO_WCS) def test_to_wcs_nowcs(filename): pytest.importorskip('astropy')