You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

imghdr.py 3.5KB

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