Skip to content

Instantly share code, notes, and snippets.

@jcupitt
jcupitt / srgb2xyz.py
Created June 1, 2016 16:56
convert srgb to xyz
#!/usr/bin/python
import sys
R = int(sys.argv[1])
G = int(sys.argv[2])
B = int(sys.argv[3])
# assume RGB are 0 - 255 ... convert to 0 - 1
R /= 255.0
@jcupitt
jcupitt / untile-google.py
Last active October 25, 2023 15:59
untile a layer in a google maps layout dzsave pyramid
#!/usr/bin/python
# untile a dzsave --layout google level
# tiles are expected to be named as ARG/y/x.jpg, so use as:
#
# untile-google.py ~/pics/somepyramid/4 out.jpg
#
# to make out.jpg, level 4 of somepyramid reassembled
@jcupitt
jcupitt / untile-dz.py
Last active October 25, 2023 15:59
untile a layer in a deepzoom pyramid
#!/usr/bin/python3
# untile a dzsave --layout dz level
# tiles are expected to be named as ARG/x_y.jpeg, so use as:
#
# untile-dz.py ~/pics/somepyramid_files/11 out.jpg
#
# to make out.jpg, level 4 of somepyramid reassembled
@jcupitt
jcupitt / colourdeconv.py
Created September 9, 2023 12:43
colour deconvolution with pyvips
#!/usr/bin/python3
import sys
import pyvips
from numpy import linalg
stain = [
[0.468, 0.023, 0.767],
[0.721, 0.141, 0.576],
[0.511, 0.990, 0.284]
@jcupitt
jcupitt / trim.rb
Last active April 30, 2023 02:32
auto-crop in ruby-vips
#!/usr/bin/env ruby
# "trim" is nnow built in, so this is easy
require 'vips'
im = Vips::Image.new_from_file(ARGV[0])
left, top, width, height = im.find_trim
im = im.crop(left, top, width, height)
@jcupitt
jcupitt / fetch-vs-crop.py
Created March 30, 2023 10:38
time fetch and crop for image read
#!/usr/bin/python3
import random
import time
import sys
import pyvips
if len(sys.argv) != 4:
print("usage: ./fetch-vs-crop.py IMAGE SIZE N-TILES")
@jcupitt
jcupitt / reflection-fill.py
Created November 19, 2017 19:25
rotate an image in pyvips, with reflection fill for any new pixels
import pyvips
# rotate an image with centre crop and reflection fill
im = pyvips.Image.new_from_file("/home/john/pics/k2.jpg")
# pyvips rotate always fills any new pixels which appear around the image edges
# with 0 (black). To have something else in there, we need to expand the image
# first, then crop it down to just the bit we need.
@jcupitt
jcupitt / introspect.c
Last active March 8, 2023 06:52
libvips introspection demo
/* vips8 introspection demo
*
* compile with:
*
* gcc -g -Wall introspect.c `pkg-config vips --cflags --libs`
*
* try:
*
* ./a.out embed
*
@jcupitt
jcupitt / dominant-lab.py
Last active February 15, 2023 16:14
find dominant colour in an 8-bit RGB Image with libvips python
#!/usr/bin/python
import sys
from gi.repository import Vips
N_BINS = 10
BIN_SIZE = 256 / N_BINS
im = Vips.Image.new_from_file(sys.argv[1], access = Vips.Access.SEQUENTIAL)
@jcupitt
jcupitt / createtiff.c
Created December 10, 2022 12:40
make a multipage tiff file
/* compile with
*
* gcc -Wall createtiff.c `pkg-config libtiff --cflags --libs`
*/
#include <tiffio.h>
int
main (int argc, const char **argv)
{