Skip to content

Instantly share code, notes, and snippets.

View KrisYu's full-sized avatar

Xue Yu KrisYu

View GitHub Profile
@KrisYu
KrisYu / NSImage_to_pdf.swift
Last active February 23, 2024 10:21
convert NSImage to pdf, basically the same as UIImage
import Cocoa
let a = #imageLiteral(resourceName: "hot.png")
extension NSImage {
var toCGImage: CGImage {
var imageRect = NSRect(x: 0, y: 0, width: size.width, height: size.height)
guard let image = cgImage(forProposedRect: &imageRect, context: nil, hints: nil) else {
abort()
}
return image
@KrisYu
KrisYu / oobb.py
Last active February 23, 2024 01:01
oriented bounding box for points
#!/usr/bin/env python3
# https://stackoverflow.com/questions/32892932/create-the-oriented-bounding-box-obb-with-python-and-numpy
def oobb(pts):
'''
given:
pts, 2d or 3d
return:
corners, center of the oobb
'''
@KrisYu
KrisYu / saveImage.swift
Last active July 3, 2022 11:55
Save image to file on macOS using swift, answers found from SO
// plain write to image
@discardableResult func writeCGImage(_ image: CGImage, to destinationURL: URL) -> Bool {
guard let destination = CGImageDestinationCreateWithURL(destinationURL as CFURL, kUTTypePNG, 1, nil) else { return false }
CGImageDestinationAddImage(destination, image, nil)
return CGImageDestinationFinalize(destination)
}
// There's a panel, save image png
@KrisYu
KrisYu / DragContainer.swift
Created February 25, 2018 03:12
Drag and Drop for macos
import Cocoa
protocol DragContainerDelegate {
func draggingEntered()
func draggingExit()
func draggingFileAccept(_ files: [URL])
}
// https://stackoverflow.com/questions/29233247/implementing-a-drag-and-drop-zone-in-swift
class DragContainer: NSView {
@KrisYu
KrisYu / pca.py
Created December 8, 2021 14:27
pca to fit plane
# https://stackoverflow.com/questions/38754668/plane-fitting-in-a-3d-point-cloud
def PCA(data, correlation = False, sort = True):
""" Applies Principal Component Analysis to the data
Parameters
----------
data: array
The array containing the data. The array must have NxM dimensions, where each
of the N rows represents a different individual record and each of the M columns
@KrisYu
KrisYu / cmd_parse.py
Created November 17, 2021 20:19
command line parse
# https://stackoverflow.com/questions/15753701/how-can-i-pass-a-list-as-a-command-line-argument-with-argparse
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input',type = str, default = "sample.json")
parser.add_argument( "-l","--list", nargs="*", type=int, default=[0,1,2])
args = parser.parse_args()
@KrisYu
KrisYu / read_json.py
Created November 17, 2021 15:38
read json file
def read_data_from_file( filename ):
with open(filename, 'r') as json_file:
data = json.load(json_file)
return data
@KrisYu
KrisYu / distance_from_array_to_point.py
Created November 2, 2021 00:31
distance from one point to array of points
# https://stackoverflow.com/questions/48609781/distance-between-one-point-and-rest-of-the-points-in-an-array/48610147
from scipy.spatial import distance
X = [(35.0456, -85.2672)]
coords = [(35.1174, -89.9711),
(35.9728, -83.9422),
(36.1667, -86.7833)]
distance.cdist(X, coords, 'euclidean')
array([[ 4.70444794, 1.6171966 , 1.88558331]])
@KrisYu
KrisYu / delete_file.py
Last active November 1, 2021 20:00
delete some files in a path
import os
files = os.listdir()
# files is a string list
for filename in files:
print(filename)
# https://askubuntu.com/questions/443830/delete-all-files-whose-filenames-contain-a-particular-string
@KrisYu
KrisYu / read.py
Last active November 1, 2021 17:07
read and write file
with open('filename') as f:
lines = f.readlines()
with open('write','w') as f:
f.write('Hello\n')