|
| 1 | +from math import sqrt |
| 2 | +from animations import FadeAnimation |
| 3 | +import requests |
| 4 | +from PIL import Image |
| 5 | +from StringIO import StringIO |
| 6 | +from time import sleep |
| 7 | +from collections import namedtuple |
| 8 | +Color = namedtuple("Color", ["r","g", "b"]) |
| 9 | +# author: Harsh Singh |
| 10 | +light_count = 48 |
| 11 | +rows = 8 |
| 12 | +cols = 6 |
| 13 | +out = FadeAnimation() |
| 14 | +out.FADEINRATE = 2 # optional |
| 15 | +out.FADEOUTRATE = 8 # optional, makes things 'trail off' |
| 16 | +out.start() |
| 17 | + |
| 18 | +pix = [(1023.0, 0.0, 0.0)] * light_count |
| 19 | + |
| 20 | + |
| 21 | +class ImagePoint(object): |
| 22 | + |
| 23 | + """docstring for Point""" |
| 24 | + |
| 25 | + def __init__(t, size, rows, cols, x=0, y=0): |
| 26 | + super(ImagePoint, t).__init__() |
| 27 | + t.x = x |
| 28 | + t.y = y |
| 29 | + t.rows = rows |
| 30 | + t.cols = cols |
| 31 | + t.size_x, t.size_y = size |
| 32 | + t.done = False |
| 33 | + t.direction = "right" |
| 34 | + |
| 35 | + def move_down(t): |
| 36 | + print "called down" |
| 37 | + t.direction = "down" |
| 38 | + t.down_count = 0 |
| 39 | + t.move() |
| 40 | + |
| 41 | + def move(t): |
| 42 | + if t.direction is "right": |
| 43 | + if t.x == t.size_x - t.cols - 1: |
| 44 | + t.next_direction = "left" |
| 45 | + t.move_down() |
| 46 | + else: |
| 47 | + t.x += 1 |
| 48 | + elif t.direction is "left": |
| 49 | + if t.x - 1 < 0: |
| 50 | + t.next_direction = "right" |
| 51 | + t.move_down() |
| 52 | + else: |
| 53 | + t.x -= 1 |
| 54 | + elif t.direction is "down": |
| 55 | + if t.y is t.size_y: |
| 56 | + if t.last_row: |
| 57 | + t.done = True |
| 58 | + t.last_row = True |
| 59 | + t.direction = t.next_direction |
| 60 | + elif t.down_count + 1 is t.rows: |
| 61 | + # change rows to 2 condition for less jumpy |
| 62 | + t.direction = t.next_direction |
| 63 | + else: |
| 64 | + t.down_count += 1 |
| 65 | + t.y += 1 |
| 66 | + |
| 67 | + def similar_frame(t, img_data): |
| 68 | + # http://stackoverflow.com/questions/8863810/python-find-similar-colors-best-way |
| 69 | + a = Color(*img_data[t.x, t.y]) |
| 70 | + b = Color(*img_data[t.x + t.cols-1, t.y]) |
| 71 | + rm = 0.5*(a.r+b.r) |
| 72 | + return abs(((2+rm) * (a.r-b.r)**2) + 4*(a.g-b.g)**2 + ((3-rm) * (a.b-b.b)**2))**.5 < 10 |
| 73 | + |
| 74 | + def write_frame(t, out, img_data): |
| 75 | + """ |
| 76 | + Given a frame larger than grid size, select only values starting from top left to fit grid |
| 77 | + """ |
| 78 | + result = [rgb_to_lights(img_data[t.x + i, t.y + j]) for i in xrange(t.cols) for j in xrange(t.rows)] |
| 79 | + out.write(result) |
| 80 | + |
| 81 | + |
| 82 | +def longest_side(): |
| 83 | + return max(rows, cols) |
| 84 | + |
| 85 | + |
| 86 | +def get_image(): |
| 87 | + url = "https://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=88a7df8d4b7696c5de97f5fc97c75c91&format=json&nojsoncallback=1" |
| 88 | + recent_image = requests.get(url).json()["photos"]["photo"][0] |
| 89 | + picture_url = "http://farm{farm_id}.staticflickr.com/{server_id}/{id}_{secret}.jpg".format( |
| 90 | + farm_id=recent_image["farm"], server_id=recent_image["server"], id=recent_image["id"], secret=recent_image["secret"]) |
| 91 | + img = Image.open(StringIO(requests.get(picture_url).content)) |
| 92 | + return img |
| 93 | + |
| 94 | +def rgb_to_lights(rgb): |
| 95 | + return [i * 4.0 for i in rgb] |
| 96 | + |
| 97 | +def animation_picture_slider(): |
| 98 | + img = get_image() |
| 99 | + size = img.size |
| 100 | + img_data = img.load() |
| 101 | + speed = .15 |
| 102 | + point = ImagePoint(size, rows, cols) |
| 103 | + while (not point.done): |
| 104 | + point.write_frame(out, img_data) |
| 105 | + point.move() |
| 106 | + if point.similar_frame(img_data): |
| 107 | + if speed > .01: |
| 108 | + speed *= .9 |
| 109 | + elif speed < .16: |
| 110 | + speed *=1.04 |
| 111 | + sleep(speed) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + while True: |
| 116 | + animation_picture_slider() |
0 commit comments