私はスクリプトに取り組んできましたが、基本的に次のことを行う必要があります。
- 画像をグレースケールにします(または二調です。どちらがうまく機能するかを確認するために両方で遊んでみます)。
- 個々の列を処理し、各列の正味強度値を作成します。
- 結果を順序付きリストに分割します。
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)