pylegs.io.compress_fits_image#

compress_fits_image(file: str | Path | BytesIO, save_path: str | Path = None, overwrite: bool = True, compress_type: Literal['RICE_1', 'RICE_ONE', 'PLIO_1', 'GZIP_1', 'GZIP_2', 'HCOMPRESS_1', 'NOCOMPRESS'] = 'NOCOMPRESS', hcomp_scale: int = 3, quantize_level: int = 10, quantize_method: Literal[-1, 1, 2] = -1, tile_shape: Tuple[int, int] = None, dither_seed: int = 0, ext: int = 0)[source]#

Compress a FITS image and optionally save the compressed image to a file.

Parameters:
filePathOrFile

The path to the FITS file to be compressed. This can be a string or a pathlib.Path object.

save_pathstr or Path, optional

The path where the compressed FITS image will be saved. If None, the compressed image is not saved. Defaults to None.

overwritebool, optional

If True, overwrite the existing file at save_path. Defaults to True.

compress_typestr, optional

The compression algorithm to use. Supported values include 'HCOMPRESS_1', 'RICE_1', 'GZIP_1', 'PLIO_1'. Defaults to ‘HCOMPRESS_1’.

hcomp_scaleint, optional

The scale parameter for HCOMPRESS. Defaults to 3.

quantize_levelint, optional

The quantization level for compression. Defaults to 10.

quantize_methodint, optional

The quantization method to use. Defaults to -1.

extint, optional

The ext of the FITS file to compress. Defaults to 0.

random_seedint, optional

The seed for random number generation used in dithering. Defaults to 42.

Returns:
fits.CompImageHDU

The compressed image HDU.

Raises:
ValueError

If the specified FITS ext is out of range.

Notes

If save_path is provided and overwrite is True, the compressed image will be saved to the specified path. Otherwise, the function returns the compressed image HDU.

Examples

>>> compress_fits_image('input.fits', save_path='compressed.fits', compress_type='RICE_1')
<astropy.io.fits.hdu.compressed.CompImageHDU object at 0x...>