Using satpy, Blending multiple satellite image - python

I want blend multiple satellite images. but an error occured. I followed the example on the satpy document.
here is code and netcdf file is here : https://drive.google.com/drive/folders/1zp6EBVfjuh41LDRRZo4PJoeGGGn13AKy?usp=sharing
from glob import glob
from satpy import Scene, MultiScene, DataQuery
from satpy.utils import debug_on
debug_on()
areaid = 'worldeqc3km70'
eumetsat = glob('E:/Global/combine_test/MSG4-SEVI-MSG15-0100-NA-20210801000010.306000000Z-20210801001259-4774254.nat')
goes17 = glob('E:/Global/combine_test/OR_ABI-L1b-RadF-M6C13_G17_s20212130000319_e20212130009396_c20212130009445.nc')
gk2a = glob('E:/Global/combine_test/gk2a_ami_le1b_ir105_fd020ge_202108010000.nc')
goes17_scene = Scene(reader="abi_l1b", filenames=goes17)
eumetsat_scene = Scene(reader="seviri_l1b_native", filenames=eumetsat)
gk2a_scene = Scene(reader="ami_l1b", filenames=gk2a)
goes17_scene.load(["C13"])
eumetsat_scene.load(['IR_108'])
gk2a_scene.load(["IR105"])
mscn = MultiScene([goes17_scene, eumetsat_scene, gk2a_scene])
#groups = {DataQuery(name='IR_group', wavelength=(9.8, 10.8, 11.8)): ['C13', 'IR105', 'IR_108']}
groups = {DataQuery(name="IR_group", wavelength=(10, 11, 12)): ['C13', 'IR_108', 'IR105']}
mscn.group(groups)
print(mscn.loaded_dataset_ids)
resampled = mscn.resample(areaid, reduce_data=False)
blended = resampled.blend()
blended.save_datasets(filename='./test_{area}.png'.format(area=areaid))
Error message:
RuntimeError: None of the requested datasets have been generated or could not be loaded. Requested composite inputs may need to have matching dimensions (eg. through resampling).

As mentioned in the comments this is a known bug that will hopefully be fixed in the next couple weeks. Follow issue 2089 for more information.
The short-term workaround is to make your own "blend" method that handles things the way you expect:
from satpy.multiscene import stack
def my_blend(mscn, common_datasets, blend_function=stack):
new_scn = Scene()
for ds_id in common_datasets:
datasets = [scn[ds_id] for scn in mscn.scenes if ds_id in scn]
new_scn[ds_id] = blend_function(datasets)
return new_scn
blended = my_blend(resampled, ["ir_group"])

Related

OSError : Cannot Find sndfile library

I am trying to deploy a Web Application in Streamit Cloud and am facing the Error.
ERROR: pip's dependency resolver does not currently
take into account all the packages that are installed.
This behaviour is the source of the following dependency conflicts.
tensorflow 2.11.0 requires protobuf<3.20,>=3.9.2, but you have
protobuf 3.20.1 which is incompatible.googleapis-common-protos
1.57.0 requires protobuf!=3.20.0,!=3.20.1,!=4.21.1,!=4.21.2,!=4.21.3,
!=4.21.4,!=4.21.5,<5.0.0dev,>=3.19.5, but you have protobuf 3.20.1
which is incompatible.Successfully installed protobuf-3.20.1
WARNING: You are using pip version 22.0.3; however, version 22.3.1 is available.
You should consider upgrading via the '/home/appuser/venv/bin/python -m pip install --upgrade pip' command.
────────────────────────────────────────────────────────────────────────────────────────[10:51:17] 🐍
Python dependencies were installed from /app/tvs_credit_app/requirements.txt using pip.[10:51:17] 📦 Processed dependencies! Stopping...Collecting usage statistics. To deactivate, set browser.gatherUsageStats to False.[10:51:21] 🔄 Updated app!2022-12-31 10:51:29.902 Uncaught app exceptionTraceback (most recent call last): File "/home/appuser/venv/lib/python3.9/site-packages/soundfile.py", line 151, in <module> raise OSError('sndfile library not found')OSError: sndfile library not foundDuring handling of the above exception, another exception occurred:Traceback (most recent call last): File "/home/appuser/venv/lib/python3.9/site-packages/soundfile.py", line 178, in <module> _snd = _ffi.dlopen(_os.path.join(_path, '_soundfile_data', _packaged_libname))OSError: cannot load library '/home/appuser/venv/lib/python3.9/site-packages/_soundfile_data/libsndfile.so': /home/appuser/venv/lib/python3.9/site-packages/_soundfile_data/libsndfile.so: cannot open shared object file: No such file or directoryDuring handling of the above exception, another exception occurred:
There are two Errors : One is about protoBuff Version and the other is that the system is not able to identify the sndfile (might be used in librosa)
The requirements.txt file is :
librosa
matplotlib
numpy
pandas
Pillow
scipy
seaborn
streamlit
tensorflow
The app.py file looks like :
#Importing All Required Dependencies
import streamlit as st
from explore_page import show_explore_page
from predict_page import show_predict_page
page = st.sidebar.selectbox("Pages", ("Vehicle Price Estimator", "Explore More"))
if page=="Vehicle Price Estimator":
show_predict_page()
else:
show_explore_page()
The predict page is :
#Importing All Required Dependencies
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import scipy
from scipy import io, misc
import librosa
import tensorflow as tf
# import keras
from scipy.io.wavfile import read
from PIL import Image
import cv2
import pickle
#st.cache
def load_models():
model = tf.keras.models.load_model('audio_analysis_model.hdf5')
model_image = tf.keras.models.load_model('image_analysis_model.hdf5')
with open('saved', 'rb') as file:
data=pickle.load(file)
return (model,model_image,data)
models=load_models()
def show_predict_page():
st.title("Vehicle Price Estimator")
# MODEL 1 - Takes an Audio Input and Classify whether the defect present is Inner defect, Outer defect or Roller defect
uploaded_file = st.file_uploader("1. Upload an audio File of the Engine:")
if uploaded_file is not None:
# To read file as bytes:
bytes_data = uploaded_file.getvalue()
st.audio(bytes_data, format='audio/ogg')
# For reference, example.wav file is provided here
frequency = 10000
def feature_extract(l):
y = np.array(l)
sr = frequency
mfcc = librosa.feature.mfcc(y=y, sr=sr)
mfcc_scaled = np.mean(mfcc.T, axis=0)
return mfcc_scaled
a = read(uploaded_file)
audio= np.array(a[1],dtype=float)
test_dummy = feature_extract(audio)
test_dummy = test_dummy.reshape(1, -1)
model=models[0]
ans = model.predict(test_dummy)
# predicted class is stored in predicted_class
predicted_class = np.argmax(ans, axis=-1)
# Stores the parameter implying the defect detected through audio
audio_defect=0.0
if predicted_class==0:
st.subheader('No Defect Detected!')
elif predicted_class==1:
st.subheader('Inner Defect Detected!')
audio_defect=0.2
elif predicted_class==2:
st.subheader('Roller Defect Detected!')
audio_defect=0.2
else:
st.subheader('Outer Defect Detected!')
audio_defect=0.2
# MODEL 2 - Takes an Image Input and Classify whether the defect present is Minor, Moderate or Severe
uploaded_file2 = st.file_uploader("2. Upload an Image File of the Vehicle:")
if uploaded_file2 is not None:
# Reading the Image
image = Image.open(uploaded_file2)
st.image(image, caption='Car Image')
pic = Image.open(uploaded_file2)
# pix here denotes the resized image array
pix = np.asarray(pic)
pix = cv2.resize(pix, (80, 80))
im_data=np.array(pix).reshape(80,80,3)
im_data=im_data/255.0
# model_image is the image analysis model
model_image=models[1]
ans_im=model_image.predict(im_data.reshape(1,80,80,3))
pred_class_img = np.argmax(ans_im, axis=-1)
# parameter indicating the defect int the image
image_defect = 0.0
if pred_class_img == 0:
st.subheader('Minor Damage Detected!')
image_defect=0.2
elif pred_class_img == 1:
st.subheader('Moderate Damage Detected!')
image_defect= 0.3
elif pred_class_img == 2:
st.subheader('Severe Damage Detected!')
image_defect= 0.4
# The Same Logic can be easilt implemented for a Video Data as a Video is just several Frames of Images together
uploaded_file3 = st.file_uploader("OR, Upload an Video File of the Vehicle:")
if uploaded_file3 is not None:
video_bytes = uploaded_file3.read()
st.video(video_bytes)
# MODEL 3 = Decision Tree Regression Model to do Data Analysis as it gave the best result among all
# Designing all User Input Fields
# data stores 4 items:
# 1. A Data Analysis Model (MODEL 3)
# 2. A Standard Scaler as the input values can be of very different Ranges
# 3. A HashMap/Dictionary containing Model Name of Vehicle as keys and New Price of Vehicle as values
# 4. A List of Names of Vehicles
data=models[2]
model = data["model"]
scaling = data["scaling"]
priceMp = data["priceMap"]
model_names = data["names"]
# Taking User Inputs
col1, col2 = st.columns(2)
with col1:
name = st.selectbox( '3. Select Model of the Vehicle:', model_names)
fuel = st.selectbox(
'5. Select Fuel Type:',
('Petrol', 'Diesel', 'CNG'))
mileage = st.number_input('7. Enter Mileage in kmpl:')
with col2:
location = st.selectbox(
'4. Select nearest Location:',
('Chennai', 'Mumbai', 'Kochi', 'Delhi', 'Coimbatore','Kolkata', 'Jaipur', 'Ahmedabad', 'Hyderabad', 'Pune','Bangalore'))
owners = st.selectbox(
'6. Select Number of Owners:',
('1','2','3'))
cc = st.number_input('8. Enter CC of the Engine:')
years = st.slider('9. Numbers of years used: ', 0, 50, 1)
kms = st.number_input('10. Kilometers Driven:')
trans = st.selectbox(
'11. Select Transmission Type:',
('Manual', 'Automatic'))
# Converting Transmission Type, Location, Fuel into Integer Values
trans_val=0
if trans=='Manual' :
trans_val=0
else:
trans_val=1
new_price=priceMp[name]
dict_cities = { 'Kochi':0,'Mumbai':1,'Coimbatore':2, 'Hyderabad':3, 'Pune':4, 'Kolkata':5, 'Delhi':6, 'Chennai': 7, 'Jaipur':8, 'Ahmedabad':9, 'Bangalore':10 }
dict_fuel= {'Petrol':0,'Diesel':1,'CNG':2}
loc_val = dict_cities[location]
fuel_val = dict_fuel[fuel]
# Scaling the Values and Testing the trained Model with it
inp = np.array([[ loc_val, kms, fuel_val, trans_val, mileage, cc, new_price, years, owners ]])
df2 = pd.DataFrame(inp, columns = ['Location','Kilometers_Driven', 'Fuel_Type','Transmission','Mileage', 'Engine', 'New_Price', 'Years', 'Owners'])
transformed_data= scaling.transform(df2)
inp=np.array(transformed_data)
df2 = pd.DataFrame(inp, columns = ['Location','Kilometers_Driven', 'Fuel_Type','Transmission','Mileage', 'Engine', 'New_Price', 'Years', 'Owners'])
outp = model.predict(df2)
# Predicting the Final Price using all the outputs of the models
if st.button('GET ESTIMATED PRICE'):
ans=float(outp)
ans = ans - (ans*audio_defect)-(ans*image_defect)
if ans<=0.0:
ans=2.2
st.subheader(f"The Predicted Price is: Rs. {'%.2f'%ans} Lakhs.")
else:
st.write('')
I tried adding versions in requirements.txt file but the errors remained. Then I got the reqirements.txt file using pipreqs but still it gives error. I think the requirements.txt file needs some modifications as the app is working well locally.
THe entire code link :
https://github.com/bikramghosh-ux/TVS_CREDIT_APP

Can't resize an image properly when inserting to powerpoint using pptx python - Placeholder

I have created a PowerPoint master slide and I have a template where I'd like to replace the logo through code based on the client.
The thing is that whenever I insert the image through code, the image gets resized but does not respect the correct ratio, so it gets distorted in some cases. I have found a good answer from #scanny in this link here, which helped me a bit but not fully since I can now upload the images, but they still get distorted.
Here is my code
import collections.abc
from datetime import datetime
from logging import PlaceHolder
from pptx import Presentation
from pptx.util import *
from PIL import Image
import win32com.client as win32
import os
TEXT_PLACEHOLDER_TYPE = 2
OBJECT_PLACEHOLDER_TYPE = 7
PICTURE_PLACEHOLDER_TYPE = 18
# ------------- FUNCTIONS -------------
def _add_image(slide, placeholder_id, image_url):
placeholder = slide.placeholders[placeholder_id]
# Calculate the image size of the image
im = Image.open(image_url)
width, height = im.size
# Make sure the placeholder doesn't zoom in
placeholder.height = height
placeholder.width = width
# Insert the picture
placeholder = placeholder.insert_picture(image_url)
available_width = placeholder.width
available_height = placeholder.height
image_width, image_height = placeholder.image.size
placeholder_aspect_ratio = float(available_width) / float(available_height)
image_aspect_ratio = float(image_width) / float(image_height)
if placeholder_aspect_ratio > image_aspect_ratio:
available_width = int(image_aspect_ratio * available_height)
# ---otherwise shrink the height
else:
available_height = int(available_width/image_aspect_ratio)**
# ----------- PROGRAM START -----------
print("\n----PROGRAM STARTS HERE ----\n")
result_ppt = Presentation("data/TEMPLATE.pptx")
slide = result_ppt.slides[0]
for _ph in slide.placeholders:
print(f"NAME: {_ph.name}\n"
f"PLACEHOLDER TYPE: {_ph.placeholder_format.type}\n"
f"PLACEHODER ID: {_ph.placeholder_format.idx}\n"
f"ID: {_ph.shape_id}\n"
f"TYPE: {_ph.shape_type}\n")
# Handles IMAGE - Replacing company logo
if _ph.placeholder_format.type == PICTURE_PLACEHOLDER_TYPE:
# Makes sure we are only changing logos tagged with client_logo id
if _ph.name == "client_logo":
_add_image(slide=slide, placeholder_id=_ph.placeholder_format.idx, image_url="data/client_logo.png")
result_ppt.save("data/new_template.pptx")
And there it is! I can't make my images be not distorted no matter what I do.
One last thing is that inside the function that handles that I changed the
placeholder.width = int(image_aspect_ratio * available_height)
available_width = int(image_aspect_ratio * available_height)
Because for some reason using the placeholder.width was placing the images squished to the corner so I couldn't see it.

generating video using moviepy using image and text but getting ERROR like size = clips[0].size AttributeError: 'list' object has no attribute 'size'

import glob
import os
from natsort import natsorted
from moviepy.editor import *
base_dir = os.path.realpath("./images/")
print(base_dir)
gif_name = 'pic'
fps = 24
file_list = glob.glob('./images/*.jpg') # Get all the pngs in the current directory
file_list_sorted = natsorted(file_list,reverse=False) # Sort the images
clips = [ImageClip(m).set_duration(5)
for m in file_list_sorted]
print (clips)
text_list = ["Piggy", "Kermit", "Gonzo", "Fozzie"]
clip_list = []
for text in text_list:
try:
txt_clip = ( TextClip(text, fontsize = 70, color = 'white').set_duration(2))
clip_list.append(txt_clip)
except UnicodeEncodeError:
txt_clip = TextClip("Issue with text", fontsize = 70, color = 'white').set_duration(2)
clip_list.append(txt_clip)
print(clip_list)
final_clip = CompositeVideoClip([clips, clip_list])
final_clip.write_videofile("./video/export.mp4", fps = 24, codec = 'mpeg4')
But getting ERROR like size = clips[0].size
AttributeError: 'list' object has no attribute 'size'
i need to display image for first 15sec and after 15 sec text should be display 10 sec.
Hi thanks for posting the question. Hopefully this would be helpful.
Your main mistake concerns the following line:
final_clip = CompositeVideoClip([clips, clip_list])
CompositeVideoClip.__init__ expects a list of type Clip (CompositeVideoClip inherits the Clip class). However, in your previous lines:
print(clips)
...
print(clip_list)
so what you have effectively fed to the CompositeVideoClip.__init__ method is a nested list. So instead, you should do something like the following:
final_clip = CompositeVideoClip(clips + clip_list)
I'm sure you can come up with a more elegant solution, this is a working solution for you to start with. Do write further in case my proposal does not work.

How do I use a csv data as variables to apply it for a formula?

I'm trying to use data from a csv file ( https://www.kaggle.com/jingbinxu/sample-of-car-data ). I only need the horsepower and weight columns as variables for the equation: ( 1/4 mile et = 6.290 * (weight/hp) ** .33 ), but it won't apply it. I don't know if the storage is working or I shouldn't do it as a class. When I run the program it doesn't show any errors, but it doesn't show results either. Then I got to plot the results, but I don't think it's even calculating and storing results. Any help is appreciated. Thanks in advance.
Here's the current code i have:
import numpy as np
class car_race_analysis():
def __init__(self, filename):
import numpy as np
self.data = np.genfromtxt(filename,delimiter= ',', skip_header = 1 )
def race_stats(self,w,h):
#cars in data
cars = np.unique(self.data[:,0])
#storage for output
race_times = []
#for each car
for car in cars:
#mask
mask = self.data[:,0] == car
#get data
w = self.data[mask,12]
h = self.data[mask,18]
#apply formula
qrtr_mile = 6.290 * ( w / h ) ** .33
race_times.append(qrtr_mile)
#new atribute
self.race_times = np.array(race_times)
print(race_times)
def trend_plotter(self):
import matlib.pyplot as plt
#inputs
self.race_stats
cars = np.unique(self.data[:,0])
#plot
plt.plot(cars,self.race_times)
plt.xlabel("Car")
plt.ylabel("1/4 Mile Time")
plt.savefig("trend_plot.png")
filename = 'car_data.csv'
Two problems:
I think you meant matplotlib instead of matlib. Make sure you install it pip3 install matplotlib --user and edit your code accordingly.
Your previous code wasn't working because you weren't instantiating a class or running any methods. The only "work" your program did was to define the class and then set a filename variable.
To solve #2, replace your filename=... line with the code below.
Here's what it does:
It checks to see if the file is being run directly (i.e. from command prompt such as python3 <your_file_name>.py. If this class is being imported and used from a different python file, this code would not be executed. More reading: https://www.geeksforgeeks.org/what-does-the-if-name-main-do/
We instantiate a instance of your class and supply the filename variable since that it was your class' __init__ method expects.
We invoke the trend_plotter method on the instance of the class.
if __name__ == '__main__':
filename = 'car_data.csv'
car_analysis = car_race_analysis(filename)
car_analysis.trend_plotter()
Even with those changes, your program will not work because it has other errors. I made a guess at fixing it, which I've pasted below, but I strongly encourage you do diff my changes to understand what I altered to be sure it does what you want.
import numpy as np
import matplotlib.pyplot as plt
class car_race_analysis():
race_times = []
cars = []
def __init__(self, filename):
import numpy as np
self.data = np.genfromtxt(filename, delimiter=',', skip_header=1)
def race_stats(self, w, h):
#cars in data
self.cars = np.unique(self.data[:, 0])
# storage for output
self.race_times = []
# for each car
for car in self.cars:
# mask
mask = self.data[:, 0] == car
# get data
w = self.data[mask, 12]
h = self.data[mask, 18]
# apply formula
qrtr_mile = 6.290 * (w / h) ** .33
self.race_times.append(qrtr_mile)
# new atribute
self.race_times = np.array(self.race_times)
def trend_plotter(self):
# inputs
self.race_stats(len(self.cars), len(self.race_times))
# plot
plt.plot(self.cars, self.race_times)
plt.xlabel("Car")
plt.ylabel("1/4 Mile Time")
plt.savefig("trend_plot.png")
plt.show()
if __name__ == '__main__':
filename = 'car_data.csv'
car_analysis = car_race_analysis(filename)
car_analysis.trend_plotter()

How to get DPI value of an image in Python/Django

I am trying to extract the Dpi value of an image Using python in one of my django powered web application.I am using following function to achieve my desired output that is the Dpi value of an image but i am facing an exception.
This is the Function to get DPI value of an Image
def get_exif_data(fname):
"""Get embedded EXIF data from image file."""
ret = {}
try:
img = Image.open(fname)
if hasattr( img, '_getexif' ):
exifinfo = img._getexif()
if exifinfo != None:
for tag, value in exifinfo.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
except IOError:
print 'IOERROR ' + fname
return ret
and this is the view where i have used that above function to get the DPI value of an Image.
def get_dpi(request,image_id):
image = get_object_or_404(Photo,pk = image_id)
img = Image.open(image.photo)
dpi_info = get_exif_data(img)
context = RequestContext(request)
ctx = {'dpi':dpi_info}
return render_to_response('photo/download_image.html',ctx,context)
but i am facing the following exception
To begin, I have to ask why you want the DPI resolution. It's just a tag and doesn't really mean anything unless you are outputing to physical media. A 1000x1000 pixel image can be 10x10 at 100dpi or 100x100 at 10dpi, but it's still exactly the same image. Exactly the same pixels. It's hard to imagine scenarios where img.size doesn't give you everything you need.
Having said that, if you want to get the exif tags for resolution try XResolution from PIL.ExifTags:
import Image
from PIL.ExifTags import TAGS
img = Image.open("path/to/.jpg")
info = img._getexif()
exifObj = {}
if info != None:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
exifObj[decoded] = value
exifObj now either empty or equals something like:
{'YResolution': (720000, 10000), 'BitsPerSample': (8, 8, 8), 'ImageLength': 713, 'Orientation': 1, 'Copyright': 'Mark Meyer Photography', 'ExifImageWidth': 950, 'ExifImageHeight': 713, 'ColorSpace': 1, 'ResolutionUnit': 2, 'DateTime': '2015:01:30 21:37:51', 'XResolution': (720000, 10000), 'ExifOffset': 296, 'PhotometricInterpretation': 2, 'ExifVersion': '0221', 'Artist': 'MarkM', 'ImageWidth': 950, 'SamplesPerPixel': 3, 'Software': 'Adobe Photoshop CC 2014 (Macintosh)'}
DPI is:
exifObj['XResolution'][0]/exifObj['XResolution'][1]
72DPI in this case.
It's not clear in your example how you are trying to access the DPI value for the context. You're getting an attribute error, so maybe in your template you are trying to access ctx.dpi or something similar which doesn't exist.
Almost I spend searching 3 hours for this. At last I find it out, how we can find dpi of an image or pdf using fitz library, you can download using this command pip install PyMuPDF and pip install fitz.
If you want to know more about this process you can check it out official documentation. If you found useful upvote it!
def dpi_finder(link : str) -> int:
doc = fitz.open(link) # opening a image or pdf.
page = doc.load_page(0) # getting first page
pix = page.get_pixmap() # getting the pixamp
return pix.xres # it will give the dpi for horizontal resolution

Categories

Resources