I want to use PIL.Image to save a figure and I want to use matplotlib cmaps to map the data to a color. I have tried the following:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
from PIL import Image
M, N = 255, 255
data = np.arange(M*N).reshape((M, N))
cmap_name = 'autumn_r'
cmap_name = cmap_name
cmap = plt.get_cmap(cmap_name)
norm = mpl.colors.Normalize()
scalarMap = cm.ScalarMappable(norm=norm, cmap=cmap)
plt.imshow(data, cmap=cmap)
plt.show()
colors = scalarMap.to_rgba(data)
image = Image.fromarray((colors[:, :, :3]*256).astype(np.uint8))
image.show()
Which plots this in matplotlib:
However, it plots this in the Image:
How can I get PIL.Image to show the same colors as matplotlib?
If its possible to also add the alpha channel, that will be useful
You need to give PIL the same normalisation and cmap you give matplotlib, so it can do the same mapping from 2D array -> normalised -> mapped to cmap.
I rewrote your sample code to be a bit simpler:
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from PIL import Image
M, N = 255, 255
data = np.arange(M*N).reshape((M, N))
cmap = cm.autumn_r
plt.imshow(data, cmap=cmap)
norm = mpl.colors.Normalize()
Then your answer is:
Image.fromarray(np.uint8(cmap(norm(data))*255)).show()
(Found the solution here, might be a dupe.)
Related
I have been set this assignment:
I don't know what is wrong with my code below:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
img = mpimg.imread('Ca.PNG')
imgplot = plt.imshow(img)
img = mpimg.imread('Ca.PNG')
print(img)
lum_img = img[:, :, 0]
plt.imshow(lum_img)
plt.show()
print(lum_img)
Your code seems fine, maybe just a bit disorganized. Maybe sprinkle in a few comments to help you keep track and think through what you're doing. For example...
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# Load the image.
img = mpimg.imread('Ca.PNG')
# Take the red channel.
lum_img = img[:, :, 0]
# Plot the single-channel array.
plt.imshow(lum_img, cmap='gray')
plt.show()
This should produce a grayscale plot.
I am trying to find the phase spectrum of an image after applying DFT in python, here is the code i have used.
`
import numpy as np
import cv2
from matplotlib import pyplot as plt
img=cv2.imread('/content/drive/My Drive/IP assg2/im1.jpg')
img = cv2.cvtColor(sm1,cv2.COLOR_BGR2GRAY)
dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
`
I am not sure how to move on from here, as all the tutorials i have come across are related to MATLAB.
You can try this:
import numpy as np
import cv2
from matplotlib import pyplot as plt
img=cv2.imread('input.png')
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dft = np.fft.fft2(img)
dft_shift = np.fft.fftshift(dft)
phase_spectrum = np.angle(dft_shift)
ax1 = plt.subplot(1,2,1)
ax1.imshow(img, cmap='gray')
ax2 = plt.subplot(1,2,2)
ax2.imshow(phase_spectrum, cmap='gray')
plt.show()
In the skimage Segmentation tutorial, a 3D surface plot of the elevation map generated from the sobel function was plotted.
>>> from skimage.filters import sobel
>>> elevation_map = sobel(coins)
Question: elevation_map appears to be a 2D numpy.ndarray. How do we generate the 3D map shown using this?
This is likely produced using Paraview/VTK;
Try to play around the following:
from skimage import data
from skimage.filters import sobel
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import cm
from scipy.ndimage import zoom
coins = data.coins()
coins = zoom(coins, 10)
elevation_map = sobel(coins)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
m, n=elevation_map.shape
X, Y = np.meshgrid(np.arange(n), np.arange(m))
ax.plot_surface(X, Y, elevation_map, cmap=cm.viridis, antialiased=False)
ax.axis("off")
ax.set_facecolor('black')
plt.show()
I want to display one white pixel with mathplot:
import numpy as np
import matplotlib.pyplot as plt
plt.imshow([[0.99]], cmap='gray', interpolation='nearest')
plt.show()
but it shows black. Why?
The problem is that you only give imshow one value, so the colour scale is set around that value and it gets painted as the minimum value of the scale (thus black).
Specify vmin and vmax, as shown here:
import numpy as np
import matplotlib.pyplot as plt
plt.imshow([[0.99]], cmap='gray', interpolation='nearest', vmin=0, vmax=1)
plt.show()
More importantly, you need vmax, which will be mapped to white, to be the value you give imshow, and vmin to be smaller than that:
import numpy as np
import matplotlib.pyplot as plt
max_value = np.random.random()
min_value = -max_value # for instance
plt.imshow([[max_value]], cmap='gray', interpolation='nearest',
vmin=min_value, vmax=max_value)
plt.show()
I want to plot a 3D histogram of my RGB image.
Below is my code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import imread
import pylab
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
img1 = imread('image.jpg')
img_reshaped = img1.reshape(img1.shape[0] * img1.shape[1], img1.shape[2])
hist, edges = np.histogramdd(img_reshaped, bins=(100, 100, 100))
Please tell me how to plot the hist histogram that I have obtained.
Have you taken a look at the 3d histogram example from the matplotlib gallery?
see: http://matplotlib.org/examples/mplot3d/hist3d_demo.html