-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathreadjpg.py
More file actions
36 lines (31 loc) · 903 Bytes
/
readjpg.py
File metadata and controls
36 lines (31 loc) · 903 Bytes
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
"""
readjpg.py
Chapter 4 Files
Author: William C. Gunnells
Rapid Python Programming
"""
import sys
import binascii
import struct
signature = [binascii.unhexlify(b'FFD8FFD8'),
binascii.unhexlify(b'FFD8FFE0'),
binascii.unhexlify(b'FFD8FFE1')
]
with open(sys.argv[1], 'rb') as file:
first_four_bytes = file.read(4)
if first_four_bytes in signature:
print("JPEG found.")
else:
print("This is not a JPEG file.")
print(first_four_bytes)
print(binascii.hexlify(first_four_bytes))
second = file.read(1)
while ord(second) != 0xDA:
second = file.read(1)
while ord(second) != 0xFF:
second = file.read(1)
while ord(second) == 0xFF:
second = file.read(1)
if ord(second) >= ord(second) <= 0xC3:
print(binascii.hexlify(file.read(3)))
print(struct.unpack('>HH', file.read(4)))