Qu

draw_scale_bar

Builtin index · Images

result = draw_scale_bar(img, length_physical, [pixel_size=], [unit=], color=, [position="bottom-right"], [margin=10], [thickness=4], [label=true], [font_scale=1])

Input: img, length_physical, pixel_size, unit, color, position, margin, thickness, label, font_scale · Output: result

What it is

img is an Image; length_physical is a positive number. Draws a horizontal bar whose PIXEL length is length_physical / pixel_size, rounded to the nearest pixel — reusing image.regions's own pixel_size=/unit= convention exactly: omitting pixel_size= means length_physical is already a pixel count (unit= then defaults to, and must be, "px"); giving unit= with no pixel_size=, or unit="px" alongside one, are refused for the same reason image.regions refuses them (a unit with no scale, or a scale with a contradictory unit). position= is one of "bottom-left"/"bottom-right" (default)/"top-left"/"top-right", placed margin= pixels (default 10) from that corner. thickness= (default 4) is the bar's pixel height. label= (default true) draws length_physical and the unit underneath (bottom positions: above, to stay on-canvas) the bar using a built-in bitmap font — digits, ., -, %, and the handful of unit letters (u, n, m, c, p, x, k, i) a physical unit actually needs; any other character renders as blank space rather than an error. font_scale= (default 1) is an integer pixel-doubling factor for the label.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = draw_scale_bar(img, length_physical, [pixel_size=], [unit=], color=, [position="bottom-right"], [margin=10], [thickness=4], [label=true], [font_scale=1])
img Imageimg is an Image.
length_physical numberlength_physical is a positive number.
pixel_size optionalgiving unit= with no pixel_size=, or unit="px" alongside one, are refused for the same reason image.regions refuses them (a unit with no scale, or a scale with a contradictory unit).
unit optionalgiving unit= with no pixel_size=, or unit="px" alongside one, are refused for the same reason image.regions refuses them (a unit with no scale, or a scale with a contradictory unit).
color —not documented yet
position optional (default "bottom-right")position= is one of "bottom-left"/"bottom-right" (default)/"top-left"/"top-right", placed margin= pixels (default 10) from that corner.
margin optional (default 10)position= is one of "bottom-left"/"bottom-right" (default)/"top-left"/"top-right", placed margin= pixels (default 10) from that corner.
thickness optional (default 4)thickness= (default 4) is the bar's pixel height.
label optional (default true)label= (default true) draws length_physical and the unit underneath (bottom positions: above, to stay on-canvas) the bar using a built-in bitmap font — digits, ., -, %, and the handful of unit letters (u, n, m, c, p, x, k, i) a physical unit actually needs.
font_scale optional (default 1)font_scale= (default 1) is an integer pixel-doubling factor for the label.
result Image
Returns a new Image, same dimensions as img.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

img = image_new(300, 200, r=20, g=20, b=20)
img = draw_line(img, 10, 10, 290, 10, color="yellow", thickness=2)
img = draw_rect(img, 20, 30, 60, 40, color="cyan", thickness=2)
img = draw_circle(img, 150, 100, 30, color="magenta", filled=true)
img = draw_arrow(img, 200, 150, 260, 180, color="white", head_size=12)

# A 100 px bar labeled "50 um" when pixel_size = 0.5 um/px.

img = draw_scale_bar(img, 50, pixel_size=0.5, unit="um", position="bottom-right", color="white")
save_image("/tmp/annotated.bmp", img)

from images

How it works

draw_scale_bar(img, length_physical, [pixel_size=], [unit=], [position="bottom-right"], color=, [margin=10], [thickness=4], [label=true], [font_scale=1]) -- draws a horizontal bar whose PIXEL length is length_physical / pixel_size, rounded to the nearest pixel. Reuses image::regions's own pixel_size=/ regions doc comment): omitting pixel_size= means length_physical IS already a pixel count (unit= then defaults to, and must be, "px"); giving both unit= with no pixel_size=, or unit="px" alongside one, are the same "unit with no scale" / "a unit that contradicts its own scale" errors regions reports, reworded for this builtin. label draws length_physical + the unit underneath the bar using image::Image::draw_text's built-in bitmap font (digits/./ -/%/the handful of unit letters it supports; anything else renders blank — see that method's own doc comment for the full scope of this minimal font).

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.