Back to index

python3.2  3.2.2
Functions | Variables
imghdr Namespace Reference

Functions

def what
def test_jpeg
def test_png
def test_gif
def test_tiff
def test_rgb
def test_pbm
def test_pgm
def test_ppm
def test_rast
def test_xbm
def test_bmp
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 119 of file imghdr.py.

00119 
00120 def test():
00121     import sys
00122     recursive = 0
00123     if sys.argv[1:] and sys.argv[1] == '-r':
00124         del sys.argv[1:2]
00125         recursive = 1
00126     try:
00127         if sys.argv[1:]:
00128             testall(sys.argv[1:], recursive, 1)
00129         else:
00130             testall(['.'], recursive, 1)
00131     except KeyboardInterrupt:
00132         sys.stderr.write('\n[Interrupted]\n')
00133         sys.exit(1)

Here is the call graph for this function:

def imghdr.test_bmp (   h,
  f 
)

Definition at line 109 of file imghdr.py.

00109 
00110 def test_bmp(h, f):
00111     if h.startswith(b'BM'):
00112         return 'bmp'
00113 
00114 tests.append(test_bmp)
00115 
00116 #--------------------#
00117 # Small test program #
00118 #--------------------#

def imghdr.test_gif (   h,
  f 
)
GIF ('87 and '89 variants)

Definition at line 50 of file imghdr.py.

00050 
00051 def test_gif(h, f):
00052     """GIF ('87 and '89 variants)"""
00053     if h[:6] in (b'GIF87a', b'GIF89a'):
00054         return 'gif'
00055 
00056 tests.append(test_gif)

def imghdr.test_jpeg (   h,
  f 
)
JPEG data in JFIF or Exif format

Definition at line 37 of file imghdr.py.

00037 
00038 def test_jpeg(h, f):
00039     """JPEG data in JFIF or Exif format"""
00040     if h[6:10] in (b'JFIF', b'Exif'):
00041         return 'jpeg'
00042 
00043 tests.append(test_jpeg)

def imghdr.test_pbm (   h,
  f 
)
PBM (portable bitmap)

Definition at line 71 of file imghdr.py.

00071 
00072 def test_pbm(h, f):
00073     """PBM (portable bitmap)"""
00074     if len(h) >= 3 and \
00075         h[0] == ord(b'P') and h[1] in b'14' and h[2] in b' \t\n\r':
00076         return 'pbm'
00077 
00078 tests.append(test_pbm)

Here is the call graph for this function:

def imghdr.test_pgm (   h,
  f 
)
PGM (portable graymap)

Definition at line 79 of file imghdr.py.

00079 
00080 def test_pgm(h, f):
00081     """PGM (portable graymap)"""
00082     if len(h) >= 3 and \
00083         h[0] == ord(b'P') and h[1] in b'25' and h[2] in b' \t\n\r':
00084         return 'pgm'
00085 
00086 tests.append(test_pgm)

Here is the call graph for this function:

def imghdr.test_png (   h,
  f 
)

Definition at line 44 of file imghdr.py.

00044 
00045 def test_png(h, f):
00046     if h.startswith(b'\211PNG\r\n\032\n'):
00047         return 'png'
00048 
00049 tests.append(test_png)

def imghdr.test_ppm (   h,
  f 
)
PPM (portable pixmap)

Definition at line 87 of file imghdr.py.

00087 
00088 def test_ppm(h, f):
00089     """PPM (portable pixmap)"""
00090     if len(h) >= 3 and \
00091         h[0] == ord(b'P') and h[1] in b'36' and h[2] in b' \t\n\r':
00092         return 'ppm'
00093 
00094 tests.append(test_ppm)

Here is the call graph for this function:

def imghdr.test_rast (   h,
  f 
)
Sun raster file

Definition at line 95 of file imghdr.py.

00095 
00096 def test_rast(h, f):
00097     """Sun raster file"""
00098     if h.startswith(b'\x59\xA6\x6A\x95'):
00099         return 'rast'
00100 
00101 tests.append(test_rast)

def imghdr.test_rgb (   h,
  f 
)
SGI image library

Definition at line 64 of file imghdr.py.

00064 
00065 def test_rgb(h, f):
00066     """SGI image library"""
00067     if h.startswith(b'\001\332'):
00068         return 'rgb'
00069 
00070 tests.append(test_rgb)

def imghdr.test_tiff (   h,
  f 
)
TIFF (can be in Motorola or Intel byte order)

Definition at line 57 of file imghdr.py.

00057 
00058 def test_tiff(h, f):
00059     """TIFF (can be in Motorola or Intel byte order)"""
00060     if h[:2] in (b'MM', b'II'):
00061         return 'tiff'
00062 
00063 tests.append(test_tiff)

def imghdr.test_xbm (   h,
  f 
)
X bitmap (X10 or X11)

Definition at line 102 of file imghdr.py.

00102 
00103 def test_xbm(h, f):
00104     """X bitmap (X10 or X11)"""
00105     if h.startswith(b'#define '):
00106         return 'xbm'
00107 
00108 tests.append(test_xbm)

def imghdr.testall (   list,
  recursive,
  toplevel 
)

Definition at line 134 of file imghdr.py.

00134 
00135 def testall(list, recursive, toplevel):
00136     import sys
00137     import os
00138     for filename in list:
00139         if os.path.isdir(filename):
00140             print(filename + '/:', end=' ')
00141             if recursive or toplevel:
00142                 print('recursing down:')
00143                 import glob
00144                 names = glob.glob(os.path.join(filename, '*'))
00145                 testall(names, recursive, 0)
00146             else:
00147                 print('*** directory (use -r) ***')
00148         else:
00149             print(filename + ':', end=' ')
00150             sys.stdout.flush()
00151             try:
00152                 print(what(filename))
00153             except IOError:
00154                 print('*** not found ***')

Here is the call graph for this function:

Here is the caller graph for this function:

def imghdr.what (   file,
  h = None 
)

Definition at line 9 of file imghdr.py.

00009 
00010 def what(file, h=None):
00011     if h is None:
00012         if isinstance(file, str):
00013             f = open(file, 'rb')
00014             h = f.read(32)
00015         else:
00016             location = file.tell()
00017             h = file.read(32)
00018             file.seek(location)
00019             f = None
00020     else:
00021         f = None
00022     try:
00023         for tf in tests:
00024             res = tf(h, f)
00025             if res:
00026                 return res
00027     finally:
00028         if f: f.close()
00029     return None
00030 
00031 
00032 #---------------------------------#
00033 # Subroutines per image file type #
00034 #---------------------------------#

Here is the caller graph for this function:


Variable Documentation

list imghdr.__all__ = ["what"]

Definition at line 3 of file imghdr.py.

list imghdr.tests = []

Definition at line 35 of file imghdr.py.