加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:39:41 所属栏目:Python 来源:网络整理
导读:我一直在编写一个脚本,基本上我需要它: 使图像灰度(或双色调,我将同时使用两者来查看哪一个更好). 处理每个单独的列并为每列创建净强度值. 将结果吐出到有序列表中. 使用ImageMagick有一种非常简单的方法(虽然您需要一些Linux实用程序来处理输出文本),但我
我一直在编写一个脚本,基本上我需要它:

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

使用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)

解决方法

我看到你正在使用numpy.我会先将灰度图像转换为numpy数组,然后使用numpy沿轴进行求和.额外:当您修复它以接受一维数组作为输入时,您可能会发现平滑函数的运行速度要快得多.

>>> from PIL import Image
>>> import numpy as np
>>> i = Image.open(r'C:Picturespicstest.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,)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读