$ cd image $ mkdir e2 $ cd e2
# e201.py ... make histogram
import Image
img = Image.open('sample.pgm')
width, height = img.size
hist = [0,]*256
for j in range(height):
for i in range(width):
pixval = img.getpixel((i, j))
hist[pixval] += 1
for t in range(len(hist)):
print '%3d %4d' % (t, hist[t])
$ python e201.py
# e201.py ... make histogram
import Image
import pylab
img = Image.open('sample.pgm')
width, height = img.size
hist = [0,]*256
for j in range(height):
for i in range(width):
pixval = img.getpixel((i, j))
hist[pixval] += 1
pylab.plot(hist)
pylab.xlim((0, 255))
pylab.show()
#!/usr/bin/env python
# pgmhist.py ... make histogram
import sys
import Image
import pylab
if len(sys.argv) == 1:
print 'Usage: %s pgmfile.pgm' % sys.argv[0]
sys.exit(1)
else:
fname = sys.argv[1]
img = Image.open(fname)
width, height = img.size
hist = [0,]*256
for j in range(height):
for i in range(width):
pixval = img.getpixel((i, j))
hist[pixval] += 1
pylab.plot(hist)
pylab.xlim((0, 255))
pylab.show()
./pgmhist.py sample.pgm
# e202.py ... invert image
import Image
im0 = Image.open('sample.pgm')
width, height = im0.size
im1 = Image.new('L', (width, height))
for j in range(height):
for i in range(width):
x = im0.getpixel((i,j))
y = 255 - x
im1.putpixel((i,j), y)
im1.show()
y = 2 * x ... ( x < 128 )
255 - 2 * (x - 128) ... ( x >= 128 )
# e205.py ... bright
import Image
# make lookup table
brighter = []
for x in range(256):
y = 0.5 * (x - 255) + 255
brighter.append(int(y))
im0 = Image.open('sample.pgm')
im1 = im0.point(brighter)
im1.show()