'''MakeHDRFinal.py ''' FOLDER = "Reduced-Still-Life" HDRWIN = None # Window for the HDR array TOTALWEIGHT = None # Window for the weights array TONEMAPPED = None # Window for the preliminary tone-mapped image OUTPUTWIN = None # Window for the enhanced tone-mapped image CONST = 200 # Used in the mappings to and from HDR values # Map pixels from one input image into HDR values and weights: def process(folder, filename, shutter_speed): global HDRWIN, TOTALWEIGHT, FOLDER, CONST print "Preparing to read in the image file: "+filename srcwin = pmOpenImage(0, folder+"/"+filename) width = pmGetImageWidth(srcwin) height = pmGetImageHeight(srcwin) if not HDRWIN: HDRWIN = pmNewImage(0, "HDR", width, height, 0, 0, 0) pmSetComplex(HDRWIN) # For extra pixel precision TOTALWEIGHT = pmNewImage(0,"Total Weight",width,height,0,0,0) pmSetComplex(TOTALWEIGHT) # Again, extra precision pmSetSource1(srcwin) pmSetDestination(TOTALWEIGHT) f="complex(dest(x,y)+200*\ pow(2.718,-pow((S1(x,y)-127.5)/65,2)),0)" pmSetFormula(f) pmCompute() pmSetSource2(TOTALWEIGHT) pmSetDestination(HDRWIN) exposureFac = 1.0/shutter_speed f="complex(dest(x,y)+REAL2(x,y)*pow(S1(x,y), 2.2)*"+\ str(exposureFac)+" * "+str(CONST)+",0.0)" pmSetFormula(f) pmCompute() # To get correct weighted average HDR values, we # divide HDR values by corresponding total weights. def normalize_by_weights(): global HDRWIN, TOTALWEIGHT pmSetSource1(TOTALWEIGHT) pmSetDestination(HDRWIN) pmSetFormula("complex(dest(x,y)/real1(x,y), 0.0)") pmCompute() # The tone_map routine has the job of mapping back the values # from the HDR image to a normal image. The contrast and # brightness can be adjusted by changing the arguments. def tone_map(factor, power): global HDRWIN, TONEMAPPED width = pmGetImageWidth(HDRWIN) height = pmGetImageHeight(HDRWIN) if not TONEMAPPED: TONEMAPPED = pmNewImage(0,"Tone-mapped (preliminary)",width,height,0,128,0) # Initialize to green to show that the output is coming. pmSetSource1(HDRWIN) pmSetDestination(TONEMAPPED) f=str(factor)+"*pow(S1(x,y)/"+str(CONST)+", "+str(power)+")" pmSetFormula(f) pmCompute() # Increase color saturation by factor, clipping at 1.0. def saturate(factor): global TONEMAPPED, OUTPUTWIN if not OUTPUTWIN: OUTPUTWIN = pmCloneImage(TONEMAPPED) pmSetTitle(OUTPUTWIN, "Saturation-boosted, tone-mapped result") pmSetSource1(TONEMAPPED) pmSetDestination(OUTPUTWIN) f="HSV(hue1(x,y),min(1,sat1(x,y)*"+str(factor)+"),val1(x,y))" pmSetFormula(f) pmCompute() def main(): print "Starting to read in images" process(FOLDER, "REDUCED-still-life-1-1-60th.JPG", 1.0/30) # actual shutter speed = 1/60 process(FOLDER, "REDUCED-still-life-6-1-half.JPG", 1.0/2) process(FOLDER, "REDUCED-still-life-11-15-secs.JPG", 8.0) # actual shutter speed = 15 sec. normalize_by_weights() tone_map(5, 1.0/4) saturate(2.0) main()