Monday, October 19, 2015

Future, PIL, Numpy and Pylab in Python

 I use it to load an image,
 convert to grayscale,
 read the value of every pixel,
 print it,
 create the diagonal cross section and record the value
 and then plot it.

 Yeah, that's the story of code below


from __future__ import  print_function
from PIL import  Image
import numpy as np
from pylab import *

im  = Image.open("gb/lena.jpg",'r')
gr  = im.convert('L')
a   = im.load()
#b   = gr.load()
b   =np.asarray(gr.getdata(),dtype=np.float64).reshape((gr.size[1],gr.size[0]))

c = []
for i in arange(len(b[0])):
    c.append(b[i][i])

b   =np.asarray(b,dtype=np.uint8)


img = Image.fromarray(b,mode='L')
img.save('gb/gray.jpg')

print('a[0,0]    = ',a[0,0])
print('a[0,0][0] = ',a[0,0][0])
print('a[0,0][1] = ',a[0,0][1])
print('a[0,0][2] = ',a[0,0][2])
print(b[0,0])

plot (c)

xlabel('i')
ylabel('A')
title('cross section')
grid(True)
savefig("gb/test.png")
show()

.