Skip to main content

Python library that converts astronomical FITS images into color/grayscale images.

Project description

trilogy

Python library for converting astronomical FITS images into beautiful color or grayscale images.

ascl:1508.009

Cosmic Horseshoe
Cosmic Horseshoe gravitational lens imaged by Hubble WFC3
RGB composite using filters F475W (blue), F606W (green), and F814W (red)

Try it! Binder

Installation

pip install trilogy

Or from source:

git clone https://github.com/oliveirara/trilogy.git
cd trilogy
pip install -e .

Quick Start

Grayscale Image (Single Band)

from trilogy import Trilogy

trilogy = Trilogy(
    images="galaxy.fits",
    outname="galaxy"
)
img = trilogy.save()  # Saves galaxy.png

RGB Color Image (Multi-Band)

from trilogy import Trilogy

trilogy = Trilogy(
    images={
        "R": ["i_band.fits"],
        "G": ["r_band.fits"],
        "B": ["g_band.fits"]
    },
    outname="galaxy_rgb"
)
img = trilogy.save()  # Saves galaxy_rgb.png

Adjusting Brightness and Contrast

trilogy = Trilogy(
    images="galaxy.fits",
    outname="galaxy_adjusted",
    noiselum=0.10,      # Lower = darker background (0.05-0.5)
    satpercent=0.0001   # Lower = less saturation (0.0001-0.01)
)

Boosting Colors (RGB)

trilogy = Trilogy(
    images={"R": ["r.fits"], "G": ["g.fits"], "B": ["b.fits"]},
    outname="colorful",
    colorsatfac=1.5     # >1 boosts colors, <1 reduces
)

Key Parameters

Essential Parameters

Parameter Default Range Effect
noiselum 0.15 0.05-0.5 Background brightness (lower = darker)
satpercent 0.001 0.0001-0.01 % of pixels to saturate (lower = more detail)
colorsatfac 1.0 0.5-2.0 Color saturation (RGB only, >1 = more vivid)

Common Adjustments

Image too dark?

noiselum=0.25        # Increase background brightness
satpercent=0.005     # Allow more saturation

Image too bright/washed out?

noiselum=0.08        # Decrease background brightness
satpercent=0.0001    # Preserve more highlights

Colors too weak? (RGB only)

colorsatfac=1.5      # Boost color saturation

Advanced Usage

Per-Channel Control (RGB)

trilogy = Trilogy(
    images={"R": [...], "G": [...], "B": [...]},
    noiselums={
        "R": 0.20,   # Brighter red channel
        "G": 0.15,   # Balanced green
        "B": 0.10    # Darker blue channel
    }
)

Combining Multiple Images

# Average multiple exposures
trilogy = Trilogy(
    images=["exposure1.fits", "exposure2.fits", "exposure3.fits"],
    combine="average"
)

# Or sum them
trilogy = Trilogy(
    images=["exp1.fits", "exp2.fits"],
    combine="sum"
)

Using Configuration Object

from trilogy import Trilogy, TrilogyConfig

config = TrilogyConfig(
    noiselum=0.12,
    satpercent=0.0005,
    colorsatfac=1.3,
    samplesize=2000,
    combine="average"
)

trilogy = Trilogy(images=my_images, config=config)

Manual Control (Disable Auto-Adjustment)

By default, trilogy automatically adjusts problematic parameters. To use exact values:

trilogy = Trilogy(
    images="galaxy.fits",
    noiselum=0.147,      # Will use exactly this value
    satpercent=0.000823,
    auto_adjust=False    # Disable auto-adjustment
)

Use auto_adjust=False when:

  • Replicating results from papers/publications
  • You've manually fine-tuned parameters visually
  • You need specific parameter values

Jupyter Notebooks

Trilogy works seamlessly in notebooks:

from trilogy import Trilogy

t = Trilogy(images="galaxy.fits", noiselum=0.15)
img = t.run()  # Returns PIL Image, displays automatically

See examples/with_notebook/notebook.ipynb for complete examples.

Examples

The examples/ directory contains real FITS data from various surveys:

  • cosmic_horseshoe/ - HST WFC3 RGB composite
  • single_band/ - Grayscale examples (CS82, CFHTLenS, DES)
  • multiple_bands/ - RGB examples (HSC, KIDS, Legacy Survey, RCSLenS)
  • with_notebook/ - Jupyter notebook examples

Each example includes a example.py script you can run:

cd examples/cosmic_horseshoe
python example.py

All Parameters

Input/Output

  • images: str, list, or dict - Input FITS file(s)
  • indir: Path - Input directory (default: current directory)
  • outdir: Path - Output directory (default: current directory)
  • outname: str - Output filename without extension

Image Scaling

  • noiselum: float - Noise luminosity 0-1 (default: 0.15)
  • noiselums: dict - Per-channel noise luminosity
  • satpercent: float - Percentage of pixels to saturate (default: 0.001)
  • colorsatfac: float - Color saturation factor (default: 1.0)
  • noisesig: float - Noise sigma for output (default: 1.0)
  • noisesig0: float - Noise sigma for measurement (default: 2.0)
  • correctbias: bool - Correct for background bias (default: False)

Processing

  • combine: "average" or "sum" - How to combine multiple images (default: "average")
  • samplesize: int - Sample region size for determining scaling (default: 1000)
  • stampsize: int - Processing stamp size (default: 1000)
  • maxstampsize: int - Maximum stamp size (default: 6000)
  • bscale: float - Multiply all pixel values (default: 1.0)
  • bzero: float - Add to all pixel values (default: 0.0)

Advanced

  • auto_adjust: bool - Automatically adjust problematic parameters (default: True)
  • noise: float - Manual noise level (overrides automatic detection)
  • saturate: float - Manual saturation level (overrides automatic)
  • invert: bool - Invert luminosity (default: False)
  • legend: bool - Add filter legend to RGB images (default: False)

Supported File Formats

  • Standard FITS (.fits)
  • Compressed FITS (.fits.gz, .fits.fz)
  • Multi-extension FITS (specify with image.fits[1])
  • Automatic extension detection

Requirements

  • Python >= 3.11
  • astropy >= 6.1.7
  • numpy >= 2.2.4
  • pillow >= 11.2.1
  • scipy >= 1.15.2

Troubleshooting

Image is too dark

trilogy = Trilogy(images=..., noiselum=0.25, satpercent=0.005)

Image is too bright/washed out

trilogy = Trilogy(images=..., noiselum=0.08, satpercent=0.0001)

Colors are too weak (RGB)

trilogy = Trilogy(images=..., colorsatfac=1.5)

Getting warnings about parameter adjustments

The auto-adjustment system is helping you avoid problematic values. To use exact values:

trilogy = Trilogy(images=..., your_params, auto_adjust=False)

Image appears blank/black

  • Check that FITS files have data (not empty)
  • Try increasing noiselum and satpercent
  • Verify FITS extension if using multi-extension files

Images with many NaN pixels

Trilogy automatically handles NaN (Not a Number) and Inf pixels:

Smart NaN handling:

  • NaN/Inf pixels are set to 0 (black) in output
  • Statistics calculated only from valid pixels
  • Warning displayed if >10% of pixels are NaN
  • No crashes or numerical errors from NaN pixels

Example output:

⚠️  Warning: 50.0% of pixels are NaN/Inf

Recommendation:

  • Images with >50% NaN pixels will appear mostly black
  • Use quality filtering before processing (e.g., filter-empty-cutouts.py)
  • Consider cropping to regions with valid data

Why NaN pixels occur:

  • Edge effects from mosaicking/reprojection
  • Missing detector data
  • Masked pixels in processed images
  • Image cutouts extending beyond original field

Resources

Citation

If you use trilogy in your research, please cite:

@MISC{2012ascl.soft08009C,
  author = {{Coe}, D.},
  title = "{Trilogy: Image composition software}",
  keywords = {Software},
  year = 2012,
  month = aug,
  eid = {ascl:1508.009},
  pages = {ascl:1508.009},
  archivePrefix = {ascl},
  eprint = {1508.009},
  adsurl = {https://ui.adsabs.harvard.edu/abs/2012ascl.soft08009C},
}

License

See LICENSE file.

Contributing

Contributions are welcome! Please open an issue or pull request on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trilogy-1.1.0.tar.gz (39.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trilogy-1.1.0-py3-none-any.whl (36.4 kB view details)

Uploaded Python 3

File details

Details for the file trilogy-1.1.0.tar.gz.

File metadata

  • Download URL: trilogy-1.1.0.tar.gz
  • Upload date:
  • Size: 39.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for trilogy-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a381554e721f3852c4d3f1751af2b5043a3c9a4165c9e28b078959697bf0b4b2
MD5 6eb0a33f233abbfe5ddaecce107b9259
BLAKE2b-256 5789e762ca9b8522ff04910f6a7622c0e4ded8288d13ad2b58bc95195a99baa0

See more details on using hashes here.

Provenance

The following attestation bundles were made for trilogy-1.1.0.tar.gz:

Publisher: publish.yml on oliveirara/trilogy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trilogy-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: trilogy-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 36.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for trilogy-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d3be7cb4d8fcbfef107085bd5899b7924ea415e16461e1bc2d2b75930d8ba3c9
MD5 0032e1d6e6ba01ceb1af2405b3ec46c2
BLAKE2b-256 cf2c190e4b00801b444b1eab334fcbfa641aeb17b0d694275686086e67d1373b

See more details on using hashes here.

Provenance

The following attestation bundles were made for trilogy-1.1.0-py3-none-any.whl:

Publisher: publish.yml on oliveirara/trilogy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page