Skip to content

Instantly share code, notes, and snippets.

View kevinlinxc's full-sized avatar

Kevin Lin kevinlinxc

View GitHub Profile
@kevinlinxc
kevinlinxc / README.md
Last active April 27, 2025 22:42
Make your Python user scripts delightful with `prompt_toolkit`

Make your Python user scripts delightful with prompt_toolkit

When writing Python scripts, I ask for user input all the time. This input often has a fixed set of options, those options are usually not fully listed out to the user, the user has to type them out while making no typos, and then we have to do validation afterwards to make sure they picked something valid.

Recently I discovered the prompt_toolkit Python library, which fixes all this.

The library actually has lots of features, but the one that is relevant is this nice dropdown that lets you pick an item from pre-defined options. It saves time for the end-user and the script writer, and it'll delight anyone seeing it for the first time.

@kevinlinxc
kevinlinxc / dilation.py
Created April 26, 2025 07:33
Manim Morphological Dilation Animation
"""
Manim animation showing morphological dilation. Input image and kernel can be modified and the
output will still look good, although you may need to move/translate it to be viewable.
Mostly written by Claude, I needed to modify it a bit.
`manim -pqh dilation.py`
"""
from manim import *
import numpy as np
@kevinlinxc
kevinlinxc / keyboard_to_letter_contour.py
Created March 23, 2024 19:00
On key press, display a letter in the center of cv2 imshow, and get points on the contour of the letter
from PIL import Image, ImageDraw, ImageFont
import cv2
import numpy as np
def get_bounding_rect_area(contour):
# calculate area using the minimum bounding rectangle instead of whatever cv2.contourArea does
rect = cv2.minAreaRect(contour)
width = rect[1][0]
height = rect[1][1]
return width * height
@kevinlinxc
kevinlinxc / opencv_trackbars_example.py
Created March 15, 2024 00:33
opencv_trackbars_example.py
import cv2
import numpy as np
def nothing(x):
pass
# Create a black image, a window
img = np.zeros((300, 512, 3), np.uint8)
@kevinlinxc
kevinlinxc / serial_sender.py
Created February 4, 2024 04:00
Serial Sender
import serial
import serial.tools.list_ports
def get_ports():
"""
Get a list of available serial ports.
Returns:
List of serial ports, or None if no ports are available.
"""
serial_ports = serial.tools.list_ports.comports()
@kevinlinxc
kevinlinxc / haversine.py
Last active February 10, 2024 08:34
Haversine Distance Calculator
"""Calculate the difference between two GPS coordinates using the Haversine formula"""
from math import radians, cos, sin, sqrt, atan2
def haversine(coord1, coord2):
# Radius of the Earth in kilometers
R = 6378.137
# Converting coordinates from degrees to radians
lat1, lon1 = radians(coord1[0]), radians(coord1[1])
@kevinlinxc
kevinlinxc / arduino_port_detector.py
Created October 14, 2023 23:24
Arduino Port Detector Utility
"""
This simple helper script auto-detects the Arduino port.
Saves time because you don't need to check the Arduino IDE/device manager to get the port number each time.
"""
import serial.tools.list_ports
def get_port():
"""
Get the port name of a connected Arduino if one is connected.
@kevinlinxc
kevinlinxc / frame_video.py
Created October 14, 2023 23:20
Simple OpenCV video genereator where each frame is the frame number
import cv2
import numpy
# make a video where each frame is the frame number
writer = cv2.VideoWriter("frame_numbers.mp4", cv2.VideoWriter_fourcc(*"mp4v"), 30, (1920, 1080))
for i in range(6572):
frame = numpy.zeros((1080, 1920, 3), dtype=numpy.uint8)
cv2.putText(frame, str(i), (500, 500), cv2.FONT_HERSHEY_SIMPLEX, 10, (255, 255, 255), 2)
@kevinlinxc
kevinlinxc / triangulation.py
Created October 1, 2023 05:32
Pygeodesy Testing
# pip install pygeodesy
import pygeodesy.sphericalNvector
def intersection_point(lat_long_1, bearing_1, lat_long_2, bearing_2):
vector1 = pygeodesy.sphericalNvector.LatLon(*lat_long_1)
vector2 = pygeodesy.sphericalNvector.LatLon(*lat_long_2)
return vector1.intersection(bearing_1, vector2, bearing_2)
@kevinlinxc
kevinlinxc / pdfdiff.md
Last active September 10, 2023 04:37
How to diff 2 PDFs

Diffing PDFs

Recently, I wanted to find the textual differences between two PDFS, in the same way that you would compare plain text files with Git/GitHub. I wanted the nice side-by-side view too, not just the Git diff terminal output.

Edit: After writing this gist, I realized that https://www.diffchecker.com/pdf-compare/ pretty much does what I want. This was still a fun experiment though.

I tried a few different ways of extracting the text (Tesseract OCR, Copy and Pasting all the text), but eventually I found the best solution for me was a tool called textract, which uses pdftotext under the hood. This did the best job, as it didn't have weird misread symbols like OCR and it didn't have extra nonsense copied by C/P.