Created
June 11, 2013 18:12
-
-
Save jsundram/5759297 to your computer and use it in GitHub Desktop.
Takes a folder full of images and computes their average and median. Assumes they all have the same dimensions. Makes no attempt at alignment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from glob import glob | |
import numpy as np | |
import os | |
import sys | |
# You'll need scikit-image and PIL installed to use this | |
try: | |
import skimage.io | |
except ImportError: | |
sys.exit("please 'pip install scikit-image' to use this script") | |
def median(pattern, outfile): | |
c = skimage.io.ImageCollection(pattern) | |
all_images = c.concatenate() | |
median = np.median(all_images, axis=0) | |
m = np.array(median, dtype=np.uint8) | |
skimage.io.imsave(outfile, m) | |
print outfile | |
def average(images, outfile): | |
average = np.array(skimage.io.imread(images[0]), dtype=np.float32) | |
n = 1 | |
for i, img in enumerate(images[1:]): | |
try: | |
im = skimage.io.imread(img) | |
average += im | |
n += 1 | |
except KeyboardInterrupt: | |
print "quitting" | |
break | |
except Exception, e: | |
print img, e | |
average /= float(n) | |
a = np.array(average, dtype=np.uint8) | |
skimage.io.imsave(outfile, a) | |
print outfile | |
def main(): | |
folder = sys.argv[1] | |
pattern = os.path.join(folder, "*.jpg") | |
average(glob(pattern), os.path.join(folder, "average.jpg")) | |
median(pattern, os.path.join(folder, "median.jpg")) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm in need of your help, I'd like to use your code in TCC, but I can not get it to work on ubuntu 16.04. Would you help me find out why your code does not work on ubuntu?