-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageSampler.py
73 lines (58 loc) · 2.03 KB
/
ImageSampler.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def main():
# this program is coded in Python v. 2.7
from __future__ import print_function
from PIL import Image
import argparse
from time import sleep
print('ImageSampler')
# command-line argument parser
parser = argparse.ArgumentParser(
prog = 'ImageSampler',
prefix_chars = '-/',
description = """This program assigns settings to a list of bulbs (list is currently coded into the .py file).""")
parser.add_argument('-f', '--filename', help='Filename for temporary image storage', type=str, default='image.png')
parser.add_argument('-d', '--delay', help='Delay before sampling (seconds)', type=float, default=0)
args = parser.parse_args()
sleep(args.delay)
from cv2 import *
# initialize the camera
cam = VideoCapture(0) # 0 -> index of camera
s, img = cam.read()
if s: # frame captured without any errors
namedWindow("cam-test",CV_WINDOW_AUTOSIZE)
imshow("cam-test",img)
waitKey(0)
destroyWindow("cam-test")
imwrite("filename.jpg",img) #save image
from SimpleCV import Image, Camera
cam = Camera()
img = cam.getImage()
img.save("filename.jpg")
# debug
# import pdb
# pdb.set_trace()
# debug
from PIL import Image
def average_image_color(filename):
i = Image.open(filename)
h = i.histogram()
# split into red, green, blue
r = h[0:256]
g = h[256:256*2]
b = h[256*2: 256*3]
# perform the weighted average of each channel:
# the *index* is the channel value, and the *value* is its weight
return (
sum( i*w for i, w in enumerate(r) ) / sum(r),
sum( i*w for i, w in enumerate(g) ) / sum(g),
sum( i*w for i, w in enumerate(b) ) / sum(b)
)
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
print average_image_color(sys.argv[1])
else:
print 'usage: average_image_color.py FILENAME'
print 'prints the average color of the image as (R,G,B) where R,G,B are between 0 and 255.'
if __name__ == '__main__':
main()