# Created by Leo from: C:\Python22\Tom\camerasplitter.leo # @path c:\python22\tom # << CameraS declarations >> from wxPython.wx import * import Image import ImageChops import ImageStat import ImageEnhance from VideoCapture import Device import sys, time, math, string hand_tool_mode = 'RGBA' hand_tool_size = (16, 16) hand_tool_data = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\x00\x00\x00\xd4\x00\x00\x00\xbb\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xd4\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' hand_tool_img = Image.fromstring(hand_tool_mode, hand_tool_size, hand_tool_data) zoom_tool_mode = 'RGBA' zoom_tool_size = (16, 16) zoom_tool_data = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' zoom_tool_img = Image.fromstring(zoom_tool_mode, zoom_tool_size, zoom_tool_data) inspect_tool_mode = 'RGBA' inspect_tool_size = (16, 16) inspect_tool_data = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' inspect_tool_img = Image.fromstring(inspect_tool_mode, inspect_tool_size, inspect_tool_data) sharp_tool_mode = 'RGBA' sharp_tool_size = (16, 16) sharp_tool_img = Image.fromstring(inspect_tool_mode, inspect_tool_size, inspect_tool_data) # -- end -- << CameraS declarations >> # <> (1 of 20) def WXToPIL(image, mode = 'RGBA'): "convert a wxImage to a PIL RGBA image" imageData = image.GetData() size = (image.GetWidth(), image.GetHeight()) imagePIL = Image.fromstring('RGB', size, imageData) if mode != 'RGB': imagePIL = imagePIL.convert(mode) return imagePIL # <> (2 of 20) def PILToWX(image): "convert a PIL image to a wxImage" if (image.mode == 'RGBA'): bk = Image.new("RGB", image.size, (255, 250, 255)) image = Image.composite(image, bk, image) if (image.mode != 'RGB'): image = image.convert('RGB') imageData = image.tostring('raw', 'RGB') imageWx = wxEmptyImage(image.size[0], image.size[1]) imageWx.SetData(imageData) imageWx.SetMaskColour(255, 250, 255) return imageWx # <> (3 of 20) def PILToPy(image): ret = "import Image\n" ret += "\n" ret += "mode = %s\n" % repr(image.mode) ret += "size = %s\n" % repr(image.size) ret += "data = %s\n" % repr(image.tostring()) ret += "img = Image.fromstring(mode, size, data)\n" return ret # <> (4 of 20) ## # A standard ImageView tool for panning around the image. class PanTool: # << class PanTool methods >> (1 of 4) def __init__(self, image_view): self.__image_view = image_view img = hand_tool_img img = PILToWX(img) bmp = wxBitmapFromImage(img) id = image_view.get_tool_id(self) self.__id = id self.__dragging = 0 self.__last_pos = (0, 0) image_view.get_toolbar().AddTool(id, bmp, isToggle = 1, shortHelpString = "Pan") # << class PanTool methods >> (2 of 4) ## # Called by the ImageView when the tool is activated. def activate(self, id): win = self.__image_view.get_image_window() cursor = wxStockCursor(wxCURSOR_HAND) win.SetCursor(cursor) win.Connect(-1, -1, wxEVT_LEFT_DOWN, self.on_mouse) win.Connect(-1, -1, wxEVT_LEFT_UP, self.on_mouse) win.Connect(-1, -1, wxEVT_MOTION, self.on_mouse) # << class PanTool methods >> (3 of 4) ## # Called by the ImageView when the tool is deactivated. def deactivate(self, id): win = self.__image_view.get_image_window() win.SetCursor(wxSTANDARD_CURSOR) win.Disconnect(-1, -1, wxEVT_LEFT_DOWN) win.Disconnect(-1, -1, wxEVT_LEFT_UP) win.Disconnect(-1, -1, wxEVT_MOTION) # << class PanTool methods >> (4 of 4) def on_mouse(self, evt): pos = (evt.m_x, evt.m_y) win = self.__image_view.get_image_window() if evt.GetEventType() == wxEVT_MOTION: if not self.__dragging: return diff = (pos[0] - self.__last_pos[0], pos[1] - self.__last_pos[1]) self.__last_pos = pos origin = self.__image_view.get_origin() self.__image_view.set_origin((origin[0] - diff[0], origin[1] - diff[1])) elif evt.GetEventType() == wxEVT_LEFT_DOWN: win.CaptureMouse() self.__last_pos = pos self.__dragging = 1 elif evt.GetEventType() == wxEVT_LEFT_UP: win.ReleaseMouse() self.__dragging = 0 # -- end -- << class PanTool methods >> # <> (5 of 20) ## # A standard ImageView tool for zooming in and out of the image. class ContTool: # << class ContTool methods >> (1 of 6) def __init__(self, image_view): self.__image_view = image_view img = sharp_tool_img img = PILToWX(img) bmp = wxBitmapFromImage(img) id = image_view.get_tool_id(self) self.__id = id image_view.get_toolbar().AddTool(id, bmp, isToggle = 1, shortHelpString = "Contrast") # << class ContTool methods >> (2 of 6) ## # Called by the ImageView when the tool is activated. def activate(self, id): win = self.__image_view.get_image_window() cursor = wxStockCursor(wxCURSOR_PAINT_BRUSH) win.SetCursor(cursor) win.Connect(-1, -1, wxEVT_LEFT_UP, self.on_mouse) win.Connect(-1, -1, wxEVT_RIGHT_UP, self.on_mouse) # << class ContTool methods >> (3 of 6) ## # Called by the ImageView when the tool is deactivated. def deactivate(self, id): win = self.__image_view.get_image_window() win.SetCursor(wxSTANDARD_CURSOR) win.Disconnect(-1, -1, wxEVT_LEFT_UP) win.Disconnect(-1, -1, wxEVT_RIGHT_UP) # << class ContTool methods >> (4 of 6) def on_mouse(self, evt): if evt.GetEventType() == wxEVT_LEFT_UP: if evt.ControlDown() or evt.ShiftDown(): self.cont_out() else: self.cont_in() elif evt.GetEventType() == wxEVT_RIGHT_UP: self.cont_out() # << class ContTool methods >> (5 of 6) def cont_in(self): contrast = self.__image_view.get_contrast() if contrast > 19.9: contrast = 20.0 self.__image_view.set_contrast(contrast) else: contrast = contrast + .1 self.__image_view.set_contrast(contrast) print contrast # << class ContTool methods >> (6 of 6) def cont_out(self): contrast = self.__image_view.get_contrast() if contrast < -19.9: contrast = -20.0 self.__image_view.set_contrast(contrast) else: contrast = contrast - .1 self.__image_view.set_contrast(contrast) print contrast # -- end -- << class ContTool methods >> # <> (6 of 20) ## # A standard ImageView tool for zooming in and out of the image. class RestoreTool: # << class RestoreTool methods >> (1 of 3) def __init__(self, image_view): self.__image_view = image_view img = sharp_tool_img img = PILToWX(img) bmp = wxBitmapFromImage(img) id = image_view.get_tool_id(self) self.__id = id image_view.get_toolbar().AddSeparator() image_view.get_toolbar().AddSimpleTool(id, bmp, 'Restore all Enhancement Tools', "Restore") image_view.get_toolbar().AddSeparator() # << class RestoreTool methods >> (2 of 3) ## # Called by the ImageView when the tool is activated. def activate(self, id): self.__image_view.set_sharp(1.0) self.__image_view.set_bright(1.0) self.__image_view.set_color(1.0) self.__image_view.set_contrast(1.0) self.__image_view.set_zoom(1.0) self.__image_view.set_rgb(0) # << class RestoreTool methods >> (3 of 3) ## # Called by the ImageView when the tool is deactivated. def deactivate(self, id): pass # -- end -- << class RestoreTool methods >> # <> (7 of 20) ## # A standard ImageView tool for zooming in and out of the image. class BrightTool: # << class BrightTool methods >> (1 of 6) def __init__(self, image_view): self.__image_view = image_view img = sharp_tool_img img = PILToWX(img) bmp = wxBitmapFromImage(img) id = image_view.get_tool_id(self) self.__id = id image_view.get_toolbar().AddTool(id, bmp, isToggle = 1, shortHelpString = "Brightness") # << class BrightTool methods >> (2 of 6) ## # Called by the ImageView when the tool is activated. def activate(self, id): win = self.__image_view.get_image_window() cursor = wxStockCursor(wxCURSOR_SPRAYCAN) win.SetCursor(cursor) win.Connect(-1, -1, wxEVT_LEFT_UP, self.on_mouse) win.Connect(-1, -1, wxEVT_RIGHT_UP, self.on_mouse) # << class BrightTool methods >> (3 of 6) ## # Called by the ImageView when the tool is deactivated. def deactivate(self, id): win = self.__image_view.get_image_window() win.SetCursor(wxSTANDARD_CURSOR) win.Disconnect(-1, -1, wxEVT_LEFT_UP) win.Disconnect(-1, -1, wxEVT_RIGHT_UP) # << class BrightTool methods >> (4 of 6) def on_mouse(self, evt): if evt.GetEventType() == wxEVT_LEFT_UP: if evt.ControlDown() or evt.ShiftDown(): self.bright_out() else: self.bright_in() elif evt.GetEventType() == wxEVT_RIGHT_UP: self.bright_out() # << class BrightTool methods >> (5 of 6) def bright_in(self): bright = self.__image_view.get_bright() if bright > 19.9: bright = 20.0 self.__image_view.set_bright(bright) else: bright = bright + .1 self.__image_view.set_bright(bright) print bright # << class BrightTool methods >> (6 of 6) def bright_out(self): bright = self.__image_view.get_bright() if bright < -19.9: bright = -20.0 self.__image_view.set_bright(bright) else: bright = bright - .1 self.__image_view.set_bright(bright) print bright # -- end -- << class BrightTool methods >> # <> (8 of 20) ## # A standard ImageView tool for zooming in and out of the image. class ColorTool: # << class ColorTool methods >> (1 of 6) def __init__(self, image_view): self.__image_view = image_view img = sharp_tool_img img = PILToWX(img) bmp = wxBitmapFromImage(img) id = image_view.get_tool_id(self) self.__id = id image_view.get_toolbar().AddTool(id, bmp, isToggle = 1, shortHelpString = "Color") # << class ColorTool methods >> (2 of 6) ## # Called by the ImageView when the tool is activated. def activate(self, id): win = self.__image_view.get_image_window() cursor = wxStockCursor(wxCURSOR_PENCIL) win.SetCursor(cursor) win.Connect(-1, -1, wxEVT_LEFT_UP, self.on_mouse) win.Connect(-1, -1, wxEVT_RIGHT_UP, self.on_mouse) # << class ColorTool methods >> (3 of 6) ## # Called by the ImageView when the tool is deactivated. def deactivate(self, id): win = self.__image_view.get_image_window() win.SetCursor(wxSTANDARD_CURSOR) win.Disconnect(-1, -1, wxEVT_LEFT_UP) win.Disconnect(-1, -1, wxEVT_RIGHT_UP) # << class ColorTool methods >> (4 of 6) def on_mouse(self, evt): if evt.GetEventType() == wxEVT_LEFT_UP: if evt.ControlDown() or evt.ShiftDown(): self.color_out() else: self.color_in() elif evt.GetEventType() == wxEVT_RIGHT_UP: self.color_out() # << class ColorTool methods >> (5 of 6) def color_in(self): color = self.__image_view.get_color() if color > 19.9: color = 20.0 self.__image_view.set_color(color) else: color = color + .1 self.__image_view.set_color(color) print color # << class ColorTool methods >> (6 of 6) def color_out(self): color = self.__image_view.get_color() if color < -19.9: color = -20.0 self.__image_view.set_color(color) else: color = color - .1 self.__image_view.set_color(color) print color # -- end -- << class ColorTool methods >> # <> (9 of 20) ## # A standard ImageView tool for zooming in and out of the image. class SharpTool: # << class SharpTool methods >> (1 of 6) def __init__(self, image_view): self.__image_view = image_view img = sharp_tool_img img = PILToWX(img) bmp = wxBitmapFromImage(img) id = image_view.get_tool_id(self) self.__id = id image_view.get_toolbar().AddTool(id, bmp, isToggle = 1, shortHelpString = "Sharpness") # << class SharpTool methods >> (2 of 6) ## # Called by the ImageView when the tool is activated. def activate(self, id): win = self.__image_view.get_image_window() cursor = wxStockCursor(wxCURSOR_BULLSEYE) win.SetCursor(cursor) win.Connect(-1, -1, wxEVT_LEFT_UP, self.on_mouse) win.Connect(-1, -1, wxEVT_RIGHT_UP, self.on_mouse) # << class SharpTool methods >> (3 of 6) ## # Called by the ImageView when the tool is deactivated. def deactivate(self, id): win = self.__image_view.get_image_window() win.SetCursor(wxSTANDARD_CURSOR) win.Disconnect(-1, -1, wxEVT_LEFT_UP) win.Disconnect(-1, -1, wxEVT_RIGHT_UP) # << class SharpTool methods >> (4 of 6) def on_mouse(self, evt): if evt.GetEventType() == wxEVT_LEFT_UP: if evt.ControlDown() or evt.ShiftDown(): self.sharp_out() else: self.sharp_in() elif evt.GetEventType() == wxEVT_RIGHT_UP: self.sharp_out() # << class SharpTool methods >> (5 of 6) def sharp_in(self): sharpness = self.__image_view.get_sharp() if sharpness > 19.9: sharpness = 20.0 self.__image_view.set_sharp(sharpness) else: sharpness = sharpness + .1 self.__image_view.set_sharp(sharpness) print sharpness # << class SharpTool methods >> (6 of 6) def sharp_out(self): sharpness = self.__image_view.get_sharp() if sharpness < -19.9: sharpness = -20.0 self.__image_view.set_sharp(sharpness) else: sharpness = sharpness - .1 self.__image_view.set_sharp(sharpness) print sharpness # -- end -- << class SharpTool methods >> # <> (10 of 20) ## # A standard ImageView tool for zooming in and out of the image. class BlueTool: # << class BlueTool methods >> (1 of 3) def __init__(self, image_view): self.__image_view = image_view img = zoom_tool_img img = PILToWX(img) bmp = wxBitmapFromImage(img) id = image_view.get_tool_id(self) self.__id = id image_view.get_toolbar().AddTool(id, bmp, isToggle = 1, shortHelpString = "Blue") # << class BlueTool methods >> (2 of 3) ## # Called by the ImageView when the tool is activated. def activate(self, id): self.__image_view.set_rgb(3) # << class BlueTool methods >> (3 of 3) ## # Called by the ImageView when the tool is deactivated. def deactivate(self, id): self.__image_view.set_rgb(0) # -- end -- << class BlueTool methods >> # <> (11 of 20) ## # A standard ImageView tool for zooming in and out of the image. class GreenTool: # << class GreenTool methods >> (1 of 3) def __init__(self, image_view): self.__image_view = image_view img = zoom_tool_img img = PILToWX(img) bmp = wxBitmapFromImage(img) id = image_view.get_tool_id(self) self.__id = id image_view.get_toolbar().AddTool(id, bmp, isToggle = 1, shortHelpString = "Green") # << class GreenTool methods >> (2 of 3) ## # Called by the ImageView when the tool is activated. def activate(self, id): self.__image_view.set_rgb(2) # << class GreenTool methods >> (3 of 3) ## # Called by the ImageView when the tool is deactivated. def deactivate(self, id): self.__image_view.set_rgb(0) # -- end -- << class GreenTool methods >> # <> (12 of 20) ## # A standard ImageView tool for zooming in and out of the image. class RedTool: # << class RedTool methods >> (1 of 3) def __init__(self, image_view): self.__image_view = image_view img = zoom_tool_img img = PILToWX(img) bmp = wxBitmapFromImage(img) id = image_view.get_tool_id(self) self.__id = id image_view.get_toolbar().AddTool(id, bmp, isToggle = 1, shortHelpString = "Red") # << class RedTool methods >> (2 of 3) ## # Called by the ImageView when the tool is activated. def activate(self, id): self.__image_view.set_rgb(1) # << class RedTool methods >> (3 of 3) ## # Called by the ImageView when the tool is deactivated. def deactivate(self, id): self.__image_view.set_rgb(0) # -- end -- << class RedTool methods >> # <> (13 of 20) ## # A standard ImageView tool for zooming in and out of the image. class ZoomTool: # << class ZoomTool methods >> (1 of 6) def __init__(self, image_view): self.__image_view = image_view img = zoom_tool_img img = PILToWX(img) bmp = wxBitmapFromImage(img) id = image_view.get_tool_id(self) self.__id = id image_view.get_toolbar().AddTool(id, bmp, isToggle = 1, shortHelpString = "Zoom") # << class ZoomTool methods >> (2 of 6) ## # Called by the ImageView when the tool is activated. def activate(self, id): win = self.__image_view.get_image_window() cursor = wxStockCursor(wxCURSOR_MAGNIFIER) win.SetCursor(cursor) win.Connect(-1, -1, wxEVT_LEFT_UP, self.on_mouse) win.Connect(-1, -1, wxEVT_RIGHT_UP, self.on_mouse) # << class ZoomTool methods >> (3 of 6) ## # Called by the ImageView when the tool is deactivated. def deactivate(self, id): win = self.__image_view.get_image_window() win.SetCursor(wxSTANDARD_CURSOR) win.Disconnect(-1, -1, wxEVT_LEFT_UP) win.Disconnect(-1, -1, wxEVT_RIGHT_UP) # << class ZoomTool methods >> (4 of 6) def on_mouse(self, evt): if evt.GetEventType() == wxEVT_LEFT_UP: if evt.ControlDown() or evt.ShiftDown(): self.zoom_out() else: self.zoom_in() elif evt.GetEventType() == wxEVT_RIGHT_UP: self.zoom_out() # << class ZoomTool methods >> (5 of 6) def zoom_in(self): zoom = self.__image_view.get_zoom() if zoom >= 1.0: self.__image_view.set_zoom(zoom + 1.0) else: self.__image_view.set_zoom(1.0 / (1.0 / zoom - 1.0)) # << class ZoomTool methods >> (6 of 6) def zoom_out(self): zoom = self.__image_view.get_zoom() if zoom > 1.0: self.__image_view.set_zoom(zoom - 1.0) else: self.__image_view.set_zoom(1.0 / (1.0 / zoom + 1.0)) # -- end -- << class ZoomTool methods >> # <> (14 of 20) class InspectWindow (wxFrame): # << class InspectWindow methods >> (1 of 3) def __init__(self, image_view): wxFrame.__init__(self, None, -1, "Color inspector", style = wxFRAME_TOOL_WINDOW | wxDEFAULT_FRAME_STYLE) display_size = wxDisplaySize() self.SetBackgroundColour(wxSystemSettings_GetSystemColour(wxSYS_COLOUR_BTNFACE)) self.color_view = wxWindow(self, -1, style = wxSUNKEN_BORDER) self.color_view.SetClientSize((80, 80)) m = (96 - self.color_view.GetSize()[0]) / 2 self.color_view.SetPosition((m, m)) self.color_view.SetBackgroundColour(wxColour(0, 0, 0)) base = (m, m + self.color_view.GetSize()[1] + m) across = self.color_view.GetSize()[0] self.red_text = wxStaticText(self, -1, "-", (base[0], base[1]), size = (across / 3, 24), style = wxALIGN_CENTRE | wxST_NO_AUTORESIZE) self.grn_text = wxStaticText(self, -1, "-", (base[0] + across / 3, base[1]), size = (across / 3, 24), style = wxALIGN_CENTRE | wxST_NO_AUTORESIZE) self.blu_text = wxStaticText(self, -1, "-", (base[0] + 2 * across / 3, base[1]), size = (across / 3, 24), style = wxALIGN_CENTRE | wxST_NO_AUTORESIZE) self.SetClientSize((96, base[1] + 16 + m)) size = self.GetSize() self.SetPosition((display_size[0] - size[0] - 32, display_size[1] - size[1] - 32)) EVT_PAINT(self.color_view, self.on_paint) self.image_view = image_view self.pos = (0, 0) # << class InspectWindow methods >> (2 of 3) def set_position(self, pos): self.pos = (int(pos[0]), int(pos[1])) img = self.image_view.get_image() try: if img == None: raise IndexError rgb = img.getpixel(self.pos) self.red_text.SetLabel(str(rgb[0])) self.grn_text.SetLabel(str(rgb[1])) self.blu_text.SetLabel(str(rgb[2])) except IndexError: self.red_text.SetLabel("-") self.grn_text.SetLabel("-") self.blu_text.SetLabel("-") self.color_view.Refresh(0) # << class InspectWindow methods >> (3 of 3) def on_paint(self, evt): dc = wxPaintDC(self.color_view) img = self.image_view.get_image() if img == None: return for y in range(-2, 3): for x in range(-2, 3): try: rgb = img.getpixel((self.pos[0] + x, self.pos[1] + y)) except IndexError: rgb = (0, 0, 0) if x == 0 and y == 0: dc.SetPen(wxBLACK_PEN) else: dc.SetPen(wxTRANSPARENT_PEN) dc.SetBrush(wxBrush(wxColour(rgb[0], rgb[1], rgb[2]))) dc.DrawRectangle((x + 2) * 16, (y + 2) * 16, 16, 16) # -- end -- << class InspectWindow methods >> # <> (15 of 20) ## # A standard ImageView tool for inspecting colors in the image. class InspectTool: # << class InspectTool methods >> (1 of 4) def __init__(self, image_view): self.__image_view = image_view img = inspect_tool_img img = PILToWX(img) bmp = wxBitmapFromImage(img) id = image_view.get_tool_id(self) image_view.get_toolbar().AddTool(id, bmp, isToggle = 1, shortHelpString = "Inspect") self.__id = id self.__dragging = 0 self.__win = None # << class InspectTool methods >> (2 of 4) ## # Called by the ImageView when the tool is activated. def activate(self, id): win = self.__image_view.get_image_window() cursor = wxStockCursor(wxCURSOR_CROSS) win.SetCursor(cursor) win.Connect(-1, -1, wxEVT_LEFT_DOWN, self.on_mouse) win.Connect(-1, -1, wxEVT_LEFT_UP, self.on_mouse) win.Connect(-1, -1, wxEVT_MOTION, self.on_mouse) # << class InspectTool methods >> (3 of 4) ## # Called by the ImageView when the tool is deactivated. def deactivate(self, id): win = self.__image_view.get_image_window() win.SetCursor(wxSTANDARD_CURSOR) win.Disconnect(-1, -1, wxEVT_LEFT_DOWN) win.Disconnect(-1, -1, wxEVT_LEFT_UP) win.Disconnect(-1, -1, wxEVT_MOTION) # << class InspectTool methods >> (4 of 4) def on_mouse(self, evt): pos = (evt.m_x, evt.m_y) win = self.__image_view.get_image_window() if evt.GetEventType() == wxEVT_MOTION: if not self.__dragging: return if self.__image_view.get_image() != None: self.__win.set_position(self.__image_view.window_to_image(pos)) elif evt.GetEventType() == wxEVT_LEFT_DOWN: win.CaptureMouse() self.__win = InspectWindow(self.__image_view) self.__win.Show() self.__dragging = 1 elif evt.GetEventType() == wxEVT_LEFT_UP: win.ReleaseMouse() if self.__win: self.__win.Close() self.__win = None self.__dragging = 0 # -- end -- << class InspectTool methods >> # <> (16 of 20) class TileCache: # << class TileCache methods >> (1 of 3) def __init__(self, image, megapixel_limit = 1.0): self.image = image self.limit = int(megapixel_limit * 1024 * 1024) self.used = 0 self.cache = [] # << class TileCache methods >> (2 of 3) def get_tile(self, pos, size, zoom): key = (pos, size, zoom) for (k, v) in self.cache: if key == k: self.cache.remove((k, v)) self.cache.append((k, v)) return v self.make_space(size[0] * size[1]) end = [pos[0] + size[0], pos[1] + size[1]] if end[0] > self.image.size[0]: end[0] = self.image.size[0] if end[1] > self.image.size[1]: end[1] = self.image.size[1] img = self.image.crop((pos[0], pos[1], end[0], end[1])) img = img.resize((int(img.size[0] * zoom), int(img.size[1] * zoom))) img = PILToWX(img) img_bmp = wxBitmapFromImage(img) self.used += size[0] * size[1] self.cache.append((key, img_bmp)) return img_bmp # << class TileCache methods >> (3 of 3) def make_space(self, size): while self.used + size > self.limit and self.cache: self.used -= self.cache[0][0][1][0] * self.cache[0][0][1][1] self.cache.remove(self.cache[0]) # -- end -- << class TileCache methods >> # <> (17 of 20) ## # A wxPython window used for viewing an image with pan, zoom and inspect # tools built in. # # Note that if you are writing your own tools to use with the ImageView window, you must provide an # __init__ method which takes a reference to the ImageView window, an activate # method which is called when the tool is activated and is passed the tool button id, and a deactivate # method which is called when the tool is deactivated and is also passed the tool button id. # #

# tools is a list of tools used for constructing the toolbar and image is the initial # image to be viewed. image can be a PIL image or a wxPython Image. class ImageView(wxWindow): # << class ImageView methods >> (1 of 29) def __init__(self, parent, id, tools = [ PanTool, ZoomTool, InspectTool , SharpTool , ColorTool , BrightTool , ContTool , RestoreTool , RedTool , GreenTool , BlueTool], image = None, pos=(-1, -1), size=(-1, -1), style=0, name='ImageView'): wxWindow.__init__(self, parent, id, pos = pos, size = size, style = style, name = name) self.__rgb = 0 self.__image = None self.__cache = None self.__tools = tools self.__tool_id = 20 - 1 self.__tool_ids = {} self.__active_tool = None self.__origin = [0, 0] self.__zoom = 1.0 self.__sharp = 1.0 self.__color = 1.0 self.__bright = 1.0 self.__contrast = 1.0 self.__image_win = wxWindow(self, -1, style = wxSUNKEN_BORDER) self.__toolbar = wxToolBar(self, -1) self.set_image(image) for tool in self.__tools: tool(self) if 20 in self.__tool_ids: self.__set_tool(20) self.__toolbar.Realize() self.__set_constraints() EVT_SIZE(self, self.__on_size) EVT_TOOL(self.__toolbar, -1, self.__on_tool) EVT_PAINT(self.__image_win, self.__on_paint) EVT_SCROLLWIN(self.__image_win, self.__on_scroll) self.__image_win.SetBackgroundColour(wxColour(128, 128, 128)) # << class ImageView methods >> (2 of 29) ## # Sets the image being viewed. def set_image(self, image): #if isinstance(image, wxImage): # image = WXToPIL(image) self.__image = image if self.__rgb > 0: colors = self.__image.split() self.__image = colors[self.__rgb - 1] if self.__sharp <> 1.0: sharper = ImageEnhance.Sharpness(self.__image) self.__image = sharper.enhance(self.__sharp) if self.__color <> 1.0: sharper = ImageEnhance.Color(self.__image) self.__image = sharper.enhance(self.__color) if self.__bright <> 1.0: sharper = ImageEnhance.Brightness(self.__image) self.__image = sharper.enhance(self.__bright) if self.__contrast <> 1.0: sharper = ImageEnhance.Contrast(self.__image) self.__image = sharper.enhance(self.__contrast) self.__cache = TileCache(self.__image) self.__image_win.Refresh(0) # << class ImageView methods >> (3 of 29) ## # Returns the viewed image. def get_image(self): return self.__image # << class ImageView methods >> (4 of 29) ## # Returns the wxPython window displaying the image. This is used by custom tool implementations to # add event handlers or change the cursor when the tool is active. def get_image_window(self): return self.__image_win # << class ImageView methods >> (5 of 29) ## # Returns the toolbar window. This is used by custom tool implementations to add tool buttons to the # toolbar. def get_toolbar(self): return self.__toolbar # << class ImageView methods >> (6 of 29) ## # Returns a unique tool id for use in the toolbar. This is used by custom tool implementations when they # add tools to the toolbar to ensure that their tool ids don't conflict with those used by other tools. # The custom tool is responsible for adding the tool to the toolbar, but it doesn't need to set up any # event handlers because the ImageView window will call the activate and deactivate # methods of the tool automatically when a tool with the id returned is activated. def get_tool_id(self, tool): self.__tool_id += 1 self.__tool_ids[self.__tool_id] = tool return self.__tool_id # << class ImageView methods >> (7 of 29) def __set_tool(self, id): if self.__active_tool: self.__toolbar.ToggleTool(self.__active_tool, 0) if self.__active_tool in self.__tool_ids: self.__tool_ids[self.__active_tool].deactivate(id) self.__toolbar.ToggleTool(id, 1) if id in self.__tool_ids: self.__tool_ids[id].activate(id) self.__active_tool = id # << class ImageView methods >> (8 of 29) ## # Sets the zoom factor of the image being viewed. 1.0 is a one-for-one pixel correspondence between the image and # the screen, 2.0 is zoomed in by a factor of two and 1.0 / 2.0 is zoomed out by a factor of two. def set_zoom(self, zoom): self.__origin = [self.__origin[0] * zoom / self.__zoom, self.__origin[1] * zoom / self.__zoom] self.__zoom = zoom self.__set_scrollbars() self.__get_scrollbars() self.__image_win.Refresh(0) # << class ImageView methods >> (9 of 29) ## # Returns the zoom factor of the image being viewed. 1.0 is a one-for-one pixel correspondence between the image # and the screen, 2.0 is zoomed in by a factor of two and 1.0 / 2.0 is zoomed out by a factor of two. def get_zoom(self): return self.__zoom # << class ImageView methods >> (10 of 29) ## # Returns the zoom factor of the image being viewed. 1.0 is a one-for-one pixel correspondence between the image # and the screen, 2.0 is zoomed in by a factor of two and 1.0 / 2.0 is zoomed out by a factor of two. def get_sharp(self): return self.__sharp # << class ImageView methods >> (11 of 29) ## # Returns the zoom factor of the image being viewed. 1.0 is a one-for-one pixel correspondence between the image # and the screen, 2.0 is zoomed in by a factor of two and 1.0 / 2.0 is zoomed out by a factor of two. def get_color(self): return self.__color # << class ImageView methods >> (12 of 29) ## # Returns the zoom factor of the image being viewed. 1.0 is a one-for-one pixel correspondence between the image # and the screen, 2.0 is zoomed in by a factor of two and 1.0 / 2.0 is zoomed out by a factor of two. def get_bright(self): return self.__bright # << class ImageView methods >> (13 of 29) ## # Returns the zoom factor of the image being viewed. 1.0 is a one-for-one pixel correspondence between the image # and the screen, 2.0 is zoomed in by a factor of two and 1.0 / 2.0 is zoomed out by a factor of two. def get_contrast(self): return self.__contrast # << class ImageView methods >> (14 of 29) ## # Sets the zoom factor of the image being viewed. 1.0 is a one-for-one pixel correspondence between the image and # the screen, 2.0 is zoomed in by a factor of two and 1.0 / 2.0 is zoomed out by a factor of two. def set_sharp(self, sharp): self.__sharp = sharp old_image = self.__image self.set_image(self.__image) self.__image = old_image # << class ImageView methods >> (15 of 29) ## # Sets the zoom factor of the image being viewed. 1.0 is a one-for-one pixel correspondence between the image and # the screen, 2.0 is zoomed in by a factor of two and 1.0 / 2.0 is zoomed out by a factor of two. def set_rgb(self, rgb): self.__rgb = rgb old_image = self.__image self.set_image(self.__image) self.__image = old_image # << class ImageView methods >> (16 of 29) ## # Sets the zoom factor of the image being viewed. 1.0 is a one-for-one pixel correspondence between the image and # the screen, 2.0 is zoomed in by a factor of two and 1.0 / 2.0 is zoomed out by a factor of two. def set_color(self, color): self.__color = color old_image = self.__image self.set_image(self.__image) self.__image = old_image # << class ImageView methods >> (17 of 29) ## # Sets the zoom factor of the image being viewed. 1.0 is a one-for-one pixel correspondence between the image and # the screen, 2.0 is zoomed in by a factor of two and 1.0 / 2.0 is zoomed out by a factor of two. def set_bright(self, bright): self.__bright = bright old_image = self.__image self.set_image(self.__image) self.__image = old_image # << class ImageView methods >> (18 of 29) ## # Sets the zoom factor of the image being viewed. 1.0 is a one-for-one pixel correspondence between the image and # the screen, 2.0 is zoomed in by a factor of two and 1.0 / 2.0 is zoomed out by a factor of two. def set_contrast(self, contrast): self.__contrast = contrast old_image = self.__image self.set_image(self.__image) self.__image = old_image # << class ImageView methods >> (19 of 29) ## # Sets the on-screen origin of the image being viewed. (0, 0) causes the image to be centered in the image view # window. The origin is applied after the zoom factor. def set_origin(self, origin): self.__origin = list(origin) self.__set_scrollbars() self.__get_scrollbars() self.__image_win.Refresh(0) # << class ImageView methods >> (20 of 29) ## # Returns the on-screen origin of the image being viewed. (0, 0) causes the image to be centered in the image view # window. The origin is applied after the zoom factor. def get_origin(self): return [] + self.__origin # << class ImageView methods >> (21 of 29) def __set_constraints(self): l = wxLayoutConstraints() l.left.Absolute(0) l.top.Absolute(0) l.height.AsIs() l.width.SameAs(self, wxWidth) self.__toolbar.SetConstraints(l) l = wxLayoutConstraints() l.left.Absolute(0) l.top.Below(self.__toolbar) l.bottom.SameAs(self, wxHeight) l.width.SameAs(self, wxWidth) self.__image_win.SetConstraints(l) self.Layout() # << class ImageView methods >> (22 of 29) def __on_tool(self, evt): self.__set_tool(evt.GetId()) # << class ImageView methods >> (23 of 29) def __on_scroll(self, evt): if evt.GetOrientation() == wxHORIZONTAL: dim = 0 else: dim = 1 pos = self.__image_win.GetScrollPos(evt.GetOrientation()) self.__image_win.SetScrollPos(evt.GetOrientation(), evt.GetPosition()) if evt.GetEventType() == wxEVT_SCROLLWIN_TOP: self.__image_win.SetScrollPos(evt.GetOrientation(), 0) if evt.GetEventType() == wxEVT_SCROLLWIN_BOTTOM: self.__image_win.SetScrollPos(evt.GetOrientation(), evt.GetScrollRange(evt.GetOrientation())) if evt.GetEventType() == wxEVT_SCROLLWIN_LINEUP: self.__image_win.SetScrollPos(evt.GetOrientation(), pos - 96) if evt.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN: self.__image_win.SetScrollPos(evt.GetOrientation(), pos + 96) if evt.GetEventType() == wxEVT_SCROLLWIN_PAGEUP: self.__image_win.SetScrollPos(evt.GetOrientation(), pos - self.GetClientSize()[1]) if evt.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN: self.__image_win.SetScrollPos(evt.GetOrientation(), pos + self.GetClientSize()[1]) cw = self.__image_win.GetClientSize()[dim] w = self.__image.size[dim] * self.__zoom self.__origin[dim] = self.__image_win.GetScrollPos(evt.GetOrientation()) - 32 - w / 2 + cw / 2 self.__image_win.Refresh(0) # << class ImageView methods >> (24 of 29) ## # Translates a tuple of image coordinates from pixel coordinates in the image window to # pixel coordinates in the image itself. def window_to_image(self, pos): (x, y) = pos (cw, ch) = self.__image_win.GetClientSize() if not self.__image: raise ValueError w = self.__image.size[0] * self.__zoom h = self.__image.size[1] * self.__zoom return (((x - cw / 2) + (w / 2 + self.__origin[0])) / self.__zoom, ((y - ch / 2) + (h / 2 + self.__origin[1])) / self.__zoom) # << class ImageView methods >> (25 of 29) ## # Translates a tuple of image coordinates from pixel coordinates in the image to # pixel coordinates in the image window. def image_to_window(self, pos): (x, y) = pos (cw, ch) = self.__image_win.GetClientSize() if not self.__image: raise ValueError w = self.__image.size[0] * self.__zoom h = self.__image.size[1] * self.__zoom return (x * self.__zoom - (w / 2 + self.__origin[0]) + cw / 2, y * self.__zoom - (h / 2 + self.__origin[1]) + ch / 2) # << class ImageView methods >> (26 of 29) def __on_size(self, evt): self.Layout() self.__set_scrollbars() self.__get_scrollbars() self.__image_win.Refresh(0) # << class ImageView methods >> (27 of 29) def __on_paint(self, evt): dc = wxPaintDC(self.__image_win) if not self.__image: return (cw, ch) = self.__image_win.GetClientSize() bmp = wxEmptyBitmap(cw, ch) mdc = wxMemoryDC() mdc.SelectObject(bmp) mdc.SetBackground(wxBrush(wxColour(128, 128, 128))) mdc.Clear() w = int(self.__image.size[0] * self.__zoom) h = int(self.__image.size[1] * self.__zoom) if w + 64 < cw: self.__origin[0] = 0 if h + 64 < ch: self.__origin[1] = 0 (x, y) = self.image_to_window((0, 0)) mdc.DrawLine(x - 1, y - 1, x + w, y - 1) mdc.DrawLine(x + w, y - 1, x + w, y + h) mdc.DrawLine(x + w, y + h, x - 1, y + h) mdc.DrawLine(x - 1, y + h, x - 1, y - 1) if self.__zoom == 1.0: img_bmp = self.__cache.get_tile((0, 0), self.__image.size, 1.0) mdc.DrawBitmap(img_bmp, x, y) else: ul = self.window_to_image((0, 0)) ul = map(int, map(math.floor, ul)) br = self.window_to_image((cw - 1, ch - 1)) br = map(int, map(math.ceil, br)) if ul[0] < 0: ul[0] = 0 if ul[1] < 0: ul[1] = 0 if br[0] >= self.__image.size[0]: br[0] = self.__image.size[0] if br[1] >= self.__image.size[1]: br[1] = self.__image.size[1] tile_size = int(256 / self.__zoom) if tile_size < 1: tile_size = 1 y = ul[1] / tile_size * tile_size while y < br[1]: x = ul[0] / tile_size * tile_size while x < br[0]: img_bmp = self.__cache.get_tile((x, y), (tile_size, tile_size), self.__zoom) pt = self.image_to_window((x, y)) pt = map(int, pt) mdc.DrawBitmap(img_bmp, pt[0], pt[1]) x += tile_size y += tile_size del mdc dc.DrawBitmap(bmp, 0, 0, 0) self.__set_scrollbars() # << class ImageView methods >> (28 of 29) def __set_scrollbars(self): (cw, ch) = self.__image_win.GetClientSize() if not self.__image: return w = self.__image.size[0] * self.__zoom h = self.__image.size[1] * self.__zoom self.__image_win.SetScrollbar(wxHORIZONTAL, w / 2 - cw / 2 + self.__origin[0] + 32, cw, w + 64) self.__image_win.SetScrollbar(wxVERTICAL, h / 2 - ch / 2 + self.__origin[1] + 32, ch, h + 64) # << class ImageView methods >> (29 of 29) def __get_scrollbars(self): (cw, ch) = self.__image_win.GetClientSize() if not self.__image: return w = self.__image.size[0] * self.__zoom h = self.__image.size[1] * self.__zoom self.__origin[0] = self.__image_win.GetScrollPos(wxHORIZONTAL) - 32 - w / 2 + cw / 2 self.__origin[1] = self.__image_win.GetScrollPos(wxVERTICAL) - 32 - h / 2 + ch / 2 # -- end -- << class ImageView methods >> # <> (18 of 20) class CameraFrame(wxScrolledWindow): # << class CameraFrame methods >> (1 of 5) def __init__(self, parent, number, id = -1, size = wxDefaultSize): self.ID_WINDOW_LEFT1 = 5101 self.ID_WINDOW_LEFT2 = 5102 self.ID_WINDOW_LEFT3 = 5103 self.ID_WINDOW_LEFT4 = 5104 wxScrolledWindow.__init__(self, parent, id, wxPoint(0, 0), size, wxSUNKEN_BORDER) self.cam = Device(devnum=number,showVideoWindow=0) self.Imagethen = self.cam.getImage() self.Imagenow = self.cam.getImage() self.imagedifference = ImageChops.difference(self.Imagethen,self.Imagenow) self.quartercount = 0 self.avarage = 32 EVT_CLOSE(self, self.OnCloseWindow) maintimerID = wxNewId() self.maintimer = wxTimer(self, maintimerID) EVT_TIMER(self, maintimerID, self.OnTimer) self.glob_thing_red = ImageView(self, -1,tools = [ PanTool, ZoomTool, SharpTool , BrightTool , ContTool , RestoreTool , RedTool]) self.glob_thing_blue = ImageView(self, -1,tools = [ PanTool, ZoomTool, SharpTool , BrightTool , ContTool , RestoreTool , BlueTool]) self.glob_thing_green = ImageView(self, -1,tools = [ PanTool, ZoomTool, SharpTool , BrightTool , ContTool , RestoreTool , GreenTool]) self.glob_thing = ImageView(self, -1,tools = [PanTool, ZoomTool, InspectTool, SharpTool, ColorTool, BrightTool, ContTool, RestoreTool]) StartButtonID = wxNewId() self.StartButton = wxButton(self, StartButtonID, "Go", wxPoint(20, 20)) EVT_BUTTON(self, StartButtonID, self.OnGoClick) self.StartButton.SetBackgroundColour(wxBLUE) self.StartButton.SetForegroundColour(wxWHITE) self.StartButton.SetDefault() StopButtonID = wxNewId() self.StopButton = wxButton(self, StopButtonID, "Stop", wxPoint(260, 20)) EVT_BUTTON(self, StopButtonID, self.OnStopClick) self.StopButton.SetBackgroundColour(wxRED) self.StopButton.SetForegroundColour(wxWHITE) self.main_sizer = wxBoxSizer(wxVERTICAL) self.button_sizer = wxBoxSizer(wxHORIZONTAL) self.sash_sizer = wxBoxSizer(wxHORIZONTAL) self.sash_sizer.Add(self.glob_thing_red, 1, wxEXPAND) self.sash_sizer.Add(self.glob_thing_green, 1, wxEXPAND) self.sash_sizer.Add(self.glob_thing_blue, 1, wxEXPAND) self.sash_sizer.Add(self.glob_thing, 2, wxEXPAND) self.button_sizer.Add(self.StartButton, 1, wxEXPAND) self.button_sizer.Add(self.StopButton, 1, wxEXPAND) self.main_sizer.Add(self.button_sizer, 1, wxEXPAND) self.main_sizer.Add(self.sash_sizer, 10, wxEXPAND) self.SetSizer(self.main_sizer) self.SetAutoLayout(1) self.main_sizer.Fit(self) # << class CameraFrame methods >> (2 of 5) def OnTimer(self, event): #self.Imagethen = self.Imagenow self.Imagenow = self.cam.getImage() #self.imagedifference = ImageChops.difference(self.Imagethen,self.Imagenow) #print self.Imagenow.mode, ' ', self.imagedifference.mode #enh = ImageEnhance.Sharpness(self.imagedifference) #img = enh.enhance(self.display1.GetValue()/100) #enh = ImageEnhance.Color(img) #img = enh.enhance(self.display2.GetValue()/100) #self.imagedifference = self.imagedifference.convert('L') self.glob_thing_red.set_image(self.Imagenow) self.glob_thing_blue.set_image(self.Imagenow) self.glob_thing_green.set_image(self.Imagenow) img_red = self.glob_thing_red.get_image().convert('L') ing_green = self.glob_thing_green.get_image().convert('L') ing_blue = self.glob_thing_blue.get_image().convert('L') img = Image.merge('RGB',(img_red ,ing_green,ing_blue)) self.glob_thing.set_image(img) #stats = ImageStat.Stat(self.imagedifference) #num1 = int(stats.sum[0] / 10000) + int(stats.sum[1] / 10000) + int(stats.sum[2] / 10000) #print img.histogram() #if stats.extrema[0][1] > 100: #self.Imagenow.save('C:\\Python22\\tom\\imgtest\\' + str(int(time.time())) + str(self.quartercount) + '.jpg') #self.imagedifference.save('C:\\Python22\\tom\\imgtest\\diff\\dif' + str(self.quartercount) + str(int(time.time())) + '.jpg') #self.avarage = ((self.avarage + num1) / 2) + 1 #self.quartercount = self.quartercount +1 #if self.quartercount == 4: # self.quartercount = 0 #num1 = int(stats.sum[0] / 10000) + int(stats.sum[1] / 10000) + int(stats.sum[2] / 10000) #self.display1.SetValue(int(stats.extrema[0][1])) #self.display2.Replace(0, 25, str(num1) + ' ') # << class CameraFrame methods >> (3 of 5) def OnCloseWindow(self, event): self.Destroy() # << class CameraFrame methods >> (4 of 5) def OnGoClick(self, event): self.maintimer.Start(25) # << class CameraFrame methods >> (5 of 5) def OnStopClick(self, event): self.maintimer.Stop() # -- end -- << class CameraFrame methods >> # <> (19 of 20) class MyFrame(wxFrame): # << class MyFrame methods >> (1 of 6) def __init__(self, parent, id, title): wxFrame.__init__(self, parent, id, title, wxDefaultPosition, wxDefaultSize) EVT_CLOSE(self, self.OnCloseWindow) self.mainmenu = wxMenuBar() menu=wxMenu() exitID=wxNewId() menu.Append(exitID, '&Load', 'Load Data') EVT_MENU(self, exitID, self.OnLoadData) exitID=wxNewId() menu.Append(exitID, '&Save', 'Save Data') EVT_MENU(self, exitID, self.OnSaveData) exitID=wxNewId() menu.Append(exitID, 'Save &As', 'Save Data As') EVT_MENU(self, exitID, self.OnSaveAsData) exitID=wxNewId() menu.Append(exitID, '&Clear', 'Clear Data') EVT_MENU(self, exitID, self.OnClearData) self.mainmenu.Append (menu, '&Data') self.SetMenuBar (self.mainmenu) cameraID = wxNewId() self.camera = CameraFrame(self, 0, id = cameraID, size = wxDefaultSize) # << class MyFrame methods >> (2 of 6) def OnSaveAsData(self, event): pass # << class MyFrame methods >> (3 of 6) def OnClearData(self, event): pass # << class MyFrame methods >> (4 of 6) def OnLoadData(self, event): try: self.items = pickle.load(open('save.p')) except IOError: self.items = {u'turn on tee vee': u'sendKeys("^(%z)")', u'channel forty two': u'sendKeys("42")', u'close program now': u'sendKeys("%( )c")', u'channel thirty six': u'sendKeys("36")', u'play woman rock': u"say('please stand by')\nbrowseTo('http://release.theplatform.com/deliverable.select?pid=Mf5zobHmqTDt6bze2ThXCugPnEUpJQQh&UserID=choiceradio')", u'channel sixteen': u'sendKeys("16")', u'open tee vee guide': u"browseTo('http://tv.yahoo.com/grid?lineup=us_AZ02423&dur=&.intl=us&startdate=0&starthour=0&genres=0')", u'channel thirty four': u'sendKeys("34")', u'channel forty four': u'sendKeys("44")', u'play raggae': u"say('please stand by')\nbrowseTo('http://www.totallyradio.com/asx/live/reggae.asx')", u'computer': u"say('working')", u'channel forty': u'sendKeys("40")', u'channel thirty eight': u'sendKeys("38")', u'stop music': u'sendKeys("^(s)")', u'play mellow yellow': u"say('please stand by')\nbrowseTo('http://radio.player.loudeye.com/LoudSE/radio.jsp?anID=22&aID=1874&dsoID=4')", u'channel seven': u'sendKeys("7")', u'channel two': u'sendKeys("2")', u'play new age': u"say('please stand by')\nbrowseTo('http://release.theplatform.com/deliverable.select?pid=tXSTT6E-SWIv_678ayYHCdIp51IFHhLb&UserID=choiceradio')"} # << class MyFrame methods >> (5 of 6) def OnSaveData(self, event): pickle.dump(self.items, open('save.p', 'w')) # << class MyFrame methods >> (6 of 6) def OnCloseWindow(self, event): self.Destroy() # -- end -- << class MyFrame methods >> # <> (20 of 20) class CameraSplitter(wxApp): # << class CameraSplitter methods >> def OnInit(self): # Create an instance of our customized Frame class self.frame = MyFrame(None, -1, "CameraSplitter") self.frame.Show(true) # Tell wxWindows that this is our main window self.SetTopWindow(self.frame) # Return a success flag return true # -- end -- << class CameraSplitter methods >> # -- end -- <> app = CameraSplitter(0) # Create an instance of the application class app.MainLoop() # Tell it to start processing events