Skip to content

Instantly share code, notes, and snippets.

@TimSC
TimSC / StencilMesh.py
Created July 22, 2016 22:30
My solution to the non-rectangular StencilView
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#My solution to the non-rectangular StencilView
#Released by Tim Sheerman-Chase under CC0
from kivy.uix.widget import Widget
from kivy.graphics import Color, Rectangle, ClearColor
from kivy.graphics.vertex_instructions import Triangle, Mesh, Line
from kivy.core.image import Image
@TimSC
TimSC / edgecontour.py
Created August 17, 2016 13:20
Find edge contours around groups of squares in a grid
#Find edge contours around groups of squares in a grid
#By Tim Sheerman-Chase, 2016, released under CC0
def FindContourEdges(pts):
#Find edge fragments of area
unmergedEdges = {}
for pt in pts:
if (pt[0]-1, pt[1]) not in pts:
unmergedEdges[pt] = (pt[0], pt[1]+1)
@TimSC
TimSC / flag.py
Created August 18, 2016 12:20
Generated moving checkered flag
import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import PiecewiseAffineTransform, warp, resize
from skimage import data
from PIL import Image, ImageDraw
import numpy as np
import scipy.misc as misc
image = Image.new("RGBA", (512, 512))
draw = ImageDraw.Draw(image)
@TimSC
TimSC / weightedaverage.py
Last active August 24, 2016 21:37
Running weighted average
#By Tim Sheerman-Chase, 2016
#CC0 license
class RunningWeightedAverage(object):
def __init__(self):
self.val = 0.0
self.weightTotal = 0.0
def update(self, valIn, weight = 1.0):
scaling = weight / float(self.weightTotal + weight)
self.val = valIn * scaling + self.val * (1.0 - scaling)
@TimSC
TimSC / line-plane-collision.py
Last active April 7, 2023 11:00
Line-Plane collision in 3D, Python 2 or 3
#Line-plane intersection
#By Tim Sheerman-Chase
#This software may be reused under the CC0 license
#Based on http://geomalgorithms.com/a05-_intersect-1.html
from __future__ import print_function
import numpy as np
def LinePlaneCollision(planeNormal, planePoint, rayDirection, rayPoint, epsilon=1e-6):
@TimSC
TimSC / fix-relations.py
Created September 22, 2016 11:21
Python manipulation of csv
import gzip, csv, json, sys
csv.field_size_limit(sys.maxsize)
fi = csv.reader(gzip.open("/home/postgres/relations.csv.gz","rb"))
fiout2 = gzip.open("fixedrelations.csv.gz","wb")
fiout = csv.writer(fiout2)
for li in fi:
oldmems = json.loads(li[-1])
assert(len(li)==10)
mems=[]
@TimSC
TimSC / maketransparent.py
Created October 18, 2016 23:53
Change an image to have a transparency
from PIL import Image
import numpy as np
if __name__ == "__main__":
im = Image.open("in.png")
pix = im.load()
imgOut = Image.new("RGBA", im.size)
pixOut = imgOut.load()
@TimSC
TimSC / negbase.py
Last active December 15, 2016 23:49
Encode numbers with a negative base (python 2 or 3)
from __future__ import print_function
def EncodeNegBase(n, b): #Converts from decimal
if n == 0:
return "0"
out = []
while n != 0:
n, rem = divmod(n, b)
if rem < 0:
n += 1
@TimSC
TimSC / weighted_sampling.py
Created November 22, 2016 15:06
Weighted samping in python 2 and 3. Released under CC0.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import random
def get_weighted_sample(values, weights, return_index = False):
cum = []
tot = 0.0
for w in weights:
if w < 0.0:
@TimSC
TimSC / rotateicon.py
Created December 1, 2016 17:06
Add rotating triangles around an icon
from PIL import Image, ImageDraw
import math, os
def DrawTri(im, draw, ang):
vec = [math.cos(ang), -math.sin(ang)]
cross = [vec[1], -vec[0]]
cent = [im.size[0]/2.0, im.size[1]/2.0]
ln = min(cent)
pos = (cent[0]+vec[0]*ln, cent[1]+vec[1]*ln)