SoFunction
Updated on 2024-12-20

Implementing pixel access in images with python image processing

In some of the previous examples, we have used () to open an image, and then directly operate on this PIL object. This is fine if it's just a simple operation, but if the operation is slightly more complex, it's a bit of a struggle. Therefore, usually after we load the image, we are converting the image into a matrix for more complex operations.

The numpy and scipy libraries are utilized in python for various data manipulations and scientific calculations. We can install these libraries directly via pip

pip install numpy
pip install scipy

In the future, we will need to import these packages whenever we do digital image processing in python:

from PIL import Image
import numpy as np
import  as plt

The image is opened and converted to a matrix and displayed:

from PIL import Image
import numpy as np
import  as plt
img=(('d:/')) # Open the image and convert it to a digital matrix
("dog")
(img)
('off')
()

Calling the array() function in numpy will convert a PIL object to an array object.

To view image information, use the following methods:

print  
print  
print  
print type(img)

If it's an RGB image, then after converting to array, it becomes a 3D matrix of rows*cols*channels,so we can use img[i,j,k] to access the pixel values.

Example 1: Open the image and add some random pretzel noise

from PIL import Image
import numpy as np
import  as plt
img=(('d:/'))

#5000 randomly generated pretzels
rows,cols,dims=
for i in range(5000):
  x=(0,rows)
  y=(0,cols)
  img[x,y,:]=255
  
("beauty")
(img)
('off')
()

Example 2: Binarize the lena image, pixel values greater than 128 become 1, otherwise 0

from PIL import Image
import numpy as np
import  as plt
img=(('d:/pic/').convert('L'))

rows,cols=
for i in range(rows):
  for j in range(cols):
    if (img[i,j]<=128):
      img[i,j]=0
    else:
      img[i,j]=1
      
("lena")
(img,cmap='gray')
('off')
()

If you want to operate on more than one pixel, you can use the array slicing method of access. The slicing method returns the pixel values for which the array was accessed at the specified interval subscript. Here are some examples for grayscale images:

img[i,:] = im[j,:] # Assign the value in row j to row i
img[:,i] = 100 # Set all values in column i to 100
img[:100,:50].sum() # Calculate the sum of all values in the first 100 rows and 50 columns
img[50:100,50:100] # 50~100 rows, 50~100 columns (excluding the 100th row and 100th column)
img[i].mean() # Average of all values in row i
img[:,-1] # Last column
img[-2,:] (or im[-2]) # In the penultimate row

This is the whole content of this article.