2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library
|
|
|
|
# $Id$
|
|
|
|
#
|
2022-02-15 03:22:46 +03:00
|
|
|
# FITS file handling
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# Copyright (c) 1998-2003 by Fredrik Lundh
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
2022-02-15 03:22:46 +03:00
|
|
|
import math
|
2016-09-24 12:10:46 +03:00
|
|
|
|
2022-02-15 03:22:46 +03:00
|
|
|
from . import Image, ImageFile
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _accept(prefix):
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
return prefix[:6] == b"SIMPLE"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2022-02-15 03:22:46 +03:00
|
|
|
class FitsImageFile(ImageFile.ImageFile):
|
2010-07-31 06:52:47 +04:00
|
|
|
format = "FITS"
|
|
|
|
format_description = "FITS"
|
|
|
|
|
|
|
|
def _open(self):
|
2021-04-14 15:48:27 +03:00
|
|
|
headers = {}
|
|
|
|
while True:
|
|
|
|
header = self.fp.read(80)
|
|
|
|
if not header:
|
2022-12-22 00:51:35 +03:00
|
|
|
msg = "Truncated FITS file"
|
|
|
|
raise OSError(msg)
|
2021-04-14 15:48:27 +03:00
|
|
|
keyword = header[:8].strip()
|
|
|
|
if keyword == b"END":
|
|
|
|
break
|
2023-02-25 08:44:07 +03:00
|
|
|
value = header[8:].split(b"/")[0].strip()
|
2021-04-14 15:48:27 +03:00
|
|
|
if value.startswith(b"="):
|
|
|
|
value = value[1:].strip()
|
|
|
|
if not headers and (not _accept(keyword) or value != b"T"):
|
2022-12-22 00:51:35 +03:00
|
|
|
msg = "Not a FITS file"
|
|
|
|
raise SyntaxError(msg)
|
2021-04-14 15:48:27 +03:00
|
|
|
headers[keyword] = value
|
|
|
|
|
|
|
|
naxis = int(headers[b"NAXIS"])
|
|
|
|
if naxis == 0:
|
2022-12-22 00:51:35 +03:00
|
|
|
msg = "No image data"
|
|
|
|
raise ValueError(msg)
|
2021-04-14 15:48:27 +03:00
|
|
|
elif naxis == 1:
|
|
|
|
self._size = 1, int(headers[b"NAXIS1"])
|
|
|
|
else:
|
|
|
|
self._size = int(headers[b"NAXIS1"]), int(headers[b"NAXIS2"])
|
|
|
|
|
|
|
|
number_of_bits = int(headers[b"BITPIX"])
|
|
|
|
if number_of_bits == 8:
|
|
|
|
self.mode = "L"
|
|
|
|
elif number_of_bits == 16:
|
|
|
|
self.mode = "I"
|
|
|
|
# rawmode = "I;16S"
|
|
|
|
elif number_of_bits == 32:
|
|
|
|
self.mode = "I"
|
|
|
|
elif number_of_bits in (-32, -64):
|
|
|
|
self.mode = "F"
|
|
|
|
# rawmode = "F" if number_of_bits == -32 else "F;64F"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2022-02-15 03:22:46 +03:00
|
|
|
offset = math.ceil(self.fp.tell() / 2880) * 2880
|
|
|
|
self.tile = [("raw", (0, 0) + self.size, offset, (self.mode, 0, -1))]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Registry
|
|
|
|
|
2022-02-15 03:22:46 +03:00
|
|
|
Image.register_open(FitsImageFile.format, FitsImageFile, _accept)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2022-02-15 03:22:46 +03:00
|
|
|
Image.register_extensions(FitsImageFile.format, [".fit", ".fits"])
|