I need to load an image file with matplotlib and see the coordinates of points within it, as if it were a simple x,y scatter plot.
I can assume that the x axis extension is [0, 1], and the y axis follows the same scaling. I can load the above image file with
from PIL import Image
im = Image.open("del.png")
im.show()
but this uses ImageMagick (I'm on a Linux system) to display the image, and no coordinates are shown in the bottom left part of the plot window as would for a simple data plot:
Use pyplot for that:
from matplotlib import pyplot as plt
plt.imshow(plt.imread('del.png'))
Related
I want to automate one process, and I need to place some kind of pointer on my image. I found a great solution which works exactly as I would like, but its disadvantage is that it destroys my picture quality. I want to keep same size of the build in picture.
Bellow I share my code and error, which I receive. I would be grateful for your help :)
from matplotlib import image
from matplotlib import pyplot as plt
from PIL import Image
# to read the image stored in the working directory
# data = image.imread(file_name)
data = Image.open('File_name')
x, y = data.size
# to draw a point on co-ordinate (200,300)
plt.figure(figsize=(x, y))
plt.plot(650, 310, marker='*', color="red")
# plt.axis('off')
plt.imshow(data)
File = "File_name"
plt.savefig(File)
plt.show()
ValueError: Image size of 105480x55224 pixels is too large. It must be less than 2^16 in each direction.
How do I convert a 3D object in any STL file into a JPG or PNG image.
I tried searching a little bit online but I wasn't been able to arrive at finding any possible solutions.
Can anyone help me with the code that can do this straight forward task with Python? IS there any libraries that can help with that?
EDIT :
Code Sample:
from mpl_toolkits import mplot3d
from matplotlib import pyplot
import pathlib
DIR = str(pathlib.Path(__file__).parent.resolve()).replace('\\', '/')
path = f'{DIR}/any_stl_file.stl'
# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
# Load the STL files and add the vectors to the plot
your_mesh = mesh.Mesh.from_file(path)
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
# Auto scale to the mesh size
scale = your_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)
pyplot.savefig(f"{DIR}/the_image.jpg")```
Take a look at https://pypi.org/project/numpy-stl/
This code snippet is from the link above:
from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot
# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
# Load the STL files and add the vectors to the plot
your_mesh = mesh.Mesh.from_file('tests/stl_binary/HalfDonut.stl')
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
# Auto scale to the mesh size
scale = your_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)
# Show the plot to the screen
pyplot.show()
To save a pyplot object as an image:
pyplot.savefig("file_name.jpg")
I am trying to display GeoTIFF file in specified area. What I want to do is project multiband GeoTIFF file to specified area.
My GeoTIFF is satellite image containing 3 bands over area of Europe. I want to project it to area of Central Europe (extent defined by lat/lot). I have been trying to solve this using rasterio but I've been unlucky so far. This is what I get after executing:
import georaster
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.pyplot import figure
#define path to geotiff
file = "/home/lubomir/Desktop/Sentinel3_OLCI/RGB/OLCI_201812140859_Natural_Color.tif"
m = Basemap(epsg=3395,llcrnrlat=45,urcrnrlat=55,\
llcrnrlon=5,urcrnrlon=25,lat_ts=15,resolution='f')
m.drawcoastlines(linewidth=0.5, color='g')
m.fillcontinents(color='beige')
m.drawcountries(linewidth=0.5, color='m')
#load GeoTIFF multiband file
image = georaster.MultiBandRaster(file)
plt.imshow(image.r, extent=image.extent, zorder=10)
plt.savefig('test.tiff')
plt.show()
without_geotiff
As you can see, generated image does not contain my GeoTIF. Any idea how to solve this ?
In order to make use of the Basemap's projection, you must use
m.imshow(image.r, extent=image.extent, zorder=10)
in place of
plt.imshow(image.r, extent=image.extent, zorder=10)
Hope it helps.
I am trying to view a tif satellite image which has 4 bands. I want to remove the last band (NIR) and view the RGB image only, so I am trying to split the NIR from the rest of the image. Here is my code
import rasterio
from rasterio.plot import show
from matplotlib import pyplot as plt
from rasterio import plot
import numpy as np
#to display RGB
dataset = rasterio.open('2.tif')
%matplotlib inline
plot.show(dataset.read([1,2,3]), cmap="gray")
#to display just the red band
%matplotlib inline
plot.show(dataset.read(4), cmap="gray")
I provided a screen shot of the code and the output I am getting
Displaying just 1 band seems fine, but any idea why I keep seeing an image with a yellow and white color scheme when I try to display RGB bands together? I thought it's a cmap issue at the beginning, but even when I add 'cmap="gray"' the color of the image remains the same.
I have a .fit file. I have read the file, displayed the image with scale. When I want to write this image in .png file, the .png file is displaying the image without scale. I am attaching the code that I have tried.
import pyfits
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
hdulist = pyfits.open('HMI20170425_134641_6173.fits')
image_data = hdulist[0].data
hdulist.close()
fig=plt.imshow(image_data, cmap='gray')
plt.colorbar()
fig.write_png('image.png')
It is showing output image with scale. However, the 'image.png' file showing image without scale.
Please help me in this regard.
I guess what you call the scale is actually the colorbar ? Which indeed is missing when you use fig.write_png because here you are saving only the image part of the plot. You should use plt.savefig instead:
# use astropy instead of pyfits which is no more maintained
import astropy.io.fits as pyfits
import matplotlib.pyplot as plt
%matplotlib inline
image_data = pyfits.getdata('HMI20170425_134641_6173.fits')
plt.imshow(image_data, cmap='gray')
plt.colorbar()
plt.savefig('image.png')