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...
Related
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
Hello I am trying to display a picture from an url using the Matplotlib module but the problem is the following :
When I execute the code the picture has not a good quality.
Here is my code :
import requests
from PIL import Image
from io import BytesIO
fig, ax = plt.subplots()
picture_url = "https://www.google.fr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
response = requests/get(picture_url)
img = Image.open(BytesIO(response.content))
ax.imshow(img)
plt.show()
Could you help me please ?
Thank you !
While some would say this is the type of answer only a 10x engineer could answer, in this case it only took a 2x!
Try this URL:
https://www.google.fr/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png
The difference is in the "2x" in the URL, which provides a larger png!
The problem is that if the axes is not precisely as large as the image, the image will be interpolated. This results in little artifacts.
You can calculate the figure size and margins such that the image fits exactly into the axes.
import numpy as np
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import requests
from PIL import Image
from io import BytesIO
picture_url = "https://www.google.fr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
response = requests.get(picture_url)
img = np.array(Image.open(BytesIO(response.content)))
dpi = 100
bottom=0.2; left=0.1; right=0.9; top=0.9
width = img.shape[1]/(right-left)/dpi
height= img.shape[0]/(top-bottom)/dpi
fig, ax = plt.subplots(dpi=dpi, figsize=(width, height))
fig.subplots_adjust(bottom=bottom, left=left, right=right, top=top)
ax.imshow(img)
fig.savefig("googlelogo.png")
plt.show()
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[:,:]
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 am new with Python and I would like to have a histogram of the values that I have in a text file. But I get a Histogram with only the first value of my text file (See figure)
enter image description here
My file text is as follows:
2.52549
2.52821
4.69718
4.81173
4.85834
4.87637
3.03348
3.21677
3.28373
3.11296
3.11947
3.14685
3.19684
3.19533
3.20035
3.36578
3.3899
3.40696
3.47841
3.54231
3.54343
5.20521
5.23496
5.23317
6.1397
6.18261
6.18782
6.1184
6.20701
6.25631
And my code as follows
import matplotlib.pyplot as plt
import numpy as np
import plotly.plotly as py
from pylab import show,hist
gaussian_numbers = np.random.randn(1000)
data_Distanz = np.genfromtxt('data_Distance.txt')
x = data_Distanz[0]
plt.hist(x)
plt.title("Histogram")
plt.xlabel("Distance in m")
plt.ylabel("Amplitude")
plt.savefig('Histogram.png', bbox_inches='tight')
show()
I Would appreciate your help. Thanks in advance!