I'm trying to do some mathematical operations on single pixels using PIL.
From the internet I could load an image into a 2d array like this:
from PIL import Image
img_filename = "CNH.JPG"
im = Image.open(img_filename)
im = im.convert('L')
pix = im.load()
I did some changes to a single pixel value aftwards, but when I try to make a division (even if it returns an int) it just rounds to 0 or 255:
>>> i=30
>>> j=45
>>> pix[i,j]
37
>>> pix[i,j]+5
42
>>> pix[i,j]*2
84
>>> pix[i,j]/2
0
>>> pix[i,j]*2
0
It seems the problem I'm having has to do with divisions.
My goal is to normalize my pixels values to floats in range (0,1) instead of (0,255). Therefore I would just divide each pixel value by 255.
Anything I'm missing in Python language?
Also, for future works, I plan on implementing some feature extraction and classification algorithms in Python. Is PIL the right package for this purpose?
Sample code:
from PIL import Image
img_filename = "yourimage.JPG"
im = Image.open(img_filename)
im = im.convert('L')
pix = im.load()
i=30
j=45
print(pix[i,j])
pix[i,j] = pix[i,j]+5
print(pix[i,j])
pix[i,j] = pix[i,j]*2
print(pix[i,j])
pix[i,j] = float(pix[i,j]/2)
print(pix[i,j])
pix[i,j] = pix[i,j]*2
print(pix[i,j])