home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Lib / imghdr.py < prev    next >
Text File  |  2000-12-21  |  3KB  |  154 lines

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