Add generate_histogram.py

This commit is contained in:
Benjamin Loison 2024-04-25 12:37:21 +02:00
parent a78dcd6f99
commit ec3ddd9c94
Signed by: Benjamin_Loison
SSH Key Fingerprint: SHA256:BtnEgYTlHdOg1u+RmYcDE0mnfz1rhv5dSbQ2gyxW8B8

View File

@ -0,0 +1,44 @@
import os
from PIL import Image
import matplotlib.pyplot as plot
os.chdir('/home/benjamin/Desktop/bens_folder/school/ens/asp/aria/internship/work/datasets/raise/flat-field/TIF')
image = Image.open('flat_001.tif')
image.histogram()
HEX_COLOR = '#%02x%02x%02x'
def getRed(redIntensity):
return HEX_COLOR % (redIntensity, 0, 0)
def getGreen(greenIntensity):
return HEX_COLOR % (0, greenIntensity, 0)
def getBlue(blueIntensity):
return HEX_COLOR % (0, 0, blueIntensity)
def getColor(intensity, colorIndex):
return HEX_COLOR % tuple((intensity if colorIndex == colorIndexTmp else 0) for colorIndexTmp in range(3))
histogram = image.histogram()
COLOR_BASE = 256
red = histogram[0:COLOR_BASE]
green = histogram[COLOR_BASE:COLOR_BASE*2]
blue = histogram[COLOR_BASE*2:COLOR_BASE*3]
#for colorIndex, color in [red, green, blue]
plt.figure(0)
for i in range(0, COLOR_BASE):
plt.bar(i, red[i], color = getColor(i, 0),alpha=0.3)
plt.figure(1)
for i in range(0, COLOR_BASE):
plt.bar(i, green[i], color = getColor(i, 1),alpha=0.3)
plt.figure(2)
for i in range(0, COLOR_BASE):
plt.bar(i, blue[i], color = getColor(i, 2),alpha=0.3)
plt.show()