-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverageImage.py
More file actions
49 lines (41 loc) · 1.41 KB
/
averageImage.py
File metadata and controls
49 lines (41 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from PIL import Image
from numpy import *
from pylab import *
import os, sys, random
def get_imlist(path):
"""Returns a list of filenames for all jpg images in a directory."""
imlist = []
for filename in os.listdir(path):
if filename.endswith('.jpg') or filename.endswith('.png'):
f = os.path.join(path,filename)
imlist.append(f)
return imlist
def compute_average(imlist, n):
"""compute the average of a list of images"""
# open first image and make into array of type float
try:
imlist = random.sample(imlist, n)
except ValueError:
print 'ERROR: The number of images in the specified folder must be \
greater or equal to the second argument'
sys.exit(1)
averageim = array(Image.open(imlist[0]), 'f')
for imname in imlist[1:]:
try:
averageim += array(Image.open(imname))
except:
print imname + '...skipped'
averageim /= len(imlist)
return array(averageim, 'uint8')
for imfile in os.listdir(sys.argv[1]):
if imfile.endswith('.jpg') or imfile.endswith('.png'):
pil_im = Image.open(os.path.join(sys.argv[1],imfile))
if pil_im.size != (800, 800):
pil_im = pil_im.resize((800,800))
pil_im.save(os.path.join(sys.argv[1],imfile))
imlist = get_imlist(sys.argv[1])
im = compute_average(imlist,int(sys.argv[2]))
imshow(im)
axis('off')
axis('equal')
show()