-
Notifications
You must be signed in to change notification settings - Fork 0
/
3d360_sliver.py
94 lines (88 loc) · 2.91 KB
/
3d360_sliver.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
from sys import argv
from PIL import Image
import tifffile
import rawpy
import imageio
from shutil import copyfile
def sliver(pixCount, rgb, destPath):
w = rgb.shape[1]
rgbSliver = rgb[:,w / 2 - pixCount / 2:w / 2 + pixCount / 2,:]
imageio.imsave(destPath, rgbSliver)
def main(pixCount, srcDir, destDir):
srcNames = os.listdir(srcDir)
nr = 0
for srcName in srcNames:
nr += 1
countLog = str(nr) + '/' + str(len(srcNames)) + ' >'
srcPath = os.path.join(srcDir, srcName)
destName = srcName.split('.')[0] + '_slice' + str(pixCount) + '.tif'
destPath = os.path.join(destDir, destName)
if os.path.isfile(destPath):
#print(countLog, destName, 'already processed')
continue
srcExt = srcName.split('.')[-1]
if srcExt in ['png', 'jpg']:
img = Image.open(srcPath)
rgb = numpy.asarray(img)
sliver(pixCount, rgb, destPath)
print(countLog, srcName, 'slivered')
elif srcExt in ['tif', 'tiff']:
rgb = tifffile.imread(srcPath)
sliver(pixCount, rgb, destPath)
print(countLog, srcName, 'slivered')
elif srcExt in ['nef', 'cr2', 'dng']:
try:
raw = rawpy.imread(srcPath)
except:
raw = None
if raw:
rawpyPars = rawpy.Params(output_bps=16)
rgb = raw.postprocess(params=rawpyPars)
sliver(pixCount, rgb, destPath)
print(countLog, srcName, 'slivered')
else:
corruptDir = os.path.join(srcDir, '../corrupt')
if not os.path.isdir(corruptDir):
os.makedirs(corruptDir)
corruptPath = os.path.join(corruptDir, srcName)
if not os.path.exists(corruptPath):
copyfile(srcPath, corruptPath)
print(countLog, srcName, 'rawpy import faild, copied to "corrupt" dir')
else:
convertedName = srcName.split('.')[0] + '.dng'
convertedPath = os.path.join(corruptDir, convertedName)
if not os.path.exists(convertedPath):
print(countLog, srcName, 'is corrupt, convert to .dng, leave in "corrupt" dir')
else:
try:
raw = rawpy.imread(convertedPath)
except:
raw = None
if raw:
print(countLog, convertedName)
sliver(pixCount, rawConverted, destPath)
else:
print(countLog, convertedName, 'found converted .dng, rawpy import faild, you`re screwed')
print('finished!')
if __name__ == "__main__":
if len(argv) != 4:
print('3 arguments excepted:')
print('[pixCount] [srcDir] [destDir]')
else:
pixCount = argv[1]
if not pixCount.isdigit():
print('first argument, pixCount, needs to be an integer')
else:
pixCount = int(pixCount)
if pixCount < 2 or pixCount % 2:
print('first argument, pixCount, needs to be an even number and above 1')
else:
srcDir = argv[2]
if not os.path.isdir(srcDir):
print('second argument, srcDir, needs to be a valid dir')
else:
destDir = argv[3]
if not os.path.isdir(destDir):
os.makedirs(destDir)
main(pixCount, srcDir, destDir)