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

https://github.com/will-moore/openmicroscopy
Python | 76 lines | 38 code | 13 blank | 25 comment | 6 complexity | 55c03f35f86f91487d789544b8449d90 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
  38. # only)
  39. # =================================================================
  40. theZ = image.getSizeZ() / 2
  41. theT = 0
  42. cIndex = 0
  43. for minMax in channelMinMax:
  44. plane = pixels.getPlane(theZ, cIndex, theT)
  45. print "dtype:", plane.dtype.name
  46. # need plane dtype to be uint8 (or int8) for conversion to tiff by PIL
  47. if plane.dtype.name not in ('uint8', 'int8'): # we need to scale...
  48. minVal, maxVal = minMax
  49. valRange = maxVal - minVal
  50. scaled = (plane - minVal) * (float(255) / valRange)
  51. convArray = zeros(plane.shape, dtype=uint8)
  52. convArray += scaled
  53. print ("using converted int8 plane: dtype: %s min: %s max: %s"
  54. % (convArray.dtype.name, convArray.min(), convArray.max()))
  55. i = Image.fromarray(convArray)
  56. else:
  57. i = Image.fromarray(plane)
  58. i.save("tiffPlaneInt8%s.tiff" % cIndex)
  59. cIndex += 1
  60. # Close connection:
  61. # =================================================================
  62. # When you are done, close the session to free up server resources.
  63. conn._closeSession()