python pillow 处理图片
2020-12-13 03:59
标签:https img salt 添加 像素 int image rom caffe demo1 demo2 参考: python pillow 处理图片 标签:https img salt 添加 像素 int image rom caffe 原文地址:https://www.cnblogs.com/sea-stream/p/11100366.html#打开图片,并随机添加一些椒盐噪声
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‘)) #打开图像并转化为数字矩阵
#随机生成5000个椒盐
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()
#将图像二值化,像素值大于128的变为1,否则变为0
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‘).convert(‘L‘)) #打开图像并转化为数字矩阵
rows,cols=img.shape
for i in range(rows):
for j in range(cols):
if (img[i,j]128):
img[i,j]=0
else:
img[i,j]=1
plt.figure("cat_black&white")
plt.imshow(img,cmap=‘gray‘)
plt.axis(‘off‘)
plt.show()
https://blog.csdn.net/Hanging_Gardens/article/details/79014160