try:
                    colors[i,j] = np.median(photons)
                except:
                    colors[i,j] = 10000
                    
        print "..." 
    print "Finished constructing image"
    if n==1:
        totalimage = image
        if color == True:
            totalcolors = colors
    else:
        totalimage += image
        if color == True:
            totalcolors = (n*totalcolors + colors)/(n+1)
    h5.__del__()


print "subtracting off median counts of ", np.median(totalimage[totalimage>0])

totalimage - np.median(totalimage[totalimage>0])
#vmin = np.mean(totalimage)-1.0*np.std(totalimage)
vmin = 0
vmax = np.mean(totalimage)+2.0*np.std(totalimage)

im = Image.new("RGB", (nxpix,nypix))
draw = ImageDraw.ImageDraw(im)

if color == True:
    totalimage[totalimage<vmin] = vmin
    totalimage[totalimage>vmax] = vmax