Skip to content

Flip cutout images#180

Open
snbianco wants to merge 4 commits intomainfrom
flip-jpeg
Open

Flip cutout images#180
snbianco wants to merge 4 commits intomainfrom
flip-jpeg

Conversation

@snbianco
Copy link
Copy Markdown
Collaborator

@snbianco snbianco commented Apr 21, 2026

When creating PIL.Image objects or writing cutouts to image formats, vertically flip the image to match the intended orientation of astronomical data.

Added a flip_orientation parameter to get_image_cutouts and write_as_img to allow users to opt in or out. The default is True, but I wanted to keep the legacy option of the "unflipped" version.

@scfleming
Copy link
Copy Markdown
Collaborator

scfleming commented Apr 21, 2026

If this is updating the orientation of the cutouts, how was this missed the first time? Was our comparison between old and new not sufficiently detailed to include orientation checks, along with pixel-level checks I am fairly certain were done? Or was the testing focused on the FITS output and not sufficient coverage of manual testing on the JPG output?

@rlwastro
Copy link
Copy Markdown
Contributor

@scfleming The orientation problem affects only JPEG (or other graphic formats). My guess is that the existing checks did not check the orientation on those images. Checking that the FITS cutouts are correct at the pixel level would not pick up this problem.

@snbianco
Copy link
Copy Markdown
Collaborator Author

snbianco commented Apr 21, 2026

If this is updating the orientation of the cutouts, how was this missed the first time? Was our comparison between old and new not sufficiently detailed to include orientation checks, along with pixel-level checks I am fairly certain were done? Or was the testing focused on the FITS output and not sufficient coverage of manual testing on the JPG output?

Historically, the orientation of image outputs has never been flipped. The new and old version output the same thing, but they were both incorrect.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 21, 2026

Codecov Report

❌ Patch coverage is 98.30508% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.96%. Comparing base (d993436) to head (58f31f2).

Files with missing lines Patch % Lines
astrocut/image_cutout.py 98.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #180      +/-   ##
==========================================
+ Coverage   95.92%   95.96%   +0.04%     
==========================================
  Files          20       20              
  Lines        2232     2279      +47     
==========================================
+ Hits         2141     2187      +46     
- Misses         91       92       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@scfleming
Copy link
Copy Markdown
Collaborator

OK roger that, a good point to take stock of what we test as we develop, that's all. Glad we are getting this fixed with good tests, etc now.

@snbianco
Copy link
Copy Markdown
Collaborator Author

I wasn't fully aware of the convention for flipping images, so thank you @rlwastro for letting me know! I added some assertions to the tests that check that the image was flipped.

@snbianco snbianco marked this pull request as ready for review April 21, 2026 22:01


@pytest.mark.parametrize("colorize", [True, False])
def test_fits_cutout_img_flip(test_images, center_coord, cutout_size, colorize):
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the changes in this file are stylistic. This is the new test!

Comment thread astrocut/image_cutout.py
from pathlib import Path
from typing import List, Optional, Union, Tuple
import warnings
from abc import ABC, abstractmethod
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of style changes in this file as well. The relevant changes have to do with the flip_orientation parameter.

@snbianco snbianco requested a review from havok2063 April 22, 2026 13:53
@havok2063
Copy link
Copy Markdown
Contributor

Do we embed any metadata into the png image cutouts? Like coordinates or wcs or anything like that? If not, we may want to consider that. It may help with verification of correct orientation in the future, or allow users to overlay onto other things.

@snbianco
Copy link
Copy Markdown
Collaborator Author

TIL you can add metadata to images! I love this idea. I added a few keywords that were pretty trivial, but I think something like a wcs (or gwcs in the case of ASDF) would require a little more work and may be outside the scope of this PR. This is something I will add to the backlog though.

Comment thread astrocut/image_cutout.py
Comment on lines +265 to +271
meta.update(
{
"center_ra_deg": self._coordinates.ra.deg.item(),
"center_dec_deg": self._coordinates.dec.deg.item(),
"origin": "STScI/MAST",
"version": __version__,
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also encode the pixel scale used in the cutout? Or cutout size or some info about the boundaries?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added! You can also access the dimensions through the width and height attributes of the Image object, but I figure we can make them more obvious.

@havok2063
Copy link
Copy Markdown
Contributor

I added a few keywords that were pretty trivial, but I think something like a wcs (or gwcs in the case of ASDF) would require a little more work and may be outside the scope of this PR. This is something I will add to the backlog though.

Yeah, I embedded a standard FITS WCS for some png image cutouts with reasonable success. It was accurate enough to reasonably recover and overlay the image onto other FITS. The one caveat is the conversion to/from can be tricky since you have to convert every field to a string. gwcs might be trickier, but may also not be needed for a simple image cutout.

@scfleming
Copy link
Copy Markdown
Collaborator

TIL you can add metadata to images! I love this idea. I added a few keywords that were pretty trivial, but I think something like a wcs (or gwcs in the case of ASDF) would require a little more work and may be outside the scope of this PR. This is something I will add to the backlog though.

I may be incorrect about this, but I thought the older PanSTARRS cutout service provided JPEG output with metadata/WCS embedded in them? I am fairly certain something at MAST had/has this capability, and Rick might know about it....just FYI, I agree adding WCS is a great idea and could be very useful especially with some of the stuff Cobalt team is doing (they might also have capability of exporting JPG with WCS info embedded for overlaying on Aladin), but as a separate PR.

@rlwastro
Copy link
Copy Markdown
Contributor

rlwastro commented Apr 24, 2026

@scfleming Scott is correct, the current fitscut service does add the WCS to JPEG images in the JPEG comments section. This was originally added because it was supported by Aladin, which can display a JPEG with an embedded WCS. It turns out to be pretty easy to access from within Python using PIL.
I do think this is worth doing, even if it is not essential for this particular change. The WCS consists of just strings with the card images (without trailing blanks) for the FITS WCS header keywords.
Probably C code for this is not very helpful, but here is a link to the code in fitscut that adds the WCS header.

@scfleming
Copy link
Copy Markdown
Collaborator

when we add the WCS in the output, you can check with Brett on how they are handling the aladin overlay with Roman gwcs, e.g., I think they are using an approximation of gwcs to handle what FITS supports, which is probably sufficient for use within Aladin especially over a small area, but could to check in with them what they've learned since they are also working on this for exporting JPG from Jdaviz as overlays on Aladin. We'd want astrocut JPG output to have same precision and implementation as what's being done for Jdaviz I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants