Hdf5StubImagePlugin.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #
  2. # The Python Imaging Library
  3. # $Id$
  4. #
  5. # HDF5 stub adapter
  6. #
  7. # Copyright (c) 2000-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 HDF5 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[:8] == b"\x89HDF\r\n\x1a\n"
  24. class HDF5StubImageFile(ImageFile.StubImageFile):
  25. format = "HDF5"
  26. format_description = "HDF5"
  27. def _open(self):
  28. offset = self.fp.tell()
  29. if not _accept(self.fp.read(8)):
  30. raise SyntaxError("Not an HDF file")
  31. self.fp.seek(offset)
  32. # make something up
  33. self.mode = "F"
  34. self._size = 1, 1
  35. loader = self._load()
  36. if loader:
  37. loader.open(self)
  38. def _load(self):
  39. return _handler
  40. def _save(im, fp, filename):
  41. if _handler is None or not hasattr("_handler", "save"):
  42. raise IOError("HDF5 save handler not installed")
  43. _handler.save(im, fp, filename)
  44. # --------------------------------------------------------------------
  45. # Registry
  46. Image.register_open(HDF5StubImageFile.format, HDF5StubImageFile, _accept)
  47. Image.register_save(HDF5StubImageFile.format, _save)
  48. Image.register_extensions(HDF5StubImageFile.format, [".h5", ".hdf"])