我有一个带有alpha通道的.png图像和一个由numpy生成的随机模式。我想使用matplotlib来假设这两个图像。底部的图像必须是随机模式,在这上面,我想看到第二个图像(附在文章的末尾)。
这两种图像的代码如下:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# Random image pattern
fig = plt.subplots(figsize = (20,4))
x = np.arange(0,2000,1)
y = np.arange(0,284,1)
X,Y = np.meshgrid(x,y)
Z = 0.6+0.1*np.random.rand(284,2000)
Z[0,0] = 0
Z[1,1] = 1
# Plot the density map using nearest-neighbor interpolation
plt.pcolormesh(X,Y,Z,cmap = cm.gray)结果如下所示:

要导入图像,我使用以下代码:
# Sample data
fig = plt.subplots(figsize = (20,4))
# Plot the density map using nearest-neighbor interpolation
plt.imread("good_image_2.png")
plt.imshow(img)
print(img.shape)图像如下:

因此,我想要的最后结果是:

发布于 2018-02-28 09:44:23
您可以为Z制作一个类似图像的数组,然后只需使用imshow在按钮图像之前显示它,等等。注意,这只是因为png有一个alpha通道才能工作。
代码:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# Plot the density map using nearest-neighbor interpolation
img = plt.imread("image.png")
(xSize, ySize, cSize) = img.shape
x = np.arange(0,xSize,1)
y = np.arange(0,ySize,1)
X,Y = np.meshgrid(x,y)
Z = 0.6+0.1*np.random.rand(xSize,ySize)
Z[0,0] = 0
Z[1,1] = 1
# We need Z to have red, blue and green channels
# For a greyscale image these are all the same
Z=np.repeat(Z,3).reshape(xSize,ySize,3)
fig = plt.figure(figsize=(20,8))
ax = fig.add_subplot(111)
ax.imshow(Z, interpolation=None)
ax.imshow(img, interpolation=None)
fig.savefig('output.png')输出:

如果您愿意,也可以关闭轴。
ax.axis('off')

https://stackoverflow.com/questions/49025832
复制相似问题