Skip to content

Commit 7cabf8d

Browse files
authored
Merge pull request adafruit#1 from caternuson/master
added amg88xx_still.py to examples
2 parents 6299a94 + 415008c commit 7cabf8d

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

examples/amg88xx_still.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/python
2+
# Copyright (c) 2017 Adafruit Industries
3+
# Author: Carter Nelson
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
23+
import argparse
24+
from time import sleep
25+
from colour import Color
26+
27+
from Adafruit_AMG88xx import Adafruit_AMG88xx
28+
from PIL import Image, ImageDraw
29+
30+
# parse command line arguments
31+
parser = argparse.ArgumentParser(description='Take a still image.')
32+
parser.add_argument('-o','--output', metavar='filename', default='amg88xx_still.jpg', help='specify output filename')
33+
parser.add_argument('-s','--scale', type=int, default=2, help='specify image scale')
34+
parser.add_argument('--min', type=float, help='specify minimum temperature')
35+
parser.add_argument('--max', type=float, help='specify maximum temperature')
36+
parser.add_argument('--report', action='store_true', default=False, help='show sensor information without saving image')
37+
38+
args = parser.parse_args()
39+
40+
# sensor setup
41+
NX = 8
42+
NY = 8
43+
sensor = Adafruit_AMG88xx()
44+
45+
# wait for it to boot
46+
sleep(.1)
47+
48+
# get sensor readings
49+
pixels = sensor.readPixels()
50+
51+
if args.report:
52+
print "Min Pixel = {0} C".format(min(pixels))
53+
print "Max Pixel = {0} C".format(max(pixels))
54+
print "Thermistor = {0} C".format(sensor.readThermistor())
55+
exit()
56+
57+
# output image buffer
58+
image = Image.new("RGB", (NX, NY), "white")
59+
draw = ImageDraw.Draw(image)
60+
61+
# color map
62+
COLORDEPTH = 256
63+
colors = list(Color("indigo").range_to(Color("red"), COLORDEPTH))
64+
colors = [(int(c.red * 255), int(c.green * 255), int(c.blue * 255)) for c in colors]
65+
66+
#some utility functions
67+
def constrain(val, min_val, max_val):
68+
return min(max_val, max(min_val, val))
69+
70+
def map(x, in_min, in_max, out_min, out_max):
71+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
72+
73+
# map sensor readings to color map
74+
MINTEMP = min(pixels) if args.min == None else args.min
75+
MAXTEMP = max(pixels) if args.max == None else args.max
76+
pixels = [map(p, MINTEMP, MAXTEMP, 0, COLORDEPTH - 1) for p in pixels]
77+
78+
# create the image
79+
for ix in xrange(NX):
80+
for iy in xrange(NY):
81+
draw.point([(ix,iy%NX)], fill=colors[constrain(int(pixels[ix+NX*iy]), 0, COLORDEPTH- 1)])
82+
83+
# scale and save
84+
image.resize((NX*args.scale, NY*args.scale), Image.BICUBIC).save(args.output)

0 commit comments

Comments
 (0)