compression.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. Functions for image compression and extraction
  3. """
  4. # image processing imports
  5. from numpy.linalg import svd
  6. def get_SVD(image):
  7. """Transforms Image using SVD compression
  8. Args:
  9. image: image to convert into SVD compression
  10. Return:
  11. U, s, V obtained from SVD compression
  12. Usage:
  13. >>> from PIL import Image
  14. >>> from ipfml.processing import compression
  15. >>> img = Image.open('./images/test_img.png')
  16. >>> U, s, V = compression.get_SVD(img)
  17. >>> U.shape
  18. (200, 200, 3)
  19. >>> len(s)
  20. 200
  21. >>> V.shape
  22. (200, 3, 3)
  23. """
  24. return svd(image, full_matrices=False)
  25. def get_SVD_s(image):
  26. """Transforms Image into SVD and returns only 's' part
  27. Args:
  28. image: image to convert
  29. Returns:
  30. vector of singular values obtained from SVD compression
  31. Usage:
  32. >>> from PIL import Image
  33. >>> from ipfml.processing import compression
  34. >>> img = Image.open('./images/test_img.png')
  35. >>> s = compression.get_SVD_s(img)
  36. >>> len(s)
  37. 200
  38. """
  39. U, s, V = svd(image, full_matrices=False)
  40. return s
  41. def get_SVD_U(image):
  42. """Transforms Image into SVD and returns only 'U' part
  43. Args:
  44. image: image to convert
  45. Returns:
  46. U matrix from SVD compression
  47. Usage:
  48. >>> from PIL import Image
  49. >>> from ipfml.processing import compression
  50. >>> img = Image.open('./images/test_img.png')
  51. >>> U = compression.get_SVD_U(img)
  52. >>> U.shape
  53. (200, 200, 3)
  54. """
  55. U, s, V = svd(image, full_matrices=False)
  56. return U
  57. def get_SVD_V(image):
  58. """Transforms Image into SVD and returns only 'V' part
  59. Args:
  60. image: image to convert
  61. Returns:
  62. V matrix obtained from SVD compression
  63. Usage :
  64. >>> from PIL import Image
  65. >>> from ipfml.processing import compression
  66. >>> img = Image.open('./images/test_img.png')
  67. >>> V = compression.get_SVD_V(img)
  68. >>> V.shape
  69. (200, 3, 3)
  70. """
  71. U, s, V = svd(image, full_matrices=False)
  72. return V