PIL:创建图像颜色亮度的一维直方图? [英] PIL: Create one-dimensional histogram of image color lightness?

查看:140
本文介绍了PIL:创建图像颜色亮度的一维直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写一个脚本,基本上我需要它:

I've been working on a script, and I need it to basically:


  • 使图像灰度(或双色调,我将同时使用两者来查看哪一个效果更好。)

  • 处理每个列并为每列创建净强度值。

  • 吐出将结果转换为有序列表。

使用ImageMagick有一种非常简单的方法(尽管您需要一些Linux实用程序来处理输出文本),但我真的没有看到如何使用Python和PIL。

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)

这输出了一些东西(我想要绘制结果图ts),但它看起来不像ImageMagick输出。我用它来检测扫描书的接缝和内容。

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.

感谢任何帮助过的人!

我现在有一个(讨厌的)解决方案,现在可以使用:

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)


推荐答案

我看到你正在使用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.

>>> 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,)

这篇关于PIL:创建图像颜色亮度的一维直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆