相关疑难解决方法(0)

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)
Run Code Online (Sandbox Code Playgroud)

这会输出一些内容(我希望对结果进行图形化),但它看起来与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 …
Run Code Online (Sandbox Code Playgroud)

python image-processing scanning python-imaging-library

3
推荐指数
2
解决办法
2万
查看次数