Tag: Python 3

Generate SHA 256 hashing using Python 3

Hello Friends,

SHA 256 is a Secure Hash Algorithm that generates 256 bit signature of the string. This hashing algorithm generates fixed size of hash for any length of input string. This is a one-way function so the result that is generated from this algorithm cannot be decrypted back.

This algorithm is also used in Blockchain to secure the data.

# Generating SHA hashing using Python 3
# Import sha256 module from hashlib
from hashlib import sha256

# Getting input string from the user
your_data = input('Enter a string: ')

# Generating SHA256 hash
hash_result = sha256(str(your_data).encode())
result = hash_result.hexdigest()

# Printing the result
print('Result: ',str(result))

Output of the above code:

Result of the above code

Thank you

Histogram Equalization in Python

Friends,

Here is the program for histogram equalization of image processing in python 3.

Histogram Equalization is the adjustment of the contrast of the image by modifying the intensity distribution of the histogram.

# Histogram Equalization in python
# By: Ngangbam Indrason (May 2019)

import cv2
import numpy as np
from matplotlib import pyplot as plt

# Reading an image in grayscale
img = cv2.imread("E:\\flower2.jpg", 0)

# Printing the size of the image
print('Image Size: ',img.size)

#Printing the shape of the image
print('Image Shape: ',img.shape)

# Getting the pixel values of the image
pix = np.array(img)
print('Image Pixel values: \n',pix)

# Creating a new matrix for the image
pix2 = np.copy(pix)

# Getting unique pixels and frequency of the values from the image
rk, nk = np.unique(pix, return_counts=True)
print('Unique Pixel values: \n',rk)
print('Frequency of pixel values: \n',nk)

# Image pixels divided by the size of the image
pk = nk/img.size
print('pdf: \n',pk)

pk_length = len(pk)
print('Length of pdf: ',pk_length)

# Getting the cummulative frequency of the unique pixel values
sk = np.cumsum(pk)
print('Cumulative Sum: \n',sk)

# Multiplying the cummulative frequency by the maximum value of the pixels
mul = sk*np.max(pix)
print('Multiplying by Max of Image Pixels: \n',mul)

# Getting rounded value of the multiplied results
roundVal = np.round(mul)
print('Rounded value of multiplied value: \n',roundVal)

# Mapping the pixels for the equalization
for i in range(len(pix)):
    for j in range(len(pix[0])):
        pix2[i][j] = roundVal[np.where(rk == pix[i][j])]

# Pixels of the original image
print('Old Image: \n', pix)   

# Pixels of the new image     
print('New Image: \n', pix2)

# Plotting the histogram of the original image
plt.hist(img.ravel(), 256, [0,256])
plt.show()

# Plotting the histogram of new image
plt.hist(pix2.ravel(), 256, [0,256])
plt.show()

# Displaying the original image
plt.imshow(pix, cmap='gray', interpolation='nearest')
plt.show()

# Displaying the new image
plt.imshow(pix2, cmap='gray', interpolation='nearest')

cv2.waitKey(0)
cv2.destroyAllWindows()
Continue reading “Histogram Equalization in Python”