Skip to content

Instantly share code, notes, and snippets.

View brunomsantiago's full-sized avatar

Bruno Santiago brunomsantiago

  • Brazil
View GitHub Profile
# Imports to download the wav file. Not necessary for local files.
from requests import get
from io import BytesIO
# Import to read the wav file.
from scipy.io import wavfile
# Import to plot the waveform.
import matplotlib.pyplot as plt
@brunomsantiago
brunomsantiago / video_to_thumb_mosaic.py
Created March 24, 2021 23:30
Making thumbnail mosaic from videos using Python and OpenCV
from pathlib import Path
import cv2
import numpy as np
def make_mosaic(cap, n_rows=4, n_cols=5):
n_frames = n_rows * n_cols
cap_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
floor = int(cap_frames*0.05)
ceil = int(cap_frames*0.95)
frames_indexes = np.linspace(floor, ceil, n_frames).astype(int)
@brunomsantiago
brunomsantiago / toggle_touchpad.py
Created March 25, 2021 23:40
Toogle (enable/disable) Thinkpad 440p touchpad on Linux
#!/home/bms/miniconda3/bin/python3
# This script toggles status (enabled/disabled) of my notebook touchpad. My
# notebook is a Thinkpad 440p and its touchpad is a Synaptics TM2722-001. The
# system is Linux Mint. I map it to a keyboard shortcut (alt+F11 in my case).
#
# I tried just using: xinput disable 14
# ... but the device id keeps changing after boot.
#
# I tried just using: xinput disable "Synaptics TM2722-001"
@brunomsantiago
brunomsantiago / human_readable_size.py
Last active April 6, 2021 19:37
human_readable_size
import math
def human_readable_size(size_bytes, base=1024, decimal_places=2):
suffixes = 'B KB MB GB TB PB EB ZB YB'.split(' ')
if size_bytes == 0:
return '0B'
magnitude_index = int(math.floor(math.log(size_bytes, base)))
magnitude_bytes = math.pow(base, magnitude_index)
size = size_bytes / magnitude_bytes
suffix = suffixes[magnitude_index]
@brunomsantiago
brunomsantiago / rename.py
Created April 8, 2021 12:30
Batch rename (serialize) video files
from pathlib import Path
# Input parameters
folders = ['/my/video/folder1', '/my/video/folder2']
video_file_types = 'avi flv m4v mkv mov mp4 mpg wmv'.split(' ')
# Folders as pathlib objects
folders = [Path(folder) for folder in folders if Path(folder).is_dir()]
# List and sort all files from all folders
@brunomsantiago
brunomsantiago / search_df.py
Created April 28, 2021 22:49
String search on pandas dataframe
import numpy as np
import pandas as pd
from random import choice, randint
names = ['David', 'James', 'John', 'Michael', 'Richard', 'Robert', 'William']
surnames = ['Davis', 'Jones', 'Lee', 'Miller', 'Moore', 'Smith', 'Taylor']
colors = ['Black', 'Blue', 'Green', 'Purple', 'Red', 'White', 'Yellow']
pets = ['', 'Dog', 'Cat', 'Bird', 'Fish', 'Bunny', 'Hamster', 'Guinea Pig']
def random_person():
from zipfile import ZipFile
def create_zip_with_fake_files(zip_filepath, fake_file_list):
with ZipFile(zip_filepath, 'w') as zip_archive:
for relative_filepath in fake_file_list:
zip_archive.writestr(relative_filepath, 'a')
def main():
fake_files = [
r'file0.txt',
from PIL import Image
def im_hstack(images):
min_height = min([im.height for im in images])
for im in images:
if im.height != min_height:
im.thumbnail((im.width, min_height))
total_width = sum([im.width for im in images])
panorama = Image.new('RGB', (total_width, min_height))
x_offset = 0
@brunomsantiago
brunomsantiago / app.py
Last active December 1, 2023 11:29
Streamlit keyboard shortcuts to buttons (tested on streamlit 1.4.0)
import streamlit as st
import streamlit.components.v1 as components
def left_callback():
st.write('Left button was clicked')
def right_callback():
st.write('Right button was clicked')
@brunomsantiago
brunomsantiago / term.ooo.py
Created February 1, 2022 04:16
Beating term.ooo game by brute force (version of wordly in portuguese)
from urllib.request import urlopen
import unicodedata
def remove_accents(input_str):
nfkd_form = unicodedata.normalize('NFKD', input_str)
return u"".join([c for c in nfkd_form if not unicodedata.combining(c)])
def has_letters(word, letters=''):