-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmposplit.py
executable file
·49 lines (39 loc) · 1.3 KB
/
mposplit.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
#!/usr/bin/env python3
import argparse
from pathlib import Path
from PIL import Image
parser = argparse.ArgumentParser(
description='Split MPO files foo.jpg into foo-0.jpg, foo-1.jpg, ...')
parser.add_argument(
'file',
help='MPO file(s) to be split',
nargs='+')
args = parser.parse_args()
for file in args.file:
print('== Processing {} =='.format(file))
path = Path(file).resolve()
try:
image = Image.open(path)
except IOError as e:
print('Could not read image: {}'.format(e))
continue
if (image.format != 'MPO'):
print('Format ' + image.format +
' not recognized as MPO, this script may not work')
try:
position = 0
while True:
image.seek(position)
filename = '{}-{}{}'.format(path.stem, position, path.suffix)
fullname = path.parent.joinpath(filename)
if fullname.exists():
print('{} already exists - skipping.'.format(filename))
else:
print('Extracting image to {}'.format(filename))
try:
image.save(fullname)
except IOError as e:
print(' Could not save: {}'.format(e))
position = image.tell() + 1
except EOFError:
pass # All images processed