Source

adlerviz.2012 / enhance.py

Full commit
import glob, os, sys, os.path
import matplotlib.pylab as pl
import numpy as na
from yt.visualization.image_writer import write_bitmap

def enhance(input_file, output_file, stdval=6.0, just_alpha=True):
    if just_alpha:
        im = pl.imread(input_file)[:,:,:3]
        nim = na.zeros_like(im)
        nz = im[im>0.0]
        nim = im/(nz.mean()+stdval*na.std(nz))
        nim[nim>1.0]=1.0
        nim[nim<0.0]=0.0
        write_bitmap(nim, output_file)
        del im, nim, nz
    else:
        im = pl.imread(input_file)[:,:,:3]
        nim = na.zeros_like(im)
        for c in range(3):
            nz = im[:,:,c][im[:,:,c]>0.0]
            nim[:,:,c] = im[:,:,c]/(nz.mean()+stdval*na.std(nz))
            del nz
        nim[:,:][nim>1.0]=1.0
        nim[:,:][nim<0.0]=0.0
        write_bitmap(nim, output_file)
        del im, nim

input_files = sys.argv[1:-1]
output_dir = sys.argv[-1]

if not os.path.exists(output_dir):
    os.mkdir(output_dir)

files = input_files
for my_file in files:
    new_file = output_dir+my_file
    if not os.path.exists(new_file):
        print "%s -> %s" % (my_file, new_file)
        enhance(my_file, new_file)

# USAGE IS python enhance.py *.png enhanced/