Is it possible to create mobile app in streamlit - python-3.x

I am trying to create mobile app in streamlit but I have no idea how to do that could someone help me to create streamlit mobile app by giving example
In mobile app we should take pictures and crop the same.

You will need to use some external libraries or frameworks that provide the necessary functionality for capturing and cropping images.
One option is to use the Python Pillow library, which provides functions for reading and manipulating image files. You can use the Image.open() function to open an image file, and the Image.crop() function to crop the image. You can then use Streamlit's st.image() function to display the cropped image in your app.
import streamlit as st
from PIL import Image
# Open an image file
image = Image.open("image.jpg")
# Crop the image
# The crop() function takes a tuple of (left, upper, right, lower) coordinates
# that define the bounding box for the cropped image.
cropped_image = image.crop((100, 100, 200, 200))
# Display the cropped image
st.image(cropped_image)

Related

Crop TIFF using JPG mask

I'm currently working on cloud removals from satellite data (I'm pretty new).
This is the image I'm working on (TIFF)
And this is the mask, where black pixels represent clouds (JPG)
I'm trying to remove the clouds from the TIFF, using the mask to identify the position of the cloud, and the cloudless image itself, like this (the area is the same, but the period is different):
I'm kindly ask how can I achieve that. A Python solution, with libraries like Rasterio or skimage is particularly appreciated.
Thanks in advance.
You can read the images with rasterio, PIL, OpenCV or tifffile, so I use OpenCV
import cv2
import numpy as np
# Load the 3 images
cloudy = cv2.imread('cloudy.png')
mask = cv2.imread('mask.jpg')
clear = cv2.imread('clear.png')
Then just use Numpy where() to choose whether you want the clear or cloudy image at each location according to the mask:
res = np.where(mask<128, clear, cloudy)
Note that if your mask was a single channel PNG rather than JPEG, or if it was read as greyscale like this:
mask = cv2.imread('mask.jpg', cv2.IMREAD_GRAYSCALE)
you would have to make it broadcastable to the 3 channels of the other two arrays by adding a new axis like this:
res = np.where(mask[...,np.newaxis]<128, clear, cloudy)

Problem in saving Python tkinter Canvas drawing as an image file using PIL Image and ImageDraw functions

Problem statement
I have written a code in Python on which a user can draw on a tkinter GUI canvas. The program is capable of taking the app's screenshot but saving the canvas content to a viewable image file (such as .jpg or .png) is not properly done yet.
Screenshots
What I draw on the tkinter app's canvas.
What the code (given below) saved.
What I expect.
I have tried and will expect
A number of possible solutions from the internet (including Stack Overflow) have been tried but somehow the process cannot be completed successfully. After drawing whatever on the canvas, the saved image will have nothing but a dot whether it is in *.jpg or *.png. I need the image of the full canvas with all the colorful drawings on it in that saved image file.
For any reference, here is my code
from tkinter import Tk, Canvas, Button
from PIL import Image, ImageDraw
X = 0
Y = 0
app = Tk()
def set_xy(click):
global X, Y
X, Y = click.x, click.y
def draw_line(drag):
global X, Y
draw_panel.create_line((X, Y, drag.x, drag.y), fill="black", capstyle="round", smooth=True)
draw.line([(X, Y), (drag.x, drag.y)], fill="black", joint="curve")
X, Y = drag.x, drag.y
def save(event=None):
# ATTENTION: This is not working properly
image.save("image.png", "png")
# App's drawing panel
draw_panel = Canvas(app, background="white", cursor="dot")
draw_panel.place(x=0, relwidth=1.0, y=0, relheight=1.0)
# Invisible canvas for saving as an image later on
image = Image.new("RGB", (draw_panel.winfo_width(), draw_panel.winfo_height()), "white")
draw = ImageDraw.Draw(image)
# Mouse and Key bindings
draw_panel.bind('<Button-1>', set_xy)
draw_panel.bind('<B1-Motion>', draw_line)
app.bind('<s>', save) # press 's' to save the drawing as .png
app.mainloop()
Addition Informations:
HOST: Windows 10 Version 20H2 (OS Build 19042.2130)
IDE: PyCharm 2022.2 (Community Edition) (Build #PC-222.3345.131)
Language: Python 3.10.6
Framework: tkinter Tk/Tcl 8.6
Library: pillow 9.2.0
Any solution shall be highly appreciated, and I shall be thankful.
That's a nice idea and there is just one piece missing. You set the size of the image to the size of the canvas before you call mainloop() for the first time. The canvas does not have a size at this time because the window has not been initiated, yet.
You need to force an update before getting the width and height. Then, it works fine:
# Invisible canvas for saving as an image later on
draw_panel.update()
image = Image.new("RGB", (draw_panel.winfo_width(), draw_panel.winfo_height()), "white")
draw = ImageDraw.Draw(image)
If you print the draw_panel.winfo_width() before and after calling the update, you see what I mean. Before update(), width and height are both 1. Afterwards, they are 200, as they should be.

Both rasterio open and skimage.io.read return a NaN array for the TIFF I am trying to open

I'm trying to open a SAR image from sentinel-1. I can view the tiff file in QGIS, so I know the data is there, but when I go to open and view/show it in python, all of the modules I could use to open the data produce a NaN area, basically insinuating that there is no data in the image. Visualizing the image produces a completely black image, however the shape is correct.
Here is the code where I read in the image:
img = skimage.io.imread('NewData.tif', as_gray = True, plugin = 'tifffile')
with rio.open(r'NewData.tif') as src:
img2 = src.read()
imgMeta = src.profile
print(img)
skimage.io.imshow(img)
Any help would be appreciated.
thank you
The problem is not on the way rasterio or skimage is importing the image, but on the way it is displayed. I am assumign you are working with Calibrated SAR images that ARE NOT converted to the decibel dB scale. Here is the problem, the dynamic range of your data.
The issue here is that by default, the color ramp is not being strech according to the distribution of values in the raster histogram. In QGIS, SNAP or many other EO-related softwares, the color distribution matches the histogram to produce proper visualizations.
Solution: either you make that happen in your code or simply convert your backscatter values to decibel (which is a very common procedure when working with SAR data and produces an almost normal distrubution of the data). The conversion can be done in a EO software or more directly in your imported image with:
srcdB = 10*np.log10(src)
Once done, you can properly display your image:
import rasterio
from rasterio.plot import show
import numpy as np
with rio.open(r'/.../S1B_IW_GRDH_1SDV_20190319T161451_20190319T161520_015425_01CE3C_A401_Cal.tif') as src:
img2 = src.read()
imgMeta = src.profile
srcdB = 10*np.log10(src) # to decibel
show(srcdB, cmap='gray') # show using rasterio

How to overlay images on each other in python and opencv?

I am trying to write images over each other. Ideally, what I want to do is to write every image in one folder over every image in another folder and output every unique image to another folder. So far, I am just working on having one image write over one image, but I can't seem to get that to work.
import numpy as np
import cv2
import matplotlib
def opencv_createsamples():
mask = ('resized_pos/2')
img = cv2.imread('neg/1')
new_img = img * (mask.astype(img.dtype))
cv2.imwrite('samp', new_img)
opencv_createsamples()
It would be helpful to have more information about your errors.
Something that stands out immediately is the lack of file type extensions. Your images are probably not being read correctly, to begin with. Also, image sizes would be a good thing to consider so you could resize as required.
If the goal is to blend images, considering the alpha channel is important. Here is a relevant question on StackOverflow:How to overlay images in python
Some other OpenCV docs that have helped me in the past: https://docs.opencv.org/trunk/d0/d86/tutorial_py_image_arithmetics.html
https://docs.opencv.org/3.1.0/d5/dc4/tutorial_adding_images.html
Hope this helps!

Holding the windows to display an HSI using spectral in Python

I am using spectral to view an hyperspectral image for a specific band in python. This is my code.
from spectral import *
img=open_image('flc1.lan')
view = imshow(img)
print(view)
gt=open_image('flc1.lan').read_band(0)
view1= imshow(classes=gt)
print(view1)
The image does pop up, but then closes. Is there any kind of function like waitkey to hold the window?
As an alternative you can use :
envi.save_image(give_file_name, image_name, ext='hdr')
to save the image locally and view it later, at your discretion

Resources