imghdr.py 3.5 KB

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