Skip to content

Instantly share code, notes, and snippets.

View CnrLwlss's full-sized avatar

Conor Lawless CnrLwlss

View GitHub Profile
@CnrLwlss
CnrLwlss / colour_samples.jl
Created January 3, 2025 18:10
Iterative construction of video from contiguous samples from static image
using VideoIO
using Images
download("https://live.staticflickr.com/4293/35896494470_6e106950d9_o_d.jpg","input.jpg")
img = load("input.jpg")
(h,w) = size(img)
function getframe(img, dx, dy, N, scaleup)
smimg = img[dy:(dy+(N-1)),dx:(dx+(N-1))]
bigpix = repeat(smimg, inner=(scaleup,scaleup))
@CnrLwlss
CnrLwlss / single_colour_frame.jl
Created January 3, 2025 17:58
Sample some pixels from a colour image and write to file
using VideoIO
using Images
download("https://live.staticflickr.com/4293/35896494470_6e106950d9_o_d.jpg","input.jpg")
img = load("input.jpg")
(h,w) = size(img)
function getframe(img, dx, dy, N, scaleup)
smimg = img[dy:(dy+(N-1)),dx:(dx+(N-1))]
bigpix = repeat(smimg, inner=(scaleup,scaleup))
@CnrLwlss
CnrLwlss / nn_enlarge.jl
Created January 3, 2025 12:18
Generate a random greyscale image and enlarge it using "nearest neighbours".
using Images
smallpix = rand(UInt8, 8, 8)
bigpix = repeat(smallpix, inner=(200,200))
save("greychess.png",bigpix)
@CnrLwlss
CnrLwlss / MusicStaff.py
Last active November 12, 2024 11:22
Python code to write a .pdf file of empty music staff for composing music. You can print it out and start writing.
import fpdf
def makePage(w=297,h=210):
'''Page width and height in mm. Default is A4 landscape'''
pdf = fpdf.FPDF(unit='mm', format=(w, h))
pdf.add_page()
return(pdf)
def drawLines(pdf, titlegap = 30, stafflinegap = 4, linegap = 10):
'''All dimensions in mm. 5 lines for music notation.
@CnrLwlss
CnrLwlss / flatim.py
Created March 12, 2024 10:15
Making single channel image from multichannel images (RGB in this example).
import numpy as np
from PIL import Image
# Open a multichannel image
# In this example, an RGB image
fname = r"C:\Users\Conor\Downloads\PXL_20240304_174814563.MP.jpg"
im = Image.open(fname)
arr = np.array(im,dtype=np.uint8)
@CnrLwlss
CnrLwlss / zero_correlation.R
Created January 12, 2024 12:39
Sometimes zero correlation does not mean "randomly distributed", rather just "no effect".
seed(99)
N = 100
x = runif(N)
noise = rnorm(N,mean=0,sd=0.1)
y_corr = 1*x + noise
y_nocorr = noise
rsq_corr = cor(x,y_corr)
rsq_nocorr = cor(x,y_nocorr)
plot(x,y_corr,xlim=c(0,1),ylim=c(-0.3,1.0),pch=16,xlab="x",ylab="y",cex.lab=1.55)
@CnrLwlss
CnrLwlss / pdfGrid.py
Created January 4, 2024 09:51
Writes a single page pdf of width w mm and height h mm (defaults are A4) with a nrow x ncol grid traced out on it, to file outf. Grid is a thin line (thickness is lwd) coloured grey (default is very light grey) with dashing specified by dash length dl and space length sl.
from fpdf import FPDF
def makeGrid(w=210.0, h=297.0, nrow=5, ncol=4, outf="grid.pdf", grey = 220, dl=3, sl=2, lwd=0.2):
'''Writes a single page pdf of width w mm and height h mm (defaults are A4) with a nrow x ncol grid
traced out on it, to file outf. Grid is a thin line (thickness is lwd) coloured grey (default is very
light grey) with dashing specified by dash length dl and space length sl.'''
pdf = FPDF(orientation="P", unit="mm", format=(w,h))
deltax = float(w)/ncol
deltay = float(h)/nrow
@CnrLwlss
CnrLwlss / Long and wide stripcharts.R
Created November 28, 2023 12:52
Plotting long and wide stripcharts
# Plot wide data
test = data.frame(id=1:4,"A"=1:4,"B"=5:8,"C"=11:14)
# Plot original order
stripchart(test[,2:4])
# Plot updated order
stripchart(test[,c("C","A","B")])
# Plot long data
test2 = reshape(test,direction="long",varying=c("A","B","C"),v.names="Value",idvar=c("id"),timevar=c("Label"),times=c("A","B","C"))
# Plot original order
@CnrLwlss
CnrLwlss / freeze.R
Last active May 31, 2023 11:47
Simulating protein pixel intensities in fibre sections with freezing artefacts
# A possible problem with freezing artefacts and correlations
# Need MASS library to sample from multivariate normal distribution (correlated datasets)
library(MASS)
R_good = matrix(c(0.55, 0.5,
0.5, 0.55),
nrow = 2, ncol = 2, byrow = TRUE)
R_bad = matrix(c(1, 0.5,
0.5, 1),
@CnrLwlss
CnrLwlss / captureVid.py
Created March 3, 2023 15:07
Previewing and then capturing HD video frames from webcam with Python
import cv2
import time
from pathlib import Path
import os
cam = cv2.VideoCapture(1)
cam.set(3, 1920)
cam.set(4, 1080)
cv2.namedWindow("test")