14
14
#-------------------------#
15
15
16
16
def what (file , h = None ):
17
+ """Return the type of image contained in a file or byte stream."""
17
18
f = None
18
19
try :
19
20
if h is None :
@@ -40,7 +41,7 @@ def what(file, h=None):
40
41
tests = []
41
42
42
43
def test_jpeg (h , f ):
43
- """JPEG data with JFIF or Exif markers; and raw JPEG"""
44
+ """Test for JPEG data with JFIF or Exif markers; and raw JPEG. """
44
45
if h [6 :10 ] in (b'JFIF' , b'Exif' ):
45
46
return 'jpeg'
46
47
elif h [:4 ] == b'\xff \xd8 \xff \xdb ' :
@@ -49,83 +50,87 @@ def test_jpeg(h, f):
49
50
tests .append (test_jpeg )
50
51
51
52
def test_png (h , f ):
53
+ """Verify if the image is a PNG."""
52
54
if h .startswith (b'\211 PNG\r \n \032 \n ' ):
53
55
return 'png'
54
56
55
57
tests .append (test_png )
56
58
57
59
def test_gif (h , f ):
58
- """GIF ('87 and '89 variants)"""
60
+ """Verify if the image is a GIF ('87 or '89 variants). """
59
61
if h [:6 ] in (b'GIF87a' , b'GIF89a' ):
60
62
return 'gif'
61
63
62
64
tests .append (test_gif )
63
65
64
66
def test_tiff (h , f ):
65
- """TIFF (can be in Motorola or Intel byte order)"""
67
+ """Verify if the image is a TIFF (can be in Motorola or Intel byte order). """
66
68
if h [:2 ] in (b'MM' , b'II' ):
67
69
return 'tiff'
68
70
69
71
tests .append (test_tiff )
70
72
71
73
def test_rgb (h , f ):
72
- """SGI image library"""
74
+ """test for the SGI image library. """
73
75
if h .startswith (b'\001 \332 ' ):
74
76
return 'rgb'
75
77
76
78
tests .append (test_rgb )
77
79
78
80
def test_pbm (h , f ):
79
- """PBM (portable bitmap)"""
81
+ """Verify if the image is a PBM (portable bitmap). """
80
82
if len (h ) >= 3 and \
81
83
h [0 ] == ord (b'P' ) and h [1 ] in b'14' and h [2 ] in b' \t \n \r ' :
82
84
return 'pbm'
83
85
84
86
tests .append (test_pbm )
85
87
86
88
def test_pgm (h , f ):
87
- """PGM (portable graymap)"""
89
+ """Verify if the image is a PGM (portable graymap). """
88
90
if len (h ) >= 3 and \
89
91
h [0 ] == ord (b'P' ) and h [1 ] in b'25' and h [2 ] in b' \t \n \r ' :
90
92
return 'pgm'
91
93
92
94
tests .append (test_pgm )
93
95
94
96
def test_ppm (h , f ):
95
- """PPM (portable pixmap)"""
97
+ """Verify if the image is a PPM (portable pixmap). """
96
98
if len (h ) >= 3 and \
97
99
h [0 ] == ord (b'P' ) and h [1 ] in b'36' and h [2 ] in b' \t \n \r ' :
98
100
return 'ppm'
99
101
100
102
tests .append (test_ppm )
101
103
102
104
def test_rast (h , f ):
103
- """Sun raster file"""
105
+ """test for the Sun raster file. """
104
106
if h .startswith (b'\x59 \xA6 \x6A \x95 ' ):
105
107
return 'rast'
106
108
107
109
tests .append (test_rast )
108
110
109
111
def test_xbm (h , f ):
110
- """X bitmap (X10 or X11)"""
112
+ """Verify if the image is a X bitmap (X10 or X11). """
111
113
if h .startswith (b'#define ' ):
112
114
return 'xbm'
113
115
114
116
tests .append (test_xbm )
115
117
116
118
def test_bmp (h , f ):
119
+ """Verify if the image is a BMP file."""
117
120
if h .startswith (b'BM' ):
118
121
return 'bmp'
119
122
120
123
tests .append (test_bmp )
121
124
122
125
def test_webp (h , f ):
126
+ """Verify if the image is a WebP."""
123
127
if h .startswith (b'RIFF' ) and h [8 :12 ] == b'WEBP' :
124
128
return 'webp'
125
129
126
130
tests .append (test_webp )
127
131
128
132
def test_exr (h , f ):
133
+ """verify is the image ia a OpenEXR fileOpenEXR."""
129
134
if h .startswith (b'\x76 \x2f \x31 \x01 ' ):
130
135
return 'exr'
131
136
0 commit comments