Changeset 792 for trunk/UTIL/PYTHON/mymath.py
- Timestamp:
- Sep 21, 2012, 3:03:27 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UTIL/PYTHON/mymath.py
r647 r792 229 229 return idx 230 230 231 # Author: A.C. 231 232 def fig2data ( fig ): 232 233 import numpy … … 248 249 return buf 249 250 251 # Author: A.C. 250 252 def fig2img ( fig ): 251 253 import Image … … 260 262 w, h, d = buf.shape 261 263 return Image.fromstring( "RGBA", ( w ,h ), buf.tostring( ) ) 264 265 # Author: A.C. 266 # Convert a single layer image object (greyscale) to an array 267 def image2array(im): 268 import numpy as np 269 if im.mode not in ("L", "F"): 270 raise ValueError, ("can only convert single-layer images", im.mode) 271 if im.mode == "L": 272 a = np.fromstring(im.tostring(), np.uint8) 273 else: 274 a = np.fromstring(im.tostring(), np.float32) 275 a.shape = im.size[1], im.size[0] 276 return a 277 278 # Author: A.C. 279 # Convert a 2D array to a single layer image object (greyscale) 280 def array2image(a): 281 import numpy as np 282 import Image 283 if a.dtype == np.uint8: 284 mode = "L" 285 elif a.dtype == np.float32: 286 mode = "F" 287 else: 288 raise ValueError, "unsupported image mode" 289 return Image.fromstring(mode, (a.shape[1], a.shape[0]), a.tostring()) 290
Note: See TracChangeset
for help on using the changeset viewer.