A Julia package for adding scale bars to images, particularly useful for scientific and microscopy applications.
- Physical Scale Bars: Add scale bars with proper physical dimensions (μm, nm, etc.)
- Pixel-based Scale Bars: Add scale bars with exact pixel dimensions
- Flexible Positioning: Place scale bars at any corner using CairoMakie-style position symbols
- Smart Defaults: Automatically calculates sensible scale bar dimensions based on image size
- In-place and Non-destructive: Choose between modifying images or creating new copies
using Pkg
Pkg.add("ScaleBar")using Images, ScaleBar
# Create a test image with gray background for better visibility
img = RGB.(fill(0.5, 512, 512))
# In-place with required length
scalebar!(img, 0.1, 10; units="μm")
# Non-destructive with auto-calculated length
result = scalebar(img, 0.1; units="μm")
img_with_bar1 = result.image
physical_length = result.physical_length # Get the physical length used
pixel_length = result.pixel_length # Get the length in pixels
# Non-destructive with explicit length
result = scalebar(img, 0.1; physical_length=10, units="μm")
img_with_bar2 = result.image
# Or use destructuring
(; image, physical_length, pixel_length, units) = resultusing Images, ScaleBar
# Create a test image with gray background for better visibility
img = RGB.(fill(0.5, 512, 512))
# In-place with required length
scalebar!(img, 50)
# Non-destructive with auto-calculated length
result = scalebar(img)
img_with_bar1 = result.image
pixel_length = result.pixel_length # Get the length in pixels that was used
# Non-destructive with explicit length
result = scalebar(img; length=50)
img_with_bar2 = result.image
# Or use destructuring
(; image, pixel_length) = resultScaleBar.jl provides a clean, unified API with two main functions:
scalebar!: Modifies the image in-place (length required)scalebar: Creates a new image with the scale bar and returns a named tuple containing the image and scale bar information (length optional)
Each function has two methods:
# In-place version (length is required)
scalebar!(img, pixel_size, physical_length;
position=:br,
width=nothing,
padding=10,
color=:white,
units="")
# Non-mutating version (length is optional)
# Auto-calculated length - returns a named tuple with image and scale bar info
scalebar(img, pixel_size;
position=:br,
width=nothing,
padding=10,
color=:white,
units="")
# Returns (image, physical_length, pixel_length, units)
# Explicit length - returns a named tuple with image and scale bar info
scalebar(img, pixel_size;
physical_length=10,
position=:br,
width=nothing,
padding=10,
color=:white,
units="")
# Returns (image, physical_length, pixel_length, units)Parameters:
img: Input image (AbstractArray)pixel_size: Size of each pixel in physical units (e.g., 0.1 for 0.1μm per pixel)physical_length: Length of the scale bar in physical units (required for in-place version)
Keyword Arguments:
position: Position of the scale bar (:tl,:tr,:bl,:br), default::brphysical_length: Length of the scale bar in physical units (optional for non-destructive version)width: Width of the scale bar in pixels, default: auto-calculatedpadding: Padding from the edge of the image in pixels, default: 10color: Color of the scale bar (:whiteor:black), default::whiteunits: Units for the physical length (e.g., "nm", "μm"), default: ""
# In-place version (length is required)
scalebar!(img, length;
position=:br,
width=nothing,
padding=10,
color=:white)
# Non-mutating version (length is optional)
# Auto-calculated length - returns a named tuple with image and scale bar info
scalebar(img;
position=:br,
width=nothing,
padding=10,
color=:white)
# Returns (image, pixel_length)
# Explicit length - returns a named tuple with image and scale bar info
scalebar(img;
length=50,
position=:br,
width=nothing,
padding=10,
color=:white)
# Returns (image, pixel_length)Parameters:
img: Input image (AbstractArray)length: Length of the scale bar in pixels (required for in-place version)
Keyword Arguments:
position: Position of the scale bar (:tl,:tr,:bl,:br), default::brlength: Length of the scale bar in pixels (optional for non-destructive version)width: Width of the scale bar in pixels, default: auto-calculatedpadding: Padding from the edge of the image in pixels, default: 10color: Color of the scale bar (:whiteor:black), default::white
- For
scalebar!functions, you must always specify the scale bar length explicitly - For
scalebarfunctions, you can either let the length be auto-calculated or specify it explicitly - The non-mutating
scalebarfunction returns a named tuple with:image: The new image with the scale bar addedphysical_length: The physical length of the scale bar (for physical units version)pixel_length: The length of the scale bar in pixelsunits: The units of the physical length (for physical units version)
- You can use destructuring to extract the fields you need:
(; image, physical_length) = scalebar(...) - Auto-calculated lengths are designed to be ~10% of the image width and rounded to a "nice" value
- Choose contrasting colors for better visibility (white on dark backgrounds, black on light)