How to change the scale of json animation lottie in streamlit? - python

I'm using streamlit for visualization and wanted to add some animation. I find and install lottie. I liked one gradient that I would like to use as a separator between blocks, that is, it should be a small strip across the entire width of the screen. And with that I had a problem.
Sample code:
import streamlit as st
import requests
import json
from streamlit_lottie import st_lottie
from streamlit_lottie import st_lottie_spinner
def load_lottieurl(url: str):
""" Load animation and images from lottie"""
r = requests.get(url)
if r.status_code != 200:
return None
else:
animation = json.loads(r.text)
return animation
animation_1 = load_lottieurl('https://assets5.lottiefiles.com/packages/lf20_OXZeQi.json')
st_lottie(animation_1, speed=1, loop=True, quality="medium", width=1920)
Actually what's the problem, I can't scale the image without scaling it entirely. Is it possible to do this somehow, or somehow crop the displayed image vertically? Literally height in pixels 10) No matter how I twisted the settings, I can’t orient it correctly
That's what happens now, I just wanted it to be a small strip that would shimmer:

Related

Why are there Horizontal Stripes on my Palettized Image?

I am trying to make a palettized version of my height image data (using Python/Matplotlib) and for some reason...it is giving me quite weird horizontal lines which I know are not actually present in the dataset.
Both images (mine and the "better" one).
Is this something weird with how Matplotlib normalizes the data? I just don't quite understand how this could happen, so I am at a loss for where to start. I have provided my code below (sorry if there is a typo, I slightly changed it to make sense outside of the code).
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# file location of the raw data
fileloc = r'C:\Users\...\raw_height_profile.csv'
# generate height profile map
palettized_image = getheightprofile(fileloc)
def getheightprofile(fileloc, color_palette='jet'):
# read data from file
data = pd.read_csv(fileloc, skiprows=0)
# generate colormap (I'm using the jet colormap rn)
colormap = plt.get_cmap(color_palette)
# normalize the height data to the range [0, 1]
norm = (data - np.min(data)) / (np.max(data) - np.min(data))
# convert the height data to RGB values using the palette
palettized_data = (colormap(norm)*255).astype(np.uint8)
# save the file as a png (to check quality)
saveloc = r'C:\Users\...\palletized_height_profile.png'
plt.imsave(saveloc, palettized_data)
# return the nice numbers for later analysis
return palettized_data
But instead of returning the nice image that I think I should get, it returns a super weird image with lines across it. note: I know these images aren't quite the same palettization, but I think you can understand the issue.
Does anyone understand how, why, etc.? I have also attached a link to the dataset, because maybe that is helpful...but I am quite sure there is nothing wrong with the data.

Image not updating in python plot during animation

The Problem:
I'm trying to simulate a live video by cycling through a series of still images I have saved in a directory, but when I add the animation and update functions my plot is displayed empty.
Background on why I'm doing this:
I believe its important for me to do it this way rather than a complete change of approach, say turning the images into a video first then displaying that, because what I really want to test is the image analysis I will be adding and then overlaying on each frame. The final application will be receiving frames one by one from a camera and will need to do some processing, display the image + annotations + output the data as .csv etc... I'm simulating this for now because I do not have any of the hardware to generate the images and will not have it for several months during which time I need to get the image processing set up, but I do have access to some sets of stills that are approximately what will be produced. In case its relevant my simulation images are 1680x1220 and are 1.88 Mb TIFFs, though I could covert and compress them if needed, and in the final form the resolution will be a bit higher and probably the image format could be adjusted if needed.
What I have tried:
I followed an example to list all files in a folder, and an example
to update a plot. However, the plot displays blank when I run the
code.
I added a line to print the current file name, and I can see this
cycling as expected.
I also made sure the images will display in the plot if I just create
a plot and add one image, and they do. But, when combined with the
animation function the plot is blank and I'm not sure what I've done
wrong/failed to include.
I also tried adding a plt.pause() in the update, but again this
didn't work.
I increased the interval up to 2000 to give it more time, but that didn't work. I believe 2000 is extreme, I'm expecting it should work with more like 20-30fps. Going to 0.5fps tells me the code is wrong or incomplete, rather than it just being a question of needing time to read the image file.
I appreciate no one else has my images, but they are nothing special. I'm using 60 images but I guess it could be tested with any 2 random images and setting range(60) to range(2) instead?
The example I copied originally demonstrated the animation function by making a random array, and if I do that it will show a plot that updates with random squares as expected.
Replacing:
A = np.random.randn(10,10)
im.set_array(A)
...with my image instead...
im = cv2.imread(files[i],0)
...and the plot remains empty/blank. I get a window shown called "Figure1" (like when using the random array), but unlike with the array there is nothing in this window.
Full code:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import os
import cv2
def update(i):
im = cv2.imread(files[i],0)
print(files[i])
#plt.pause(0.1)
return im
path = 'C:\\Test Images\\'
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if '.TIFF' in file:
files.append(os.path.join(r, file))
ani = FuncAnimation(plt.gcf(), update, frames=range(60), interval=50, blit=False)
plt.show()
I'm a python and a programming novice so have relied on adjusting examples others have given online but I have only a simplistic understanding of how they are working and end up with a lot of trial and error on the syntax. I just can't figure out anything to make this one work though.
Cheers for any help!
The main reason nothing is showing up is because you never add the images to the plot. I've provided some code below to do what you want, be sure to look up anything you are curious about or don't understand!
import glob
import os
from matplotlib import animation
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
IMG_DIRPATH = 'C:\\Test Images\\' # the folder with your images (be careful about
# putting spaces in directory names!)
IMG_EXT = '.TIFF' # the file extension of your images
# Create a figure, and set to the desired size.
fig = plt.figure(figsize=[5, 5])
# Create axes for the current figure so that images can be sized appropriately.
# Passing in [0, 0, 1, 1] makes the axes fill the whole figure.
# frame_on=False means we won't have a bounding box, and setting xticks=[] and
# yticks=[] means that we won't have pesky tick marks along our image.
ax_props = {'frame_on': False, 'xticks': [], 'yticks': []}
ax = plt.axes([0, 0, 1, 1], **ax_props)
# Get all image filenames.
img_filepaths = glob.glob(os.path.join(IMG_DIRPATH, '*' + IMG_EXT))
def update_image(img_filepath):
# Remove all existing images on the axes, and restore our settings.
ax.clear()
ax.update(ax_props)
# Read the current image.
img = mpimg.imread(img_filepath)
# Add the current image to the plot axes.
ax.imshow(img)
anim = animation.FuncAnimation(fig, update_image, frames=img_filepaths, interval=250)
plt.show()

Plotly opens prompt screen after drawing plot

I use the following code to draw a heatmap in plotly:
import plotly.offline as plotly
import plotly.graph_objs as graph_objs
x = []
# fill x with stuff
path = os.path.join(self.get_current_job_directory(), track + '.html')
trace = graph_objs.Heatmap(z = x)
data = [trace]
plotly.plot(data, filename = path)
But I get a prompt screen like this. I need to generate hundreds of such plots on a remote server and its not practical to just dismiss them.
How to get rid of this?
Using the filename argument tells Plotly what filename to use for the HTML file it generates to contain the plot. That file is then viewed in the system's default HTML viewer, which in this case appears to be Lynx. Of course that's rather useless as the point is to view the plot, and Lynx is a text-only Web browser!
To avoid opening the plot, add auto_open=False to your plot() call:
plotly.plot(data, filename=path, auto_open=False)

Grabbing animated gif with python script

I've been playing around with Pythonista on iOS to create some automation scripts.
I have a problem where I'm trying to grab an animated gif from a remote url. I've come up with the following script.
import Image
from urllib import urlopen
from io import BytesIO
url = "http://someurl.com/funny.gif"
img = Image.open(BytesIO(urlopen(url).read()))
I get the image but it only appears to be the first frame of the gif? I'm guessing it has something to do with the BytesIO not reading in the whole file but I'm not sure?
Hope I'm along the right lines.
You're almost there. You use img.seek to advance frames. So..
import Image
from urllib import urlopen
from io import BytesIO
url = 'http://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif'
img = Image.open(BytesIO(urlopen(url).read()))
# Start with first frame
img.seek(0)
#img.show()
# Advance by one
img.seek(img.tell() + 1)
#img.show()
Here's a SO post showing how to save a gif using the Image class.
According to Pillow Manual:
To save all frames, the save_all parameter must be present and set to True.
So, opened image could be save by:
image.save('filename.gif', save_all=True)

Choosing how I apply an image to the desktop background(Center, Stretch, Fit, etc) in Python 2.7

This is my code to take the Astronomy picture of the day and automatically make it my background every 24 hours. In windows, when you make a background picture you can choose a variety of ways that it is applied. An example would be tiled, or forced to take up the whole screen. I need that ability and cant find anything online.
import ctypes
import urllib
import time
import os, sys
from bs4 import BeautifulSoup
while True:
try:
url = "http://apod.nasa.gov/apod/astropix.html"
page = BeautifulSoup(urllib.urlopen(url))
for image in page.findAll("img"):
print "Image: %(src)s" % image
parsed = "http://apod.nasa.gov/apod/"+"%(src)s" % image
x = urllib.urlretrieve(parsed)
ctypes.windll.user32.SystemParametersInfoA(20, 0,x[0], 0)
os.remove(x[0])
time.sleep(86400)
except:
continue

Categories

Resources