好的,总体结果将是一张稍微嘈杂的图片。下面是一些示例代码:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
im = Image.open('baboon.png')
l = np.array(im)
data = 50*np.random.randint(2, size=(512,512,3))
l_new = (l+data).astype(np.uint8)
fig, ax = plt.subplots(1,2)
ax[0].imshow(l)
ax[1].imshow(l_new)
fig.show()
这将给出:
在这种情况下,我们必须使用 512x512x3 大小的数组,因为图像有 3 个颜色通道(R、G、B)。另请注意,我将 data 数组乘以 50,因为每个颜色通道都是 8 位(即值从 0 到 255),因此加 1 不会产生太大的明显差异,而加 50 会。
这有帮助吗?
编辑:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
# Load image, convert to greyscale
im = Image.open('baboon.png').convert('L')
l = np.array(im)
data = np.random.randint(2, size=131072)
# To add we need to pad data so it contains the same number of elements
padSize = (l.shape[0]*l.shape[1])-data.shape[0]
data_new = np.pad(data, (0,padSize), mode='constant')
# Now we reshape data
data_reshaped = data_new.reshape((512,512))
l_new = l+data_reshaped
fig, ax = plt.subplots(1,2)
ax[0].imshow(l, cmap=plt.cm.Greys)
ax[1].imshow(l_new,cmap=plt.cm.Greys)
fig.show()