python pillow with pictures

 

demo1

# Open the picture, add some salt and pepper and random noise
 from PIL Import Image 
Import numpy AS NP 
Import matplotlib.pyplot AS plt 
img = np.array (Image.open ( ' /home/keysen/caffe/examples/images/cat.jpg ' )) # open the image into a digital matrix and 

# 5000 randomly generated pepper 
rows, cols, DIMS = img.shape
 for I in Range ( 5000 ): 
    X = np.random.randint ( 0 , rows) 
    Y = np.random .randint ( 0 , cols) 
    IMG [X, Y ,:] = 255 

plt.figure ( " cat_salt ")
plt.imshow(img)
plt.axis('off')
plt.show()

 

demo2

# Binarized image, the pixel value becomes greater than 128 to 1, otherwise becomes 0
 from the PIL Import Image 
Import numpy AS NP 
Import matplotlib.pyplot AS PLT 
IMG = np.array (Image.open ( ' / Home / keysen / Caffe / examples / images / cat.jpg ' ) .convert ( ' L ' )) and # opens into a digital image matrix 

rows, cols = img.shape
 for I in Range (rows):
     for J in Range (cols):
         IF (IMG [I, J] <= 128 ): 
            IMG [I, J] = 0 
        the else  :
            IMG [I, J]=1

plt.figure("cat_black&white")
plt.imshow(img,cmap='gray')
plt.axis('off')
plt.show()

 

Reference:
https://blog.csdn.net/Hanging_Gardens/article/details/79014160

 

 

 

Guess you like

Origin www.cnblogs.com/sea-stream/p/11100366.html