I would like to create a black frame around an image. Unfortunately, I've got the following error.
ValueError: could not broadcast input array from shape (512,512) into shape (562,562)
Here is my code:
import numpy as np
import matplotlib.pyplot as plt
import scipy as sc
import scipy.misc
im = sc.misc.ascent()
blackFrame= np.zeros((im.shape[0]+100,im.shape[1]+100))
blackFrame[50:,50:] = im[:,:]
plt.imshow(blackFrame, cmap="gray", vmin=0, vmax=250)
plt.show()
It works when I write blackFrame[100:,100:] = im[:,:] but it is not what I want.
This might help: blackFrame[50:50+im.shape[0],50:50+im.shape[1]] = im[:,:]
Related
My intention is to use matplotlib to convert a coloured image into a grayscale image and use colormap to display it in the Viridis scale.
The code for that is as follows:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
IMG = mpimg.imread('dog_1.jpg')
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
gray = rgb2gray(IMG)
plt.imshow(gray, cmap='viridis')
plt.show()
The output displayed is proper and as follows:
Output Image
Now, I want to save the output image in a variable as a numpy array to carry out further processing. Can I do it in any way?
plt.imread() returns a 3-dimensional numpy array with RGB layers. Your rgb2gray() function returns a 2-dimensional numpy array with a grayscale image. There is no need to extract a numpy array from the object returned by plt.imshow() when you have two numpy arrays with the image data already. However, if you insist in doing it, you can try the following:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
IMG = mpimg.imread('img.jpg')
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
gray = rgb2gray(IMG)
aximg = plt.imshow(gray, cmap='viridis')
# an array with RGBA data of the image produced by plt.imshow
arr = aximg.make_image(renderer=None, unsampled=True)[0]
I want to check my data and see images containing in images.npy. How to solve that type of error?
TypeError: Invalid shape (20000, 48, 48, 3) for image data
Code:
import numpy as np
from matplotlib import pyplot as plt
images = np.load('images.npy')
plt.imshow(images)
plt.show()
You cannot plot a 4d array that way. Try to split the first dimension 20000 into individual RGB images and then plot them using plt.subplots instead.
Try: (from https://matplotlib.org/3.3.3/tutorials/introductory/images.html)
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('images.npy')
imgplot = plt.imshow(img[0])#plot the first -- notice you are trying to plot 20k images, not 1
I'm trying to get the background of a noisy image based on this code https://www.kaggle.com/rdokov/background-removal
But, I'm getting ValueError. How to resolve this?
import glob
import cv2
from scipy import signal
import numpy as np
a = cv2.imread(glob.glob('lp_train/*.jpg')[0])
import matplotlib.pyplot as plt
a2 = np.asarray(a)/255.
aa = signal.medfilt2d(a2, 11)
plt.imshow(aa)
plt.show()
Error: ValueError: object too deep for desired array
.jpg has 3 channels, so you are sending 3-dimensional array but medfilt2d accepts 2d array.
Two ways you can solve this.
Just read as a grayscale image
import glob
import cv2
from scipy import signal
import numpy as np
a = cv2.imread(glob.glob('lp_train/*.jpg')[0], 0) # grayscale, single channel
import matplotlib.pyplot as plt
a2 = np.asarray(a)/255.
aa = signal.medfilt2d(a2, 11)
plt.imshow(aa)
plt.show()
Take a specific channel (R/G/B)
import glob
import cv2
from scipy import signal
import numpy as np
a = cv2.imread(glob.glob('lp_train/*.jpg')[0])
import matplotlib.pyplot as plt
a2 = np.asarray(a)/255.
aa = signal.medfilt2d(a2[:,:,0], 11) # a specific channel
plt.imshow(aa)
plt.show()
Both should work.
How does matplotlib ensure that a dataset can be within plot with specified size.
How do i from a plot stored as numpy, How do i read the color of the pixels illustration a datapoint (0,4) - in the plot.
example:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
import librosa
import librosa.display
from matplotlib import cm
fig = plt.figure(figsize=(12,4))
min = -1.828067
max = 22.70058
data = np.random.uniform(low=min, high=max, size=(474,40))
librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
plt.show()
raw_input("sadas")
convert = plt.get_cmap(cm.jet)
numpy_output_static = convert(data.T)
plt.imshow(numpy_output_static, aspect = 'auto')
plt.show()
raw_input("asds")
First plot being :
Second plot being:
so the first has been resized to plot size 12,4 where the last basically plots the same data but just using the data shape as size... how do i change that?
Librosa just performs pcolormesh according to the GitHub source code
You need to define another figure with its own size for the second figure.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
import librosa
import librosa.display
from matplotlib import cm
fig = plt.figure(figsize=(12,4))
min = -1.828067
max = 22.70058
data = np.random.uniform(low=min, high=max, size=(474,40))
librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
plt.show()
raw_input("sadas")
convert = plt.get_cmap(cm.jet)
numpy_output_static = convert(data.T)
fig = plt.figure(figsize=(12,4))
plt.imshow(numpy_output_static, aspect = 'auto')
plt.show()
raw_input("asds")
I'm trying to display a grayscale TIFF file using Python and MatPlotLib,
So far I have read the file this:
import scipy as N
import gdal
import sys
import matplotlib.pyplot as pyplot
try:
tif = gdal.Open('filename.tif')
tifArray = tif.ReadAsArray()
except:
print 'The file does not exist.'
sys.exit(0)
band1 = tif.GetRasterBand(1)
band2 = tif.GetRasterBand(2)
band3 = tif.GetRasterBand(3)
band1Array = band1.ReadAsArray()
band2Array = band2.ReadAsArray()
band3Array = band3.ReadAsArray()
But then I don't know what else should I do... I'm so clueless.
Anyone would help me in this?
Once you processed your file into a 2 Array, you could use ANY function in matplotlib that plots 2D arrays, e.g. cmap, imshow etc.
Here is the output with the marbles example
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
img=mpimg.imread('MARBLES.TIF ')
imgplot = plt.imshow(img)
Here is what you get if you view only band3 of the image:
imgplot2 = plt.imshow(band3Array)
plt.show()
Look further into image viewing in MPL and 2D array functions...