1.
>>> from PIL import Image
>>> lena = Image.open("/data/lena.jpg")
>>> lena_L =lena.convert("L")
>>> lena_L_rgb =lena_L.convert("RGB")
>>>lena.getpixel((0,0)) (197, 111, 78)
>>>lena_L.getpixel((0,0)) 132
>>>lena_rgb.getpixel((0,0)) (132, 132, 132)
import cv2
import numpy as np
def salt(img, n):
for k in range(n):
i = int(np.random.random() * img.shape[1])
j = int(np.random.random() * img.shape[0])
if img.ndim == 2:
img[j,i] = 255
elif img.ndim == 3:
img[j,i,0]= 0
img[j,i,1]= 0
img[j,i,2]= 0
return img
if __name__ == '__main__':
img = cv2.imread("test1.jpg")
h, w = source.shape[:2] #彩色图像显示图像的大小
saltImage = salt(img, 1000) cv2.imshow("Salt", saltImage) cv2.waitKey(0) cv2.destroyAllWindows()