It is commented as it does not seem to be finally what we are looking for.
41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
import os
|
|
from PIL import Image
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
from tqdm import tqdm
|
|
|
|
os.chdir('flat-field/TIF')
|
|
|
|
NUMBER_OF_COLORS = 3
|
|
HEX_COLOR = '#' + '%02x' * NUMBER_OF_COLORS
|
|
COLOR_BASE = 256
|
|
|
|
def getColor(colorIntensity, colorIndex):
|
|
return HEX_COLOR % tuple((colorIntensity if colorIndex == colorIndexTmp else 0) for colorIndexTmp in range(NUMBER_OF_COLORS))
|
|
|
|
def getHistogram(fileName):
|
|
image = Image.open(fileName)
|
|
#image = image.crop((1, 1, 2, 2))
|
|
#print(image.size)
|
|
|
|
histogram = image.histogram()
|
|
|
|
colors = [histogram[COLOR_BASE * colorIndex:COLOR_BASE * (colorIndex + 1)] for colorIndex in range(NUMBER_OF_COLORS)]
|
|
return colors
|
|
|
|
def plotHistogram(colors):
|
|
for colorIndex, color in enumerate(colors):
|
|
for colorIntensity in range(COLOR_BASE):
|
|
plt.bar(colorIntensity, color[colorIntensity], color = getColor(colorIntensity, colorIndex), alpha = 0.3)
|
|
|
|
fileNameColors = []
|
|
|
|
for fileName in tqdm(os.listdir()):
|
|
colors = getHistogram(fileName)
|
|
fileNameColors += [colors]
|
|
|
|
meanFileNameColors = np.mean(fileNameColors, axis = 0)
|
|
|
|
plotHistogram(meanFileNameColors)
|
|
|
|
plt.show() |