Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
Utils.py
Go to the documentation of this file.
1 ###############################################################################
2 #
3 # Module: Laelaps.Utils
4 #
5 # Package: RoadNarrows Laelaps Robotic Mobile Platform Package
6 #
7 # Link: https://github.com/roadnarrows-robotics/laelaps
8 #
9 # File: Utils.py
10 #
11 ## \file
12 ##
13 ## $LastChangedDate: 2016-03-17 12:14:21 -0600 (Thu, 17 Mar 2016) $
14 ## $Rev: 4350 $
15 ##
16 ## \brief Utilities.
17 ##
18 ## \author Robin Knight (robin.knight@roadnarrows.com)
19 ##
20 ## \par Copyright
21 ## \h_copy 2015-2017. RoadNarrows LLC.\n
22 ## http://www.roadnarrows.com\n
23 ## All Rights Reserved
24 ##
25 # @EulaBegin@
26 #
27 # Permission is hereby granted, without written agreement and without
28 # license or royalty fees, to use, copy, modify, and distribute this
29 # software and its documentation for any purpose, provided that
30 # (1) The above copyright notice and the following two paragraphs
31 # appear in all copies of the source code and (2) redistributions
32 # including binaries reproduces these notices in the supporting
33 # documentation. Substantial modifications to this software may be
34 # copyrighted by their authors and need not follow the licensing terms
35 # described here, provided that the new terms are clearly indicated in
36 # all files where they apply.
37 #
38 # IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
39 # OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
40 # PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
41 # DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
42 # EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
43 # THE POSSIBILITY OF SUCH DAMAGE.
44 #
45 # THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
46 # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
47 # FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
48 # "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
49 # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
50 #
51 # @EulaEnd@
52 #
53 ###############################################################################
54 
55 import sys
56 import os
57 import time
58 import math
59 
60 from pkg_resources import *
61 
62 from Tkinter import *
63 from Tkconstants import *
64 from tkFileDialog import *
65 import tkFont
66 
67 from PIL import Image, ImageTk
68 
69 # ------------------------------------------------------------------------------
70 # Class ImageLoader
71 # ------------------------------------------------------------------------------
72 
73 #
74 ## \brief Class to handle image loading.
75 #
77  #
78  ## \brief Constructor
79  ##
80  ## \param py_pkg Python resource (e.g. "laelaps_control.images").
81  ## \param image_paths List of directory paths to search for the image.
82  #
83  def __init__(self, py_pkg=None, image_paths=[]):
84  self.m_pyPkg = py_pkg
85  if len(image_paths) > 0:
86  self.m_imagePaths = image_paths
87  else:
88  self.m_imagePaths = ['.']
89 
90  #
91  ## \brief Open image from file and convert to PhotoImage.
92  ##
93  ## \param filename Image file name.
94  ##
95  ## \return Returns image widget on success, None on failure.
96  #
97  def loadImage(self, filename):
98  img = self.openImage(filename)
99  if img is not None:
100  return ImageTk.PhotoImage(img)
101  else:
102  return None
103 
104  """
105  # no file name
106  if filename is None or len(filename) == 0:
107  return None;
108  # absolute file name
109  if filename[0] == os.path.sep:
110  try:
111  return ImageTk.PhotoImage(Image.open(filename))
112  except IOError:
113  return None
114  # relative file name - try python resource(s) first
115  if self.m_pyPkg:
116  try:
117  fqname = resource_filename(self.m_pyPkg, filename)
118  try:
119  return ImageTk.PhotoImage(Image.open(fqname))
120  except IOError:
121  pass
122  except ImportError:
123  pass
124  # relative file name - search path for file
125  for path in self.m_imagePaths:
126  fqname = path + os.path.sep + filename
127  try:
128  return ImageTk.PhotoImage(Image.open(fqname))
129  except IOError:
130  continue
131  return None
132  """
133 
134  #
135  ## \brief Open image from file.
136  ##
137  ## \param filename Image file name.
138  ##
139  ## \return Returns image widget on success, None on failure.
140  #
141  def openImage(self, filename):
142  # no file name
143  if filename is None or len(filename) == 0:
144  return None;
145  # relative file name - try python resource(s) first
146  if self.m_pyPkg:
147  try:
148  fqname = resource_filename(self.m_pyPkg, filename)
149  try:
150  return Image.open(fqname)
151  except IOError:
152  pass
153  except ImportError:
154  pass
155  # relative file name - search path for file
156  for path in self.m_imagePaths:
157  fqname = path + os.path.sep + filename
158  try:
159  return Image.open(fqname)
160  except IOError:
161  continue
162  return None
163 
164 
165 
166 # ------------------------------------------------------------------------------
167 # Misc. Utilities
168 # ------------------------------------------------------------------------------
169 
170 #
171 #
172 ## Round to nearest 100th.
173 #
174 def round100th(x):
175  return math.floor((x + 0.005) * 100.0) / 100.0
176 
177 #
178 ## Round to nearest 10th.
179 #
180 def round10th(x):
181  return math.floor((x + 0.05) * 10.0) / 10.0
182 
183 #
184 ## Degrees to radians.
185 #
186 def degToRad(deg):
187  return deg / 180.0 * math.pi
188 
189 #
190 ## Radians to degrees.
191 #
192 def radToDeg(rad):
193  return rad / math.pi * 180.0
194 
195 #
196 ## Connected Laelaps
197 #
198 def whichROSMaster():
199  rosmaster = os.getenv('ROS_MASTER_URI')
200  if len(rosmaster) == 0:
201  return 'localhost'
202  parts = rosmaster.split(':')
203  if len(parts) < 2:
204  return 'localhost'
205  host = parts[1]
206  return host.strip('/')
def openImage(self, filename)
Open image from file.
Definition: Utils.py:141
def __init__(self, py_pkg=None, image_paths=[])
Constructor.
Definition: Utils.py:83
Class to handle image loading.
Definition: Utils.py:76
def loadImage(self, filename)
Open image from file and convert to PhotoImage.
Definition: Utils.py:97