/Lib/imghdr.py

http://unladen-swallow.googlecode.com/ · Python · 161 lines · 130 code · 19 blank · 12 comment · 29 complexity · bda58144594866623a38b54a87a3d448 MD5 · raw file

  1. """Recognize image file formats based on their first few bytes."""
  2. __all__ = ["what"]
  3. #-------------------------#
  4. # Recognize image headers #
  5. #-------------------------#
  6. def what(file, h=None):
  7. if h is None:
  8. if isinstance(file, basestring):
  9. f = open(file, 'rb')
  10. h = f.read(32)
  11. else:
  12. location = file.tell()
  13. h = file.read(32)
  14. file.seek(location)
  15. f = None
  16. else:
  17. f = None
  18. try:
  19. for tf in tests:
  20. res = tf(h, f)
  21. if res:
  22. return res
  23. finally:
  24. if f: f.close()
  25. return None
  26. #---------------------------------#
  27. # Subroutines per image file type #
  28. #---------------------------------#
  29. tests = []
  30. def test_jpeg(h, f):
  31. """JPEG data in JFIF format"""
  32. if h[6:10] == 'JFIF':
  33. return 'jpeg'
  34. tests.append(test_jpeg)
  35. def test_exif(h, f):
  36. """JPEG data in Exif format"""
  37. if h[6:10] == 'Exif':
  38. return 'jpeg'
  39. tests.append(test_exif)
  40. def test_png(h, f):
  41. if h[:8] == "\211PNG\r\n\032\n":
  42. return 'png'
  43. tests.append(test_png)
  44. def test_gif(h, f):
  45. """GIF ('87 and '89 variants)"""
  46. if h[:6] in ('GIF87a', 'GIF89a'):
  47. return 'gif'
  48. tests.append(test_gif)
  49. def test_tiff(h, f):
  50. """TIFF (can be in Motorola or Intel byte order)"""
  51. if h[:2] in ('MM', 'II'):
  52. return 'tiff'
  53. tests.append(test_tiff)
  54. def test_rgb(h, f):
  55. """SGI image library"""
  56. if h[:2] == '\001\332':
  57. return 'rgb'
  58. tests.append(test_rgb)
  59. def test_pbm(h, f):
  60. """PBM (portable bitmap)"""
  61. if len(h) >= 3 and \
  62. h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r':
  63. return 'pbm'
  64. tests.append(test_pbm)
  65. def test_pgm(h, f):
  66. """PGM (portable graymap)"""
  67. if len(h) >= 3 and \
  68. h[0] == 'P' and h[1] in '25' and h[2] in ' \t\n\r':
  69. return 'pgm'
  70. tests.append(test_pgm)
  71. def test_ppm(h, f):
  72. """PPM (portable pixmap)"""
  73. if len(h) >= 3 and \
  74. h[0] == 'P' and h[1] in '36' and h[2] in ' \t\n\r':
  75. return 'ppm'
  76. tests.append(test_ppm)
  77. def test_rast(h, f):
  78. """Sun raster file"""
  79. if h[:4] == '\x59\xA6\x6A\x95':
  80. return 'rast'
  81. tests.append(test_rast)
  82. def test_xbm(h, f):
  83. """X bitmap (X10 or X11)"""
  84. s = '#define '
  85. if h[:len(s)] == s:
  86. return 'xbm'
  87. tests.append(test_xbm)
  88. def test_bmp(h, f):
  89. if h[:2] == 'BM':
  90. return 'bmp'
  91. tests.append(test_bmp)
  92. #--------------------#
  93. # Small test program #
  94. #--------------------#
  95. def test():
  96. import sys
  97. recursive = 0
  98. if sys.argv[1:] and sys.argv[1] == '-r':
  99. del sys.argv[1:2]
  100. recursive = 1
  101. try:
  102. if sys.argv[1:]:
  103. testall(sys.argv[1:], recursive, 1)
  104. else:
  105. testall(['.'], recursive, 1)
  106. except KeyboardInterrupt:
  107. sys.stderr.write('\n[Interrupted]\n')
  108. sys.exit(1)
  109. def testall(list, recursive, toplevel):
  110. import sys
  111. import os
  112. for filename in list:
  113. if os.path.isdir(filename):
  114. print filename + '/:',
  115. if recursive or toplevel:
  116. print 'recursing down:'
  117. import glob
  118. names = glob.glob(os.path.join(filename, '*'))
  119. testall(names, recursive, 0)
  120. else:
  121. print '*** directory (use -r) ***'
  122. else:
  123. print filename + ':',
  124. sys.stdout.flush()
  125. try:
  126. print what(filename)
  127. except IOError:
  128. print '*** not found ***'