Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
imghdr Namespace Reference

Functions

def what
 
def test_rgb
 
def test_gif
 
def test_pbm
 
def test_pgm
 
def test_ppm
 
def test_tiff
 
def test_rast
 
def test_xbm
 
def test_jpeg
 
def test_bmp
 
def test_png
 
def test
 
def testall
 

Variables

list __all__ = ["what"]
 
list tests = []
 

Detailed Description

Recognize image file formats based on their first few bytes.

Function Documentation

def imghdr.test ( )

Definition at line 120 of file imghdr.py.

References testall().

121 def test():
122  import sys
123  recursive = 0
124  if sys.argv[1:] and sys.argv[1] == '-r':
125  del sys.argv[1:2]
126  recursive = 1
127  try:
128  if sys.argv[1:]:
129  testall(sys.argv[1:], recursive, 1)
130  else:
131  testall(['.'], recursive, 1)
132  except KeyboardInterrupt:
133  sys.stderr.write('\n[Interrupted]\n')
134  sys.exit(1)
def imghdr.test_bmp (   h,
  f 
)

Definition at line 104 of file imghdr.py.

105 def test_bmp(h, f):
106  if h[:2] == 'BM':
107  return 'bmp'
108 
109 tests.append(test_bmp)
def imghdr.test_gif (   h,
  f 
)
GIF ('87 and '89 variants)

Definition at line 44 of file imghdr.py.

44 
45 def test_gif(h, f):
46  """GIF ('87 and '89 variants)"""
47  if h[:6] in ('GIF87a', 'GIF89a'):
48  return 'gif'
49 
50 tests.append(test_gif)
def imghdr.test_jpeg (   h,
  f 
)
JPEG data in JFIF format

Definition at line 97 of file imghdr.py.

97 
98 def test_jpeg(h, f):
99  """JPEG data in JFIF format"""
100  if h[6:10] == 'JFIF':
101  return 'jpeg'
102 
103 tests.append(test_jpeg)
def imghdr.test_pbm (   h,
  f 
)
PBM (portable bitmap)

Definition at line 51 of file imghdr.py.

51 
52 def test_pbm(h, f):
53  """PBM (portable bitmap)"""
54  if len(h) >= 3 and \
55  h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r':
56  return 'pbm'
57 
58 tests.append(test_pbm)
def imghdr.test_pgm (   h,
  f 
)
PGM (portable graymap)

Definition at line 59 of file imghdr.py.

59 
60 def test_pgm(h, f):
61  """PGM (portable graymap)"""
62  if len(h) >= 3 and \
63  h[0] == 'P' and h[1] in '25' and h[2] in ' \t\n\r':
64  return 'pgm'
65 
66 tests.append(test_pgm)
def imghdr.test_png (   h,
  f 
)

Definition at line 110 of file imghdr.py.

111 def test_png(h, f):
112  if h[:8] == "\211PNG\r\n\032\n":
113  return 'png'
114 
115 tests.append(test_png)
116 
117 #--------------------#
118 # Small test program #
119 #--------------------#
def imghdr.test_ppm (   h,
  f 
)
PPM (portable pixmap)

Definition at line 67 of file imghdr.py.

67 
68 def test_ppm(h, f):
69  """PPM (portable pixmap)"""
70  if len(h) >= 3 and \
71  h[0] == 'P' and h[1] in '36' and h[2] in ' \t\n\r':
72  return 'ppm'
73 
74 tests.append(test_ppm)
def imghdr.test_rast (   h,
  f 
)
Sun raster file

Definition at line 82 of file imghdr.py.

82 
83 def test_rast(h, f):
84  """Sun raster file"""
85  if h[:4] == '\x59\xA6\x6A\x95':
86  return 'rast'
87 
88 tests.append(test_rast)
def imghdr.test_rgb (   h,
  f 
)
SGI image library

Definition at line 37 of file imghdr.py.

37 
38 def test_rgb(h, f):
39  """SGI image library"""
40  if h[:2] == '\001\332':
41  return 'rgb'
42 
43 tests.append(test_rgb)
def imghdr.test_tiff (   h,
  f 
)
TIFF (can be in Motorola or Intel byte order)

Definition at line 75 of file imghdr.py.

75 
76 def test_tiff(h, f):
77  """TIFF (can be in Motorola or Intel byte order)"""
78  if h[:2] in ('MM', 'II'):
79  return 'tiff'
80 
81 tests.append(test_tiff)
def imghdr.test_xbm (   h,
  f 
)
X bitmap (X10 or X11)

Definition at line 89 of file imghdr.py.

89 
90 def test_xbm(h, f):
91  """X bitmap (X10 or X11)"""
92  s = '#define '
93  if h[:len(s)] == s:
94  return 'xbm'
95 
96 tests.append(test_xbm)
def imghdr.testall (   list,
  recursive,
  toplevel 
)

Definition at line 135 of file imghdr.py.

References glob.glob(), and what().

136 def testall(list, recursive, toplevel):
137  import sys
138  import os
139  for filename in list:
140  if os.path.isdir(filename):
141  print filename + '/:',
142  if recursive or toplevel:
143  print 'recursing down:'
144  import glob
145  names = glob.glob(os.path.join(filename, '*'))
146  testall(names, recursive, 0)
147  else:
148  print '*** directory (use -r) ***'
149  else:
150  print filename + ':',
151  sys.stdout.flush()
152  try:
153  print what(filename)
154  except IOError:
155  print '*** not found ***'
def imghdr.what (   file,
  h = None 
)

Definition at line 9 of file imghdr.py.

References aifc.open().

9 
10 def what(file, h=None):
11  if h is None:
12  if type(file) == type(''):
13  f = open(file, 'rb')
14  h = f.read(32)
15  else:
16  location = file.tell()
17  h = file.read(32)
18  file.seek(location)
19  f = None
20  else:
21  f = None
22  try:
23  for tf in tests:
24  res = tf(h, f)
25  if res:
26  return res
27  finally:
28  if f: f.close()
29  return None
30 
31 
32 #---------------------------------#
33 # Subroutines per image file type #
34 #---------------------------------#

Variable Documentation

list __all__ = ["what"]

Definition at line 3 of file imghdr.py.

list tests = []

Definition at line 35 of file imghdr.py.