-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_checker.py
62 lines (48 loc) · 1.41 KB
/
image_checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import struct
class NotImageFileException(Exception):
pass
class ImageFileTypeChecker(object):
@classmethod
def check(cls, img_file):
img_file.seek(0)
return img_file.read(4) == cls.HEAD
@classmethod
def get_size(cls, img_file):
raise NotImplementedError()
@classmethod
def get_ext(cls):
return cls.EXT
class JPGChecker(ImageFileTypeChecker):
HEAD = '\xff\xd8\xff\xe1'
EXT = 'jpg'
@staticmethod
def get_size(jpg_file):
jpg_file.seek(0)
size = 2
ftype = 0
while not 0xc0 <= ftype <= 0xcf:
jpg_file.seek(size, 1)
byte = jpg_file.read(1)
while ord(byte) == 0xff:
byte = jpg_file.read(1)
ftype = ord(byte)
size = struct.unpack('>H', jpg_file.read(2))[0] - 2
jpg_file.seek(1, 1)
height, width = struct.unpack('>HH', jpg_file.read(4))
return height, width
class PNGChecker(ImageFileTypeChecker):
HEAD = '\x89PNG'
EXT = 'png'
@staticmethod
def get_size(png_file):
png_file.seek(16)
width, height = struct.unpack('>ii', png_file.read(8))
return height, width
def get_checker(image_file):
for checker in (PNGChecker, JPGChecker):
if checker.check(image_file):
return checker
else:
raise NotImageFileException