Skip to content

Instantly share code, notes, and snippets.

Additional necessary packages for setting up Waydroid on Ubuntu docker image

apt-get install dbus-x11 kmod weston

Bridge a system-level systemd target to the user instance

  1. Sets up user linger and units: The script enables linger for the current user so their user-level systemd instance can run without an active login, and installs the user-network-online@.service unit at the system level plus a network-online.target marker unit in the user-level systemd directory.

  2. Bridges system network-online to user instance: The user-network-online@.service runs as a system service after network-online.target, and uses systemctl --user start network-online.target (with the appropriate XDG_RUNTIME_DIR) to trigger a corresponding network-online.target in the user’s systemd instance.

  3. Enables automatic startup on boot: By enabling user-network-online@<UID>.service with systemctl enable --now, the system ensures that whenever the system reaches multi-user.target and the network is online, the user-level network-online.target is started automatically for that user.

Build cuda-samples v11.8 with NVIDIA HPC SDK

module load nvhpc-hpcx-cuda11
CUDA_PATH="$NVHPC_ROOT" SMS=61 make -kj$(nproc)

jbig2enc dependencies

# Alpine Linux
apk add leptonica-dev leptonica build-base libtool autoconf automake

# MSYS2 ucrt64
pacman -S mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-make mingw-w64-ucrt-x86_64-libtool autoconf automake

Rebuilding and Recompressing a PDF with pdfimages, jbig2enc, jbig2topdf.py, and a Python Image-Replacement Script

This article explains a full workflow to:

  1. Extract images from an existing PDF (PDF A) using pdfimages from the Poppler suite,
  2. Convert all extracted images to JBIG2 format using jbig2enc,
  3. Assemble those JBIG2 images into a temporary JBIG2-only PDF (PDF B) with jbig2topdf.py, and
  4. Replace all raster images in the original PDF A with the JBIG2 images from PDF B using a Python script based on pypdf.

The end result is an output PDF that keeps the original structure of PDF A (bookmarks, annotations, vector graphics, etc.) while replacing its raster images with JBIG2-compressed images taken from PDF B, giving you both smaller size and better preservation of the original PDF’s logical structure.

Interactive Color Picker with Visual History Preview

  1. Color selection and storage: Users choose a color via an <input type="color"> control and click “Add” to append that color to a history list.
  2. Rich visual samples per history item: Each history entry displays a 50×50 color box, text styled in the color, and three line samples (1px, 3px, 5px) to preview how the color looks in different uses.
  3. Clickable history for re-selection: Clicking any history item sets the picker back to that color, enabling quick reuse and comparison of previously selected colors.

PDF Card Tiling Tool Using pypdf

  • Reads the first page of an input PDF, optionally resizes it to a specified physical size, adds a configurable light gray border, and converts it into a “card” page while preserving aspect ratio.
  • Computes the optimal tiling of the card on an A4 sheet (portrait or landscape) without further scaling, then generates a single tiled output page containing a grid of card copies.
  • Provides flexible CLI options for source and card dimensions in millimeters, plus a --no-resize-card flag to bypass card scaling, all implemented purely with pypdf (no external PDF engines).

PDF Page Splitter with Configurable Grid Using pypdf

  1. Configurable Grid Splitting via CLI Options

    • Uses argparse to accept an input PDF path, output PDF path, and optional --cols and --rows arguments, allowing the user to define how many horizontal and vertical splits to apply (default: 2 columns × 4 rows).
  2. Precise Page Cropping Based on Page Dimensions

    • Reads the first page of the input PDF, calculates the page width and height, and derives each tile’s size by dividing these dimensions by the specified number of columns and rows. It then computes crop coordinates for each grid cell.

A Simple File Upload Tool You Can Use from Your Smartphone

This script provides a practical template for a simple, smartphone-friendly file upload tool:

  • A clean HTML form and a small JavaScript snippet for previewing files.
  • Safe and robust server-side handling using aiohttp.
  • Automatic directory creation and filename conflict avoidance.
  • Easy access from mobile devices via a web browser.

If you need a lightweight way to upload photos or documents from your phone to your own server, this example is a solid starting point that you can extend with authentication, HTTPS, or additional features as needed.

Capturing Network Traffic with Playwright and Chrome

This sample code shows how to use Playwright and Chrome DevTools Protocol (CDP) in Python to capture network requests and responses from a real Chrome browser and save them as JSON.

The main flow is:

  1. Find a free TCP port The script uses socket to get an available local port for Chrome’s --remote-debugging-port.

  2. Launch Chrome with CDP enabled