PIL:创建图像颜色亮度的一维直方图?
我一直在编写一个脚本,我基本上需要它:
- 使图像灰度化(或双色调,我将同时使用两者来看看哪个效果更好)。
- 处理每个单独的列并为每个列创建一个净强度值。
- 将结果放入有序列表中。
有一种非常简单的方法可以使用 ImageMagick 来做到这一点(尽管您需要一些 Linux 实用程序来处理输出文本),但我并没有真正了解如何使用 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)
这输出了一些东西(我正在寻找绘制结果的图表),但它看起来与 ImageMagick 输出完全不同。我用它来检测扫描书籍的接缝和内容。
感谢任何提供帮助的人!
我现在有一个(看起来很糟糕的)有效的解决方案:
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')
image2 = image.load()
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)
I've been working on a script, and I need it to basically:
- Make the image greyscale (or bitonal, I will play with both to see which one works better).
- Process each individual column and create a net intensity value for each column.
- Spit the results into an ordered list.
There is a really easy way to do this with ImageMagick (although you need a few Linux utilities to process the output text), but I'm not really seeing how to do this with Python and PIL.
Here's what I have so far:
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)
This outputs something (I am looking to graph the results), but it looks nothing like the ImageMagick output. I'm using this to detect the seam and content of a scanned book.
Thanks to anyone who helps!
I've got a (nasty-looking) solution that works, for now:
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')
image2 = image.load()
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到你正在使用 numpy。我首先将灰度图像转换为 numpy 数组,然后使用 numpy 沿轴求和。奖励:当您修复平滑函数以接受一维数组作为输入时,您可能会发现平滑函数运行得更快。
I see you are using numpy. I would convert the greyscale image to a numpy array first, then use numpy to sum along an axis. Bonus: You'll probably find your smoothing function runs a lot faster when you fix it to accept an 1D array as input.
来自 PIL 文档,
直方图
为您提供图像中每个像素值的像素计数列表。如果您有灰度图像,则将有 256 个不同的可能值,范围从 0 到 255,并且从image.histogram
返回的列表将有 256 个条目。From the docs for PIL,
histogram
gives you a list of pixel counts for each pixel value in the image. If you have a grayscale image, there will be 256 different possible values, ranging from 0 to 255, and the list returned fromimage.histogram
will have 256 entries.