forked from tmhlsky/Instagram-API-python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtils.py
More file actions
Latest commit
32 lines (31 loc) · 1.22 KB
/
Copy pathImageUtils.py
File metadata and controls
32 lines (31 loc) · 1.22 KB
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
importstruct
importimghdr
defgetImageSize(fname):
withopen(fname, 'rb') asfhandle:
head=fhandle.read(24)
iflen(head) !=24:
raiseRuntimeError("Invalid Header")
ifimghdr.what(fname) =='png':
check=struct.unpack('>i', head[4:8])[0]
ifcheck!=0x0d0a1a0a:
raiseRuntimeError("PNG: Invalid check")
width, height=struct.unpack('>ii', head[16:24])
elifimghdr.what(fname) =='gif':
width, height=struct.unpack('<HH', head[6:10])
elifimghdr.what(fname) =='jpeg':
fhandle.seek(0) # Read 0xff next
size=2
ftype=0
whilenot0xc0<=ftype<=0xcf:
fhandle.seek(size, 1)
byte=fhandle.read(1)
whileord(byte) ==0xff:
byte=fhandle.read(1)
ftype=ord(byte)
size=struct.unpack('>H', fhandle.read(2))[0] -2
# We are at a SOFn block
fhandle.seek(1, 1) # Skip `precision' byte.
height, width=struct.unpack('>HH', fhandle.read(4))
else:
raiseRuntimeError("Unsupported format")
returnwidth, height