PIL:创建图像颜色亮度的一维直方图?

发布于 2024-10-02 09:39:45 字数 1690 浏览 4 评论 0原文

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

  • 使图像灰度化(或双色调,我将同时使用两者来看看哪个效果更好)。
  • 处理每个单独的列并为每个列创建一个净强度值。
  • 将结果放入有序列表中。

有一种非常简单的方法可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2024-10-09 09:39:45

我看到你正在使用 numpy。我首先将灰度图像转换为 numpy 数组,然后使用 numpy 沿轴求和。奖励:当您修复平滑函数以接受一维数组作为输入时,您可能会发现平滑函数运行得更快。

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

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,)
深海里的那抹蓝 2024-10-09 09:39:45

来自 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 from image.histogram will have 256 entries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文