This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |