Combine image and graph with matplotlib with alpha

I have a .png image with alpha and a random template generated using numpy. I want to secure both images using matplotlib. The bottom image should be a random pattern, and above that I want to see the second image (attached at the end of the message).

The code for both images is as follows:

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)

The result is the following image:

enter image description here

To import the image, I use the following code:

# 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)

The image is as follows:

enter image description here

So the final result I want is:

enter image description here

+4
source share
1 answer

Z, imshow, .. , , png -.

:

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')

: enter image description here

, .

ax.axis('off')

enter image description here

+1

Source: https://habr.com/ru/post/1694223/


All Articles