﻿ python – PIL：创建图像颜色亮度的一维直方图？ - 代码日志

#### python – PIL：创建图像颜色亮度的一维直方图？

>使图像灰度(或双色调,我将同时使用两者来查看哪一个更好).
>处理每个单独的列并为每列创建净强度值.
>将结果吐出到有序列表中.

``````from PIL import Image

image_file = 'test.tiff'

image = Image.open(image_file).convert('L')

histo = image.histogram()
histo_string = ''

for i in histo:
histo_string += str(i) + "\n"

print(histo_string)
``````

``````from PIL import Image
import numpy

def smoothListGaussian(list,degree=5):
window=degree*2-1
weight=numpy.array([1.0]*window)
weightGauss=[]

for i in range(window):
i=i-degree+1
frac=i/float(window)
gauss=1/(numpy.exp((4*(frac))**2))
weightGauss.append(gauss)

weight=numpy.array(weightGauss)*weight
smoothed=[0.0]*(len(list)-window)

for i in range(len(smoothed)):
smoothed[i]=sum(numpy.array(list[i:i+window])*weight)/sum(weight)

return smoothed

image_file = 'verypurple.jpg'
out_file = 'out.tiff'

image = Image.open(image_file).convert('1')
image.save(out_file)

intensities = []

for x in xrange(image.size[0]):
intensities.append([])

for y in xrange(image.size[1]):
intensities[x].append(image2[x, y] )

plot = []

for x in xrange(image.size[0]):
plot.append(0)

for y in xrange(image.size[1]):
plot[x] += intensities[x][y]

plot = smoothListGaussian(plot, 10)

plot_str = ''

for x in range(len(plot)):
plot_str += str(plot[x]) + "\n"

print(plot_str)
``````

``````>>> from PIL import Image
>>> import numpy as np
>>> i = Image.open(r'C:\Pictures\pics\test.png')
>>> a = np.array(i.convert('L'))
>>> a.shape
(2000, 2000)
>>> b = a.sum(0) # or 1 depending on the axis you want to sum across
>>> b.shape
(2000,)
``````