Workshop Python Image Analysis

Martijn Wehrens, September 2025

# imports 
import numpy as np
import matplotlib.pyplot as plt
import tifffile as tiff
import skimage as sk
from scipy import ndimage

Exercise 1

  • Detecting other bright cars:
# The following parameters will detect more white cars:
MIN_SIZE1 = 400; DISK_SIZE=4; MIN_SIZE2=300 
  • To detect dark cars, in the provided code, change the threshold value and apply it using < (instead of >):
# (..)

# Determine a dark threshold using the 20th percentile
thresh = np.percentile(img_carpark,20)

# Apply treshold
img_carpark_mask = img_carpark < thresh

# (..)

A more modular version of the DNA script

def gc_content(seq: str) -> float:
    """Return GC content as a fraction of sequence length."""
    gc_count = sum(1 for base in seq if base in ["G", "C"])
    return gc_count / len(seq)


def reverse_complement(seq: str) -> str:
    """Return the reverse complement of a DNA sequence."""
    complement = {"A": "T", "T": "A", "G": "C", "C": "G"}
    return "".join(complement[base] for base in reversed(seq))


# Now we can reuse the functions on multiple sequences
sequences = [
    "ATGCGTATAGCTAGCTAGGCTA",
    "GGGGCCCCAAAA",
    "ATATATATAT",
]

for s in sequences:
    print(f"Sequence: {s}")
    print(f"  GC content: {gc_content(s):.2f}")
    print(f"  Reverse complement: {reverse_complement(s)}")
    print()
Sequence: ATGCGTATAGCTAGCTAGGCTA
  GC content: 0.45
  Reverse complement: TAGCCTAGCTAGCTATACGCAT

Sequence: GGGGCCCCAAAA
  GC content: 0.67
  Reverse complement: TTTTGGGGCCCC

Sequence: ATATATATAT
  GC content: 0.00
  Reverse complement: ATATATATAT