I have a .png image with alpha channel and a random pattern generated with numpy.
I want to supperpose both images using matplotlib. The bottom image must be the random pattern and over this, I want to see the second image (attached in the end of the post).
The code for both images is the following:
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:
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 the following:
Thus, the final result that I want is:
You can make an image-like array for Z and then just use imshow to display it before the image of the buttons, etc. Note that this only works because your png has an alpha channel.
Code:
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')
Output:
You can also turn off axes if you prefer.
ax.axis('off')
Related
I'm trying to plot x and y list then convert it to numpy array but with 0 and 255 value only, black and white.
So i tried this , it's working but i got an array with intermediary colors:
print(np.unique(data))
[0 1 2 3 4 5 6 7 8 9 10 11 ... 255]
So my solution for now is:
import numpy as np
import cv2
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
input_file = "image.png"
fig = plt.figure()
# drawn points
x = [1,2,3,4]
y = [1,2,3,4]
plt.axis('off')
plt.plot(x, y, color='black', linewidth=1)
plt.savefig(f"{input_file}.IMG.png", dpi=100)
def plot2Grayscale(image):
im = cv2.imread(image)
gray= cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
cmap = ListedColormap(['black', 'white'])
plt.imsave(image, gray, cmap = cmap)
gray = cv2.imread(image, 0)
return gray
data = plot2Grayscale(f"{input_file}.IMG.png")
print(np.unique(data))
[0 255]
What i'm asking is there is a way to got the binary array from plot without having to save it then read it like i did?
You are close to the solution and the linked answer is almost sufficient to do what you ask, you just need to disable the anti-aliasing in plt.plot.
The following code should work
import numpy as np
import matplotlib.pyplot as plt
# drawn points
x = [1,2,3,4]
y = [1,2,3,4]
fig = plt.figure()
plt.axis('off')
# Use antialised=False instead of the default antialiased=True
plt.plot(x, y, color='black', antialiased=False, linewidth=1)
fig.canvas.draw()
# Covert the plot to a numpy array
data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
# Check that the produced array contains 0 and 255 only
print(np.unique(data))
Sidenote: the linked answer uses np.fromstring that is now deprecated in favor of np.frombuffer.
I am trying to plot a RGBA image with a colorbar representing color values.
The RGBA image is generated from raw data, transforming the 2d data array into a 6d-array with x, y, [R, G, B and A] according to the color input. E.g. 'green' will make it fill just the G channel with the values from the 2d-array, leaving R and B = 0 and A = 255. Like this:
All solutions I found would apply a color map or limit the vmin and vmax of the colorbar but what I need is a colorbar that goes from pitch black to the brightest color present in the image. E.g. if I have an image in shades of purple, the color bar should go from 0 to 'full' purple with only shades of purple in it. The closest solution I found was this (https://pelson.github.io/2013/working_with_colors_in_matplotlib/), but it doesn't fit a "general" solution.
An image I'm getting is given below.
import numpy as np
from ImgMath import colorize
import matplotlib.pyplot as plt
import Mapping
data = Mapping.getpeakmap('Au')
# data shape is (10,13) and len(data) is 10
norm_data = data/data.max()*255
color_data = colorize(norm_data,'green')
# color_data shape is (10,13,4) and len(color_data) is 10
fig, ax = plt.subplots()
im = plt.imshow(color_data)
fig.colorbar(im)
plt.show()
You could map your data with a custom, all-green, colormap
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
# input 2D array
data = np.random.randint(0,255, size=(10,13))
z = np.zeros(256)
colors = np.linspace(0,1,256)
alpha = np.ones(256)
#create colormap
greencolors = np.c_[z,colors,z,alpha]
cmap = ListedColormap(greencolors)
im = plt.imshow(data/255., cmap=cmap, vmin=0, vmax=1)
plt.colorbar(im)
plt.show()
I would like to calculate the average grayscale profile of an image.
In my next code I have the evolution of the grayscale of all the pixels of the image, but how to make the average? To obtain a single curve, the average of all the others. Thank you
import imageio
import numpy as np
from matplotlib.pyplot import *
from matplotlib import pyplot as plt
img = imread("lion.jpg")
#
red = img[:,:,0]
green = img[:,:,1]
blue = img[:,:,2]
#print(np.mean(img)
line = red[:,:]
#here How to calculate the average grayscale profile?
figure(figsize=(8,4))
plot(line)
plt.show()
If I understand correctly, you want to have a profile of the greyscale image along both directions of the image.
import numpy as np
from matplotlib import pyplot as plt
img = plt.imread("https://i.stack.imgur.com/9qe6z.png")
# convert to grayscale
gray = img.mean(axis=2)
# or
#gray = np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
# profile along x -> mean along y
prof_x = gray.mean(axis=0)
# profile along y -> mean along x
prof_y = gray.mean(axis=1)
fig, (ax, ay) = plt.subplots(nrows=2, sharex=True, sharey=True)
ax.plot(prof_x, label="x profile")
ay.plot(prof_y, color="C1", label="y profile")
fig.legend()
plt.show()
enter image description hereI hope I have not over-looked as previously asked question. I don't think so.
I have an image of a spectrum. I have several laser lines for calibration. Since the laser lines and the spectrum were collected in the same way they should be correlated in pixel distance. The relationship between pixel number and wavelength is nonlinear. I have fit the pixel number along the x-axis against the wavelength of the laser lines (blue # 405nm green # 532nm and red # 650nm) using a 3rd degree polynomial with high correlation. I want to plot the spectrum by computing the wavelength( nm) directly from the pixel number and display the wavelength beneath the spectrum. Is this possible without overlapping the image on another figure? spectrograph of Laser Lines
import matplotlib.pyplot as plt
from scipy import ndimage
from pylab import *
import numpy as np
import skimage
image= laser_lines
print(image.shape)
for i in range(image.shape[1]):
x=i^3*-3.119E-6+2.926E-3*i^2+0.173*i+269.593
for j in range(image.shape[0]):
y=image[i,j]
imshow(image)
plt.show()
Probably the easiest option is to use a pcolormesh instead of an imshow plot. The pcolormesh shows the edges of a grid, such that you might simply transform the original grid using the functional dependence between pixels and wavelength to define the edges of each pixel in terms of wavelength.
import numpy as np
import matplotlib.pyplot as plt
image = np.sort(np.random.randint(0,256,size=(400,600)),axis=0)
f = lambda i: i**3*-3.119E-6+2.926E-3*i**2+0.173*i+269.593
xi = np.arange(0,image.shape[1]+1)-0.5
yi = np.arange(0,image.shape[0]+1)-0.5
Xi, Yi = np.meshgrid(xi, yi)
Xw = f(Xi)
fig, (ax) = plt.subplots(figsize=(8,4))
ax.pcolormesh(Xw, Yi, image)
ax.set_xlabel("wavelength [nm]")
plt.show()
If the image has 3 colorchannels, you need to use the color argument of pcolormesh to set the color of each pixel, as shown in this question: Plotting an irregularly-spaced RGB image in Python
import numpy as np
import matplotlib.pyplot as plt
r = np.sort(np.random.randint(0,256,size=(200,600)),axis=1)
g = np.sort(np.random.randint(0,256,size=(200,600)),axis=0)
b = np.sort(np.random.randint(0,256,size=(200,600)),axis=1)
image = np.dstack([r, g, b])
color = image.reshape((image.shape[0]*image.shape[1],image.shape[2]))
if color.max() > 1.:
color = color/255.
f = lambda i: i**3*-3.119E-6+2.926E-3*i**2+0.173*i+269.593
xi = np.arange(0,image.shape[1]+1)-0.5
yi = np.arange(0,image.shape[0]+1)-0.5
Xi, Yi = np.meshgrid(xi, yi)
Xw = f(Xi)
fig, (ax) = plt.subplots(figsize=(8,4))
pc = ax.pcolormesh(Xw, Yi, Xw, color=color )
pc.set_array(None)
ax.set_xlabel("wavelength [nm]")
plt.show()
I need to plot over an image. I use this code to display the image:
plt.figure()
mngr = plt.get_current_fig_manager()
fname = 'erausal-valence.jpg'
image = Image.open(fname).convert("L")
arr = np.asarray(image)
plt.imshow(arr)
Over on this image I need to plot an array build with components:
for i in range(0, len(BPM)):
for k in range(0, len(BPM)):
(X[k], Y[k]) = pol2cart(BPM[k], -SC[k]);
plt.plot(X[k], Y[k])
but plot shows the image and doesn't show the plot of the X and Y arrays.
What are the values of X and Y? I've used the code below and could plot a graph over an image.
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
imageFile = cbook.get_sample_data('ada.png')
image = plt.imread(imageFile)
plt.imshow(image)
coords = [0, 100, 200, 300, 400, 500, 600]
plt.plot(coords, coords, 'r--', linewidth=2)
plt.show()
To add the background image, you have to add this import :
from matplotlib import cbook
And now to fill it in the background you should add these lines before the show instruction:
imageFile = cbook.get_sample_data('<Path_to_your_image>')
image = plt.imread(imageFile)
plt.imshow(image)