-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_coeff.py
111 lines (91 loc) · 3.05 KB
/
get_coeff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# pylint: disable=C0103,E0401,W0631
'Compression methods'
import pywt
import numpy
from PIL import Image
import utils as util
def extract_rgb_coeff(img):
"""
Returns RGB dwt applied coefficients tuple
Parameters
----------
img: PIL Image
Returns
-------
(coeffs_r, coeffs_g, coeffs_b):
RGB coefficients with Discrete Wavelet Transform Applied
"""
(width, height) = img.size
img = img.copy()
mat_r = numpy.empty((width, height))
mat_g = numpy.empty((width, height))
mat_b = numpy.empty((width, height))
for i in range(width):
for j in range(height):
(r, g, b) = img.getpixel((i, j))
mat_r[i, j] = r
mat_g[i, j] = g
mat_b[i, j] = b
coeffs_r = pywt.dwt2(mat_r, 'haar')
coeffs_g = pywt.dwt2(mat_g, 'haar')
coeffs_b = pywt.dwt2(mat_b, 'haar')
return (coeffs_r, coeffs_g, coeffs_b)
def img_from_dwt_coeff(coeff_dwt):
"""
Returns Image recreated from dwt coefficients
Parameters
----------
(coeffs_r, coeffs_g, coeffs_b):
RGB coefficients with Discrete Wavelet Transform Applied
Returns
-------
Image from dwt coefficients
"""
# Channel Red
(coeffs_r, coeffs_g, coeffs_b) = coeff_dwt
cc = numpy.array((coeffs_r, coeffs_g, coeffs_b))
(width, height) = (len(coeffs_r[0]), len(coeffs_r[0][0]))
cARed = numpy.array(coeffs_r[0])
cHRed = numpy.array(coeffs_r[1][0])
cVRed = numpy.array(coeffs_r[1][1])
cDRed = numpy.array(coeffs_r[1][2])
# Channel Green
cAGreen = numpy.array(coeffs_g[0])
cHGreen = numpy.array(coeffs_g[1][0])
cVGreen = numpy.array(coeffs_g[1][1])
cDGreen = numpy.array(coeffs_g[1][2])
# Channel Blue
cABlue = numpy.array(coeffs_b[0])
cHBlue = numpy.array(coeffs_b[1][0])
cVBlue = numpy.array(coeffs_b[1][1])
cDBlue = numpy.array(coeffs_b[1][2])
# maxValue per channel par matrix
cAMaxRed = util.max_ndarray(cARed)
cAMaxGreen = util.max_ndarray(cAGreen)
cAMaxBlue = util.max_ndarray(cABlue)
cHMaxRed = util.max_ndarray(cHRed)
cHMaxGreen = util.max_ndarray(cHGreen)
cHMaxBlue = util.max_ndarray(cHBlue)
cVMaxRed = util.max_ndarray(cVRed)
cVMaxGreen = util.max_ndarray(cVGreen)
cVMaxBlue = util.max_ndarray(cVBlue)
cDMaxRed = util.max_ndarray(cDRed)
cDMaxGreen = util.max_ndarray(cDGreen)
cDMaxBlue = util.max_ndarray(cDBlue)
# Image object init
dwt_img = Image.new('RGB', (width, height), (0, 0, 20))
# cA reconstruction
'''
The image formed from the low frequnecy of the images which contains the main content of the image
'''
for i in range(width):
for j in range(height):
R = cARed[i][j]
R = (R/cAMaxRed)*100.0
G = cAGreen[i][j]
G = (G/cAMaxGreen)*100.0
B = cABlue[i][j]
B = (B/cAMaxBlue)*100.0
new_value = (int(R), int(G), int(B))
dwt_img.putpixel((i, j), new_value) # creating image back from the obtained color values
return dwt_img