FitsStubImagePlugin.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #
  2. # The Python Imaging Library
  3. # $Id$
  4. #
  5. # FITS stub adapter
  6. #
  7. # Copyright (c) 1998-2003 by Fredrik Lundh
  8. #
  9. # See the README file for information on usage and redistribution.
  10. #
  11. from . import Image, ImageFile
  12. _handler = None
  13. def register_handler(handler):
  14. """
  15. Install application-specific FITS image handler.
  16. :param handler: Handler object.
  17. """
  18. global _handler
  19. _handler = handler
  20. # --------------------------------------------------------------------
  21. # Image adapter
  22. def _accept(prefix):
  23. return prefix[:6] == b"SIMPLE"
  24. class FITSStubImageFile(ImageFile.StubImageFile):
  25. format = "FITS"
  26. format_description = "FITS"
  27. def _open(self):
  28. offset = self.fp.tell()
  29. if not _accept(self.fp.read(6)):
  30. raise SyntaxError("Not a FITS file")
  31. # FIXME: add more sanity checks here; mandatory header items
  32. # include SIMPLE, BITPIX, NAXIS, etc.
  33. self.fp.seek(offset)
  34. # make something up
  35. self.mode = "F"
  36. self._size = 1, 1
  37. loader = self._load()
  38. if loader:
  39. loader.open(self)
  40. def _load(self):
  41. return _handler
  42. def _save(im, fp, filename):
  43. if _handler is None or not hasattr("_handler", "save"):
  44. raise OSError("FITS save handler not installed")
  45. _handler.save(im, fp, filename)
  46. # --------------------------------------------------------------------
  47. # Registry
  48. Image.register_open(FITSStubImageFile.format, FITSStubImageFile, _accept)
  49. Image.register_save(FITSStubImageFile.format, _save)
  50. Image.register_extensions(FITSStubImageFile.format, [".fit", ".fits"])