def recontract_img_from_dwt_coef(coeff_dwt): (coeffs_r, coeffs_g, coeffs_b) = coeff_dwt reRed = pywt.idwt2(coeffs_r, 'haar') reGreen = pywt.idwt2(coeffs_g, 'haar') reBlue = pywt.idwt2(coeffs_b, 'haar') (width, height) = reRed.shape dwt_img = Image.new('RGB', (width, height), (0, 0, 20)) reMaxRed = util.max_ndarray(reRed) reMaxGreen = util.max_ndarray(reGreen) reMaxBlue = util.max_ndarray(reBlue) for i in range(width): for j in range(height): R = reRed[i][j] # R = (R/reMaxRed)*160.0 G = reGreen[i][j] # G = (G/reMaxGreen)*85.0 B = reBlue[i][j] # B = (B/reMaxBlue)*100.0 new_value = (int(R), int(G), int(B)) dwt_img.putpixel((i, j), new_value) dwt_img.save('/home/jiewend/download/a_re.png') return dwt_img
def test_max_ndarray(self): 'it should return an Integer, max if matrix is valid, 0 otherwise' # case 1 (it should return 6): tmp_array = np.array([[1, 2, 3], [4, 5, 6]], np.int32) max_value = max_ndarray(tmp_array) self.assertEqual(type(max_value).__name__, 'int32') self.assertEqual(max_value, 6) # case 2 (it should return 0): max_none = max_ndarray(None) self.assertEqual(type(max_none).__name__, 'int') self.assertEqual(max_none, 0)
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 cARed = numpy.array(coeffs_r[0]) (width, height) = coeffs_r.shape 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 * 2, height * 2), (0, 0, 20)) #cA reconstruction for i in range(width): for j in range(height): R = cARed[i][j] R = (R / cAMaxRed) * 160.0 G = cAGreen[i][j] G = (G / cAMaxGreen) * 85.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) #cH reconstruction for i in range(width): for j in range(height): R = cHRed[i][j] R = (R / cHMaxRed) * 160.0 G = cHGreen[i][j] G = (G / cHMaxGreen) * 85.0 B = cHBlue[i][j] B = (B / cHMaxBlue) * 100.0 new_value = (int(R), int(G), int(B)) dwt_img.putpixel((i + width, j), new_value) #cV reconstruction for i in range(width): for j in range(height): R = cVRed[i][j] R = (R / cVMaxRed) * 160.0 G = cVGreen[i][j] G = (G / cVMaxGreen) * 85.0 B = cVBlue[i][j] B = (B / cVMaxBlue) * 100.0 new_value = (int(R), int(G), int(B)) dwt_img.putpixel((i, j + height), new_value) #cD reconstruction for i in range(width): for j in range(height): R = cDRed[i][j] R = (R / cDMaxRed) * 160.0 G = cDGreen[i][j] G = (G / cDMaxGreen) * 85.0 B = cDBlue[i][j] B = (B / cDMaxBlue) * 100.0 new_value = (int(R), int(G), int(B)) dwt_img.putpixel((i + width, j + height), new_value) return dwt_img
def img_from_dwt_coeff(coeff_dwt): ''' This function will recreate an Image object, based on the generated from dwt coefficients ''' #Channel Red (coeffs_r, coeffs_g, coeffs_b) = coeff_dwt cARed = numpy.array(coeffs_r[0]) (width, height) = coeffs_r.shape 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 * 2, height * 2), (0, 0, 20)) #cA reconstruction for i in range(width): for j in range(height): R = cARed[i][j] R = (R / cAMaxRed) * 160.0 G = cAGreen[i][j] G = (G / cAMaxGreen) * 85.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) #cH reconstruction for i in range(width): for j in range(height): R = cHRed[i][j] R = (R / cHMaxRed) * 160.0 G = cHGreen[i][j] G = (G / cHMaxGreen) * 85.0 B = cHBlue[i][j] B = (B / cHMaxBlue) * 100.0 new_value = (int(R), int(G), int(B)) dwt_img.putpixel((i + width, j), new_value) #cV reconstruction for i in range(width): for j in range(height): R = cVRed[i][j] R = (R / cVMaxRed) * 160.0 G = cVGreen[i][j] G = (G / cVMaxGreen) * 85.0 B = cVBlue[i][j] B = (B / cVMaxBlue) * 100.0 new_value = (int(R), int(G), int(B)) dwt_img.putpixel((i, j + height), new_value) #cD reconstruction for i in range(width): for j in range(height): R = cDRed[i][j] R = (R / cDMaxRed) * 160.0 G = cDGreen[i][j] G = (G / cDMaxGreen) * 85.0 B = cDBlue[i][j] B = (B / cDMaxBlue) * 100.0 new_value = (int(R), int(G), int(B)) dwt_img.putpixel((i + width, j + height), new_value) return dwt_img