Skip to content

Instantly share code, notes, and snippets.

View panzi's full-sized avatar

Mathias Panzenböck panzi

View GitHub Profile
@panzi
panzi / smart_formatter.py
Last active February 28, 2024 22:32
Python argparse help formatter that keeps new lines and doesn't break words (so URLs are preserved), but still wraps lines. Use with: `argparse.ArgumentParser(formatter_class=SmartFormatter)`
import argparse
from typing import List
class SmartFormatter(argparse.HelpFormatter):
def _split_lines(self, text: str, width: int) -> List[str]:
lines: List[str] = []
for line_str in text.split('\n'):
line: List[str] = []
line_len = 0
for word in line_str.split():
@panzi
panzi / pil2cv.py
Created July 5, 2021 16:00
Python: Convert PIL.Image to OpenCV BGR(A)/grayscale image (NumPy array). This supports the most common image modes, but there are more! Patches for those are welcome.
from PIL import Image
import numpy as np
import cv2
def pil2cv(image: Image) -> np.ndarray:
mode = image.mode
new_image: np.ndarray
if mode == '1':
new_image = np.array(image, dtype=np.uint8)
@panzi
panzi / VideoScreenshot.js
Last active May 7, 2022 10:59
Make a screenshot of a video.
javascript:(function() {
function screenshot(video) {
var canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
context = canvas.getContext('2d');
var now = new Date();
context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
var url = canvas.toDataURL('image/png');
var link = document.createElement('a');
@panzi
panzi / enable_context_menu.js
Created July 22, 2020 21:17
Bookmarklet to re-enable context menus on sites that disable it.
javascript:(function(pd){Event.prototype.preventDefault=function(){if(this.type!=='contextmenu')return%20pd.apply(this,arguments);};})(Event.prototype.preventDefault);void(0)
(function() {
"use strict";
function padd(x) {
var x = String(x);
return x.length < 2 ? '0' + x : x;
}
var duration = +document.querySelector("video").duration;
var playerData = document.querySelector("ytd-watch-flexy").__data.playerData;
@panzi
panzi / mkgif.sh
Created October 2, 2019 22:49
convert a video into a GIF with optimal palette
#!/usr/bin/bash
input=/dev/stdin
output=/dev/stdout
slice=
palette="/tmp/mkgif-$$-palette.png"
fps=15
width=
crop=
@panzi
panzi / no_blocking_overlay.js
Last active November 28, 2021 03:00
A bookmarklet that removes these nasty overlays from websites that would work perfectly fine without a login, but still want you to create one. Create a new bookmark and paste the script source as the URL. (Use "Raw" to see only the script source for easy copy-paste.) When on a page with a blocking overlay click the bookmark.
javascript:(()=>{var d=document.documentElement,b=document.body,es=b.querySelectorAll("*"),m=20;for(var i=0;i<es.length;++i){var e=es[i];var s=getComputedStyle(e);if(s.position!=='static'&&s.position!=='relative'){var r=e.getBoundingClientRect();if(r.x<=m&&r.y<=m&&r.right>=window.innerWidth - m&&r.bottom>=window.innerHeight - m){e.remove();}}}var s='\n/**/;position:static!important;overflow:auto!important;width:auto!important;height:auto!important;';d.setAttribute('style',(d.getAttribute('style')||'')+ s);b.setAttribute('style',(b.getAttribute('style')||'')+ s);})();void(0)
@panzi
panzi / show_hide_passwords.js
Created January 19, 2019 19:04
Bookmarklets to make passwords in password fields readable and to hide them again.
// Show Passwords:
javascript:document.querySelectorAll('input[type=password]').forEach(e=>{e.type='text';e.setAttribute('data-io-github-panzi-password','true');});void(0)
// Hide Passwords again:
javascript:document.querySelectorAll('input[data-io-github-panzi-password=true]').forEach(e=>e.type='password');void(0)
@panzi
panzi / git-kmerge3.sh
Last active November 20, 2018 00:51
Merge files useing kdiff3. Using git mergetool merges files one by one, this opens all files in kdiff3 at once. TODO: somehow git add merged files after kdiff3 exists
#!/bin/bash
set -e
SUBDIRECTORY_OK=1
. "`git --exec-path`/git-sh-setup"
cd_to_toplevel
O=".git-kmerge3-tmp-$$"
list=$O/.git-kmerge3-list
@panzi
panzi / mysql_cvs_dump.py
Last active October 24, 2018 20:47
Dumps whole MySQL database as CSV files.
#!/usr/bin/env python3
import mysql.connector
import argparse
import csv
import re
import sys
from contextlib import closing
from os import makedirs
from os.path import join as join_path