/examples/Training/python/Advanced/Raw_Data_advanced.py

https://github.com/cneves/openmicroscopy
Python | 74 lines | 37 code | 13 blank | 24 comment | 6 complexity | f9aebaf24e0ffd09d09611e7e96bf743 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2011 University of Dundee & Open Microscopy Environment.
  5. # All Rights Reserved.
  6. # Use is subject to license terms supplied in LICENSE.txt
  7. #
  8. """
  9. FOR TRAINING PURPOSES ONLY!
  10. """
  11. from numpy import zeros, uint8
  12. from omero.gateway import BlitzGateway
  13. from Connect_To_OMERO import USERNAME, PASSWORD, HOST, PORT
  14. try:
  15. from PIL import Image
  16. except ImportError:
  17. import Image
  18. # Create a connection
  19. # =================================================================
  20. conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT)
  21. conn.connect()
  22. # Configuration
  23. # =================================================================
  24. imageId = 401
  25. # Save a plane (raw data) as tiff for analysis
  26. # =================================================================
  27. image = conn.getObject("Image", imageId) # first plane of the image
  28. pixels = image.getPrimaryPixels()
  29. # make a note of min max pixel values for each channel
  30. # so that we can scale all the planes from each channel to the same range
  31. channelMinMax = []
  32. for c in image.getChannels():
  33. minC = c.getWindowMin()
  34. maxC = c.getWindowMax()
  35. channelMinMax.append((minC, maxC))
  36. print channelMinMax
  37. # Go through each channel (looping through Z and T not shown - go for mid-Z only)
  38. # =================================================================
  39. theZ = image.getSizeZ() / 2
  40. theT = 0
  41. cIndex = 0
  42. for minMax in channelMinMax:
  43. plane = pixels.getPlane(theZ, cIndex, theT)
  44. print "dtype:", plane.dtype.name
  45. # need plane dtype to be uint8 (or int8) for conversion to tiff by PIL
  46. if plane.dtype.name not in ('uint8', 'int8'): # we need to scale...
  47. minVal, maxVal = minMax
  48. valRange = maxVal - minVal
  49. scaled = (plane - minVal) * (float(255) / valRange)
  50. convArray = zeros(plane.shape, dtype=uint8)
  51. convArray += scaled
  52. print "using converted int8 plane: dtype: %s min: %s max: %s" % (convArray.dtype.name, convArray.min(), convArray.max())
  53. i = Image.fromarray(convArray)
  54. else:
  55. i = Image.fromarray(plane)
  56. i.save("tiffPlaneInt8%s.tiff" % cIndex)
  57. cIndex += 1
  58. # Close connection:
  59. # =================================================================
  60. # When you're done, close the session to free up server resources.
  61. conn._closeSession()