XVThumbImagePlugin.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # XV Thumbnail file handler by Charles E. "Gene" Cash
  6. # (gcash@magicnet.net)
  7. #
  8. # see xvcolor.c and xvbrowse.c in the sources to John Bradley's XV,
  9. # available from ftp://ftp.cis.upenn.edu/pub/xv/
  10. #
  11. # history:
  12. # 98-08-15 cec created (b/w only)
  13. # 98-12-09 cec added color palette
  14. # 98-12-28 fl added to PIL (with only a few very minor modifications)
  15. #
  16. # To do:
  17. # FIXME: make save work (this requires quantization support)
  18. #
  19. from . import Image, ImageFile, ImagePalette
  20. from ._binary import o8
  21. _MAGIC = b"P7 332"
  22. # standard color palette for thumbnails (RGB332)
  23. PALETTE = b""
  24. for r in range(8):
  25. for g in range(8):
  26. for b in range(4):
  27. PALETTE = PALETTE + (
  28. o8((r * 255) // 7) + o8((g * 255) // 7) + o8((b * 255) // 3)
  29. )
  30. def _accept(prefix):
  31. return prefix[:6] == _MAGIC
  32. ##
  33. # Image plugin for XV thumbnail images.
  34. class XVThumbImageFile(ImageFile.ImageFile):
  35. format = "XVThumb"
  36. format_description = "XV thumbnail image"
  37. def _open(self):
  38. # check magic
  39. if not _accept(self.fp.read(6)):
  40. msg = "not an XV thumbnail file"
  41. raise SyntaxError(msg)
  42. # Skip to beginning of next line
  43. self.fp.readline()
  44. # skip info comments
  45. while True:
  46. s = self.fp.readline()
  47. if not s:
  48. msg = "Unexpected EOF reading XV thumbnail file"
  49. raise SyntaxError(msg)
  50. if s[0] != 35: # ie. when not a comment: '#'
  51. break
  52. # parse header line (already read)
  53. s = s.strip().split()
  54. self._mode = "P"
  55. self._size = int(s[0]), int(s[1])
  56. self.palette = ImagePalette.raw("RGB", PALETTE)
  57. self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), (self.mode, 0, 1))]
  58. # --------------------------------------------------------------------
  59. Image.register_open(XVThumbImageFile.format, XVThumbImageFile, _accept)