Sway Random Wallpaper - raspberry-pi

I am trying to load a random image from a folder to wallpaper.
In the sway.d folder, I have a .conf file that has one line that goes,
output * bg '$HOME/.config/sway/random_bg' fill
The random_bg file has multiple lines of codes that go,
#!/usr/bin/python3
import glob
import random
import sys
args = sys.argv[1:]
if len(args) == 0:
args = [""]
walls = list()
for source in args:
walls += glob.glob("/home/jake/.config/sway/bg/" + source + "/*.*")
print(random.choice(walls))
I have made random_bg executable by tying "chmod +x random_bg."
I have confirmed that it indeed works when I typed ./random_bg, it prints out the path of random images in the bg folder.
The issue is when I refresh sway, the screen just puts out a brownish gray color.
I can't figure out where the issue is.

Related

FTP stops working at consistent, but unexplained intervals? (Matlab)

The Problem
I'm using the (simplified) code below to get information about a repo I have on box.com.
The script should:
access my remote repo via FTP
get a list of all the directories in the repo
iterate through each directory in the list, where in each iteration it:
3a) enters the directory and gets some information about the files there
3b) stores the information in an array
3c) goes back to the root with "../"
I found the following works for about half the directories in my repo (70 of 134):
ftpobj = ftp("ftp.box.com","myUname","myPassword","TLSMode","strict");
% Get dir list
dirList = dir(ftpobj);
numDirs = size(dirList,1);
% Setup out Array
clearvars outArray
outArray = ["directory" "numFiles"];
for i = 1:numDirs
% Select and Move to subfolder
folder = dirList(i,1).name;
cd(ftpobj, folder);
% Get a filelist for dir
files = dir(ftpobj);
numFiles = size(files,1);
% Determine Output and add to OutArray
outLine = [folder numFiles];
outArray = [outArray; outLine];
disp(i + " of " + numDirs + " done");
cd(ftpobj, "../"); % Move back to Root
end
But it drops out around halfway through on the cd(ftpobj,"../"); line, saying:
"ftp://ftp.box.com//2021-11-04/../" is nonexistent or not a directory.
I've Tried:
If I run the cd(ftpobj,"../"); command again in the terminal, it works fine. I can manually increase i and go by step by step again no problem - just not in the loop.
I've tried adding in a try catch over the whole loop, but it still stops working when it gets past 2021-11-04 (dir 71)!
I tried adding in an if statement to the code which skips out the problem directory (i==71), and it instead just tripped up on the next one (dir 72). I then tried changing the for statement to for i = 72:numDirs, without clearing the workspace, and it tripped out again.
A possible solution?
I cleared the workspace completely and ran the whole code again but with for i = 72:numDirs and it carried on perfectly to the end. So I guess I can run this loop in two halves but that seems hacky?
Could it be a bandwidth limit/ structure limit or something? I haven't been able to find anything about that though?
I managed to find a reasonable work-around. All I've done is reset the ftpobj every 30 queries by putting the following in my loop. It's gone through 4 repos and analysed over 700 directories so far without issue so while a bit weird (and possibly slower?) I think it's a viable solution!
if mod(i,30) == 0 % reset the ftp every 30
disp("resetting FTP object at "+ i);
ftpobj = ftp("ftp.box.com","myUname","myPassword","TLSMode","strict");
end

How do I upload data points for NACA airfoil into simflow to run cfd simulation?

I have a text file filled with points that consist of x, y, and z coordinates of a NACA airfoil. I want to ultimately run a cfd simulation over the wing at different angles of attack using software called simflow. How do I import this data into the software so I can run my simulation?
I'm assuming that your data is delimited and doesn't contain any headers e.g.
1.002 -0.001 -0.002
0.986 0.246 0.234
.
.
1.200 0.897 0.672
Assuming you are on a UNIX or UNIX-like system with Python installed, save the following piece of code as a new file called 'convert_to_obj.py'. This script converts an airfoil .dat file into an '.OBJ' file which can be manually imported into SimFlow before meshing.
#!/usr/bin/env python
'''
Script to convert a NACA airfoil in the Selig format into an OBJ file
which can be read by SimFlow.
'''
# Make sure that your airfoil .dat file has the '.dat' extension!!!!
# Argument parser from the command line
import argparse
parser = argparse.ArgumentParser( description='Script to convert a NACA'
'airfoil in the Selig format into an'
' OBJ file that can be read by SimFlow')
parser.add_argument('files', type=str,
help='Choose files to convert, specify path.'
' Enclose entire path in single-quotes')
args = parser.parse_args()
# Import and convert file
import glob
AllFiles = glob.glob( args.files)
for datfile in AllFiles:
InFileID = open( datfile, 'r')
# If you have header files in the .dat file, specify the number of
# header lines below. (for Selig, numheader = 1)
numheader = 0
for i in range(numheader):
InFileID.readline()
# Slurp all the remaining lines of the .dat file and close it
DataLines = InFileID.readlines()
InFileID.close()
# Open a new file to write to (change extension)
OutFileID = open( datfile[:-4] + '.OBJ', 'w')
# Write the name of the group (in our case, its just 'Airfoil')
OutFileID.write('g Airfoil\n')
for line in DataLines:
OutFileID.write('v ' + line )
OutFileID.close()
# This should create an .OBJ file that can be imported into SimFlow.
To run this script do (from the terminal/command line)
$ python convert_to_obj.py './relative/path/to/airfoil.dat'
Or you can convert a bunch of files at a time
$ python convert_to_obj.py './relative/path/to/airfoil*.dat'
Note that the above script will create vertices. I'm also a bit confused as to why your airfoil has 3 coordinates. Airfoils are two-dimensional. This script will work for 3d data, but will only create vertices.

iPython notebook not loading: too much output

I have an iPython notebook file which is not loading, presumably because there is too much output in the file (thousands of lines of results printed, old computer).
I can edit the file with notepad without problems, but copying and then cleaning the code from there cell by cell is very time-consuming.
Is there a way to recover the code differently, or to ask iPython notebook to only load the code and not print all the past outputs when opening the file?
Here is an output removal script I found on Github. Credits to the author.
import sys
import io
from IPython.nbformat import current
def remove_outputs(nb):
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type == 'code':
cell.outputs = []`
if __name__ == '__main__':
fname = sys.argv[1]
with io.open(fname, 'r') as f:
nb = current.read(f, 'json')
remove_outputs(nb)
print current.writes(nb, 'json')

Read images from folders in directory

1) I have an original directory called "Original_file" that contains several number of images. The code below serves to read those images from the directory, converts them to greyscale, then write them into new directory called "Target_File".
Target_File='modified_images';
mkdir(Target_File);
directory='original_images';
dnames = {directory};
cI = cell(1,1);
c{1} = dir(dnames{1});
cI{1} = cell(length(c{1}),1);
for j = 1:length(c{1}),
cI{1}{j} = double(imread([dnames{1} '/' c{1}(j).name]))./255;
cI{1}{j} = rgb2gray(cI{1}{j});
imwrite(cI{1}{j}, fullfile(Target_File, ['image' num2str(j) '.jpg']));
end
2) From the "Target_File": The code below serves to select randomly a specific number of images and put them in a training file.
Train_images='training_file';
mkdir(Train_images);
ImageFiles = dir('Target_File');
totalNumberOfFiles = length(ImageFiles)-1;
scrambledList = randperm(totalNumberOfFiles);
numberIWantToUse = 5; % for example 5
loop_counter = 1;
for index = scrambledList(1 :numberIWantToUse)
baseFileName = ImageFiles(index).name
str = fullfile('Target_File', baseFileName);
image = imread(str);
imwrite( image, fullfile(Train_images, ['image' num2str(index) '.jpg']));
loop_counter = loop_counter + 1;
end
What I want in this question ?
A) If we consider that we have a directory that contains several number of folders (folder1, folder2, ...., foldern). Each of these folders contains several images. So how can I edit my code in 1) in order to apply the same concept and get a new directory "Target_File" that contains the same number of folders, but each folder becomes containing the greyscale images?
Then, from the Target_File created in A) : I want to select (randomly as in 2)) from each folder in Target_File, a specific number of images and put them in training file, and the remaining images in testing file. This procedure is repeated for all folders in the directory.
So if the directory contains 3 folders, each of these folders is split into training and test files. So the first folder is split into train1 and test1, the second directory into train2 and test2, the third directory into train3 and test3, etc. So how to edit my code in 2) ?
Any help will be very appreciated.
You can use the dir command to get a list of sub-directories, and then loop through that list with calls to mkdir to create each one in turn. After that, it is just a matter of matching the file paths so you can save the greyscale image loaded from a source subfolder to its corresponding target folder.
Specifically, D = dir('directory') will return a struct where each element of the structure is an element stored in 'directory'. D(i).isdir will be 1 if D(i).name corresponds to the name of one of your subfolders (note that you will need to ignore D(1:2), as those are the folder navigation tags . and ..). So, get your list of directory contents, and then loop through those calling mkdir if D(i).isdir is 1.
I am not sure I understand the rest of your question, but if you just need a random subsample of the entire image set (regardless of subfolder it is stored in), while you are making your subfolders above you can also make secondary calls of dir to the subfolders to get a list of their contents. Loop through and check whether each element is an image, and if it is save it to an array of image path names. When you have compiled this master list, you can grab a random subset from it.

Seperate and process odd named text files from an even named text files in Python

I am new to programming & python and is trying to write a program to process astronomical data.I have a huge list of files naming like ww_12m_no0021.spc, ww_12m_no0022.spc and so on. I want to move all the odd numbered files and even numbered files in two seperate folders.
import shutil
import os
for file in os.listdir("/Users/asifrasha/Desktop/python_test/input"):
if os.path.splitext(file) [1] == ".spc":
print file
shutil.copy(file, os.path.join("/Users/asifrasha/Desktop/python_test/output",file))
which is actually copying all the spc file to a different folder. I am struggling a bit on how I can only copy the odd number files (no0021, no0023…) to a seperate folder. Any help or suggestions will be much appreciated!!!
import os
import shutil
# Modify these to your need
odd_dir = "/Users/asifrasha/Desktop/python_test/output/odd"
even_dir = "/Users/asifrasha/Desktop/python_test/output/even"
for filename in os.listdir("/Users/asifrasha/Desktop/python_test/input"):
basename, extenstion = os.path.splitext(filename)
if extenstion == ".spc":
num = basename[-4:] # Get the numbers (i.e. the last 4 characters)
num = int(num, 10) # Convert to int (base 10)
if num % 2: # Odd
dest_dir = odd_dir
else: # Even
dest_dir = even_dir
dest = os.path.join(dest_dir, filename)
shutil.copy(filename, dest)
Obviously you can simplify it a bit; I'm just trying to be as clear as possible.
Assuming your files are named ww_12m_no followed by the number:
if int(os.splitext(file)[0][9:])%2==1:
#file is oddly numbered, go ahead and copy...
If the length of the first half of the name changes, I would use regex... I didn't test the code, but that's the gist of it. I'm not sure this question belongs here though...