python - PIL: Create one-dimensional histogram of image color lightness? -
i've been working on script, , need basically:
- make image greyscale (or bitonal, play both see 1 works better).
- process each individual column , create net intensity value each column.
- spit results ordered list.
there easy way imagemagick (although need few linux utilities process output text), i'm not seeing how python , pil.
here's have far:
from pil import image image_file = 'test.tiff' image = image.open(image_file).convert('l') histo = image.histogram() histo_string = '' in histo: histo_string += str(i) + "\n" print(histo_string)
this outputs (i looking graph results), looks nothing imagemagick output. i'm using detect seam , content of scanned book.
thanks helps!
i've got (nasty-looking) solution works, now:
from pil import image import numpy def smoothlistgaussian(list,degree=5): window=degree*2-1 weight=numpy.array([1.0]*window) weightgauss=[] 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) 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 = [] x in xrange(image.size[0]): intensities.append([]) y in xrange(image.size[1]): intensities[x].append(image2[x, y] ) plot = [] x in xrange(image.size[0]): plot.append(0) y in xrange(image.size[1]): plot[x] += intensities[x][y] plot = smoothlistgaussian(plot, 10) plot_str = '' x in range(len(plot)): plot_str += str(plot[x]) + "\n" print(plot_str)
i see using numpy. convert greyscale image numpy array first, use numpy sum along axis. bonus: you'll find smoothing function runs lot faster when fix accept 1d array input.
>>> pil import image >>> import numpy np >>> = image.open(r'c:\pictures\pics\test.png') >>> = np.array(i.convert('l')) >>> a.shape (2000, 2000) >>> b = a.sum(0) # or 1 depending on axis want sum across >>> b.shape (2000,)
Comments
Post a Comment