Created
March 10, 2016 10:09
-
-
Save iSWORD/6eafadebde916585ee7d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# https://youtu.be/RTSGm-a0h1Q | |
from gi.repository import Gtk, Gdk | |
from scipy import misc | |
import serial | |
FILENAME = '/home/isword/Pictures/jb.jpg' | |
DEVICE = '/dev/ttyACM0' | |
BAUDRATE = 115200 | |
def main (): | |
global ser, imageArray, gtkWindow | |
# load image in memory as a 3D array of pixels [y][x][color] | |
imageArray = misc.imread(FILENAME) | |
# initiate the serial connection | |
ser = serial.Serial(DEVICE, BAUDRATE) | |
# setup the GUI | |
gtkWindow = Gtk.Window(Gtk.WindowType.TOPLEVEL) | |
gtkWindow.connect("delete-event", Gtk.main_quit) | |
gtkWindow.set_border_width(30) | |
gtkVBox = Gtk.VBox() | |
gtkWindow.add(gtkVBox) | |
gtkEventBox = Gtk.EventBox() | |
gtkVBox.pack_start(gtkEventBox, fill=True, expand=True, padding=10) | |
gtkLabel = Gtk.Label("Intensity") | |
gtkVBox.pack_start(gtkLabel, fill=True, expand=True, padding=0) | |
gtkHScale = Gtk.HScale() | |
gtkHScale.set_range(0,100) | |
gtkHScale.set_value(100) | |
# gtkHScale.set_sensitive(Gtk.SensitivityType.OFF) | |
gtkHScale.connect("change-value", lambda t,v,d: intensity_change(d)) | |
gtkVBox.pack_start(gtkHScale, fill=True, expand=True, padding=0) | |
gtkEventBox.set_events(Gdk.EventMask.BUTTON_PRESS_MASK) | |
gtkEventBox.connect("button_press_event", | |
lambda e,d: handle_click(d.x, d.y)) | |
gtkEventBox.realize() | |
gtkEventBox.get_window().set_cursor (Gdk.Cursor(Gdk.CursorType.HAND1)) | |
gtkImage = Gtk.Image.new_from_file(FILENAME) | |
gtkEventBox.add(gtkImage) | |
gtkWindow.show_all() | |
Gtk.main() | |
def intensity_change(d): | |
if d > 100: | |
d = 100 | |
d = int(d) | |
command = "intensity %d\r" % d | |
ser.write(command.encode()) | |
def handle_click (x, y): | |
# get color at mouse position | |
color = imageArray[int(y)][int(x)] | |
# modify window background color | |
gdkColor = Gdk.Color(color[0]*256, color[1]*256, color[2]*256) | |
gtkWindow.override_background_color( | |
Gtk.StateFlags.NORMAL, | |
Gdk.RGBA( | |
gdkColor.red_float, | |
gdkColor.green_float, | |
gdkColor.blue_float, | |
alpha=1 | |
) | |
) | |
# send command to microcontroller | |
hex_color = '%02x%02x%02x' % (color[0], color[1], color[2]) | |
command = "rgb %s\r" % hex_color | |
ser.write(command.encode()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment