Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 129 lines (114 sloc) 4.991 kB
42cfa42 @suapapa add -I don't remeber- snippets
authored
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # raw2HDR.py: a simple scpit for make High Dynamic Ranage Image from
5 # single raw file using ufraw-batch and enfuse.
6 # Copyright (c) 2008 Homin Lee <ff4500@gmail.com>
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 import os, sys
21
22 def _err(errStr):
23 sys.stderr.write('ERR!! : '+errStr+'\n')
24 sys.exit(1)
25
26 def _parseEVRange(minEV, maxEV, step):
27 start = minEV
28 end = maxEV + step
29 if step < 0.1:
30 _err('step must bigger then 0.1')
31 if minEV >= maxEV:
32 _err('maxEV must bigger then minEV')
33 L = []
34 while 1:
35 next = start+len(L) * step
36 if step > 0 and next > maxEV:
37 break
38 L.append('%0.1f'%next)
39 return L
40
41 def _makeTempTiffNames(evList, inputName):
42 dirName = os.path.dirname(inputName)
43 baseName = os.path.basename(inputName)
44 fileName, _, _ = baseName.rpartition('.')
45 L =[]
46 for evStr in evList:
47 evNum = eval(evStr)
48 if evNum < 0:
49 evStr = evStr.replace('-','N')
50 elif evNum > 0:
51 evStr = 'P'+evStr
52 evStr = evStr.replace('.','')
53 newBaseName = fileName+'_'+evStr+'.tiff'
54 L.append(os.path.join(dirName, newBaseName))
55 return L
56
57 def _execCmd(cmd):
58 print '>>',cmd
59 ret = os.system(cmd)
60 if not ret==0:
61 _err('something wrong while running cmd :\n%s'%cmd)
62
63 def main():
64 from optparse import OptionParser as OptPsr
65 usage = "usage: %prog [opitons] input.raw "
66 optPsr = OptPsr(usage)
67 optPsr.add_option('-o', '--output', type='string', help='output image name')
68 optPsr.add_option('-m', '--minEV', type='float', help='min EV value; default=-3', default='-3')
69 optPsr.add_option('-M', '--maxEV', type='float', help='max EV value; default=3', default='3')
70 optPsr.add_option('-s', '--step', type='float', help='EV step; default=1', default='1')
71 optPsr.add_option('-t', '--deleteTemps', action='store_true', help='delete temp tiffs', default=False)
72 optPsr.add_option('', '--ufraw', type='string', help='location of ufraw-batch; default=ufraw-batch', default='ufraw-batch')
73 optPsr.add_option('', '--enfuse', type='string', help='location of enfuse; default=enfuse', default='enfuse')
74 optPsr.add_option('', '--exiftool', type='string', help='location of exiftool; default=exiftool', default='exiftool')
75 (opts, args) = optPsr.parse_args()
76 if not args:
77 _err('must specify input raw image file')
78 evRange = _parseEVRange(opts.minEV, opts.maxEV, opts.step)
79 inputRaws = args
80 for inputRaw in inputRaws:
81 ## make multi exposed image from one raw input.
82 tempTiffs = _makeTempTiffNames(evRange, inputRaw)
83 for i in range(len(evRange)):
84 tempTiff = tempTiffs[i]
85 if not os.path.exists(tempTiff):
86 #cmd = '%s --create-id=no --out-type=tiff8 --exposure=%s --output=%s %s'%\
87 cmd = '%s --create-id=no --out-type=tiff --exposure=%s --out-depth=8 --shrink=1 --output=%s %s'%\
88 (opts.ufraw, evRange[i], tempTiff, inputRaw)
89 _execCmd(cmd)
90
91 ## enfuse it!
92 if not opts.output:
93 dirName = os.path.dirname(inputRaw)
94 baseName = os.path.basename(inputRaw)
95 fileName, _, _ = baseName.rpartition('.')
96 output = os.path.join(dirName, fileName+'_enfused.tiff')
97 else:
98 output = opts.output
99 cmd = '%s -o %s %s '%\
100 (opts.enfuse, output, ' '.join(tempTiffs))
101 _execCmd(cmd)
102
103 ## insert exif from input raw to enfused image
104 if inputRaw.endswith('.ufraw'):
105 import re
106 ptrn = re.compile('<InputFilename>(.*)</InputFilename>')
107 for line in open(inputRaw):
108 rawSource = ptrn.findall(line)
109 if rawSource:
110 inputRaw = rawSource[0]
111 break
112
113 #cmd = '%s -tagsfromfile %s -exif:all %s'%\
114 cmd = '%s -tagsfromfile "%s" -exif:all "%s"'%\
115 (opts.exiftool, inputRaw, output)
116 _execCmd(cmd)
117 os.remove(output+'_original')
118
119 ## delete temp multi exposed tiffs
120 if opts.deleteTemps:
121 for tempTiff in tempTiffs:
122 print 'delete %s'%tempTiff
123 os.remove(tempTiff)
124
125
126 if __name__ == '__main__':
127 main()
128
Something went wrong with that request. Please try again.