Skip to content

Instantly share code, notes, and snippets.

@spajak
spajak / scan-magick.md
Created February 12, 2022 14:03
Repair Scanned Images Using ImageMagick

Improve/Repair Scanned Images Using ImageMagick

This is the command:

magick "img-01.tiff" -strip -kuwahara "1.5" -modulate "100,90" -level "6,92%,0.75" -quality 80 "img-01.jpg"
  • -strip - remove any metadata
  • -kuwahara "1.5" - apply smoothing algorithm
@spajak
spajak / vhd4wsl2.md
Last active October 1, 2023 05:09
Creating virtual hard disk (VHD) for WSL2

Creating additional virtual hard disk (VHDX) for WSL2 with ext4 filesystem

Lines starting with # mean the commands have to be executed by root user under Linux shell (WSL distro). All other commands have to be executed under Windows-PowerShell as Administrator.

Make VHDX disk file and mount it under Windows

New-VHD support.vhdx -SizeBytes 25GB -Dynamic -BlockSizeBytes 1MB
Write-Output "\\.\PhysicalDrive$((Mount-VHD -Path support.vhdx -PassThru | Get-Disk).Number)"
@spajak
spajak / make-epub.py
Created August 27, 2021 17:54
Make EPUB e-book file from a given directory. The directory must be a valid EPUB ebook source.
import sys
from pathlib import Path
from zipfile import ZipFile, ZIP_DEFLATED, ZIP_STORED
def main():
usage = f'{sys.argv[0]} source_dir [destination_file]'
if len(sys.argv) > 1:
src = Path(sys.argv[1])
dst = Path(sys.argv[2]) if len(sys.argv) > 2 else Path(src.name + ".epub")
validate(src, dst)
@spajak
spajak / .dir.php
Last active March 8, 2022 18:04
Lighttpd simple PHP example of directory listing with custom HTML/CSS template
<?php
/*
* Simple directory listing for a web server (lighttpd).
*
* Author: Sebastian Pająk
* Version: 0.0.1
* License: MIT
*
* Setup:
@spajak
spajak / 7zip-epub.md
Last active April 30, 2023 19:41
Make epub using 7zip command line utility

Make epub archive using 7zip command line utility in PowerShell.

$source = ".\ebook"
$epub = ".\ebook.epub"

$source = (Resolve-Path $source -ErrorAction Stop).Path
Move-Item "${source}\mimetype" ".mimetype" -ErrorAction Stop
Remove-Item "$epub" -ErrorAction Ignore
&amp; 7za a -mx0 "$epub" ".mimetype"
@spajak
spajak / _require.py
Last active March 8, 2022 17:08
Python's example function `require` that imports file by path (like in PHP or JavaScript), with custom module's name and without creating bytecode.
import sys
from pathlib import Path
import importlib
import runpy
def require(file, name=None):
if name is None:
name = Path(file).stem
anonymous = lambda cls: cls(name, str(Path(__file__).parent / file))
@anonymous
@spajak
spajak / rpi-temp.sh
Last active August 5, 2022 20:42
Raspberry Pi - get CPU and GPU temperature
#!/bin/bash
cpu=$(</sys/class/thermal/thermal_zone0/temp)
cpu=$(printf %.1f "${cpu}e-3")
gpu=$(vcgencmd measure_temp)
gpu=${gpu:5:-2}
echo "CPU: ${cpu} °C"
echo "GPU: ${gpu} °C"
@spajak
spajak / IPv6.ps1
Last active January 29, 2024 14:04
Disable random IPv6 addresses and use SLAAC IPv6 (Windows 10)
# Disable random IPv6 addresses (but keep temporary addresses),
# and use SLAAC address as a preferred IPv6 address in Windows 10
# ------------------------------------------------------------------------------
# UseTemporaryAddresses
#
# Always. The computer always generates temporary addresses by using random numbers.
# Counter. The computer generates temporary addresses by using the interface identifier.
# You typically use this identifier for test purposes.
# Disabled. The computer does not use temporary addresses.
@spajak
spajak / png2ico.ps1
Last active January 3, 2023 06:54
Simple script to convert PNG image to Windows ICO format using ImageMagick
param(
[String] $png
)
$root = Split-Path "$png" -Parent
$name = Split-Path "$png" -LeafBase
$icon = Join-Path $root ($name + ".ico")
& magick "$png" -strip -background none `
-gravity center -extent "%[fx:max(w,h)]x%[fx:max(w,h)]" `
@spajak
spajak / build-epub.ps1
Last active April 30, 2023 19:42
Creates EPUB e-book package from a given directory containing ebook source files.
[CmdletBinding(DefaultParameterSetName = "Default")]
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[Alias("i", "Path", "Source")]
[String] $SourceDirectory,
[Parameter(Mandatory=$true, ParameterSetName="DestinationAlongSource")]
[Alias("ds", "os")]
[Switch] $DestinationAlongSource,