image_processing.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from PIL import Image
  2. from matplotlib import cm
  3. import numpy as np
  4. import ipfml.metrics as metrics
  5. def fig2data(fig):
  6. """
  7. @brief Convert a Matplotlib figure to a 3D numpy array with RGB channels and return it
  8. @param fig a matplotlib figure
  9. @return a numpy 3D array of RGB values
  10. """
  11. # draw the renderer
  12. fig.canvas.draw()
  13. # Get the RGBA buffer from the figure
  14. w,h = fig.canvas.get_width_height()
  15. buf = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8)
  16. buf.shape = (w, h, 3)
  17. # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode
  18. buf = np.roll(buf, 3, axis=2)
  19. return buf
  20. def fig2img(fig):
  21. """
  22. @brief Convert a Matplotlib figure to a PIL Image in RGB format and return it
  23. @param fig a matplotlib figure
  24. @return a Python Imaging Library (PIL) image : default size (480,640,3)
  25. """
  26. # put the figure pixmap into a numpy array
  27. buf = fig2data(fig)
  28. w, h, d = buf.shape
  29. return Image.frombytes("RGB", (w, h), buf.tostring())
  30. def get_LAB_L_SVD(image):
  31. """
  32. @brief Returns Singular values from LAB L Image information
  33. @param fig a matplotlib figure
  34. @return a Python Imaging Library (PIL) image : default size (480,640,3)
  35. """
  36. L = metrics.get_LAB_L(image)
  37. return metrics.get_SVD(L)
  38. def get_LAB_L_SVD_s(image):
  39. """
  40. @brief Returns s (Singular values) SVD from L of LAB Image information
  41. @param PIL Image
  42. @return vector of singular values
  43. """
  44. L = metrics.get_LAB_L(image)
  45. return metrics.get_SVD_s(L)
  46. def get_LAB_L_SVD_U(image):
  47. """
  48. @brief Returns U SVD from L of LAB Image information
  49. @param PIL Image
  50. @return vector of singular values
  51. """
  52. L = metrics.get_LAB_L(image)
  53. return metrics.get_SVD_U(L)
  54. def get_LAB_L_SVD_V(image):
  55. """
  56. @brief Returns V SVD from L of LAB Image information
  57. @param PIL Image
  58. @return vector of singular values
  59. """
  60. L = metrics.get_LAB_L(image)
  61. return metrics.get_SVD_V(L)
  62. def divide_in_blocks(image, block_size):
  63. '''
  64. @brief Divide image into equal size blocks
  65. @param img - PIL Image
  66. @param block - tuple (width, height) representing the size of each dimension of the block
  67. @return list containing all PIL Image block (in RGB)
  68. Usage :
  69. >>> import numpy as np
  70. >>> from PIL import Image
  71. >>> from ipfml import image_processing
  72. >>> image_values = np.random.randint(255, size=(800, 800, 3))
  73. >>> img = Image.fromarray(image_values.astype('uint8'), 'RGB')
  74. >>> blocks = divide_in_blocks(img, (20, 20))
  75. >>> len(blocks)
  76. 1600
  77. >>> blocks[0].width
  78. 20
  79. >>> blocks[0].height
  80. 20
  81. '''
  82. blocks = []
  83. # check size compatibility
  84. width, height = block_size
  85. if(image.width % width != 0):
  86. raise "Width size issue, block size not compatible"
  87. if(image.height % height != 0):
  88. raise "Height size issue, block size not compatible"
  89. nb_block_width = image.width / width
  90. nb_block_height = image.height / height
  91. image_array = np.array(image)
  92. for i in range(int(nb_block_width)):
  93. begin_x = i * width
  94. for j in range(int(nb_block_height)):
  95. begin_y = j * height
  96. # getting subblock information
  97. current_block = image_array[begin_x:(begin_x + width), begin_y:(begin_y + height)]
  98. blocks.append(Image.fromarray(current_block.astype('uint8'), 'RGB'))
  99. return blocks