Using the data storage type defined on this page for raster images, delegate writing a JPEG file through a pipe using the output_ppm function defined on this other page.

There are various utilities that can be used for this task, for example: cjpeg (package "jpeg-progs" on Linux), ppmtojpeg (package "netpbm" on Linux), convert (from ImageMagick, multi-platform).

import bitmap
import ppm_write
import osproc
 
# Build an image.
var image = newImage(100, 50)
image.fill(color(255, 0, 0))
for row in 10..20:
  for col in 0..<image.w:
    image[col, row] = color(0, 255, 0)
for row in 30..40:
  for col in 0..<image.w:
    image[col, row] = color(0, 0, 255)
 
# Launch ImageMagick "convert".
# Input is taken from stdin and result written in "output1.jpeg".
var p = startProcess("convert", args = ["ppm:-", "output1.jpeg"], options = {poUsePath})
var stream = p.inputStream()
image.writePPM(stream)
p.close()
 
# Launch Netpbm "pnmtojpeg".
# Input is taken from stdin and output sent to "output2.jpeg".
p = startProcess("pnmtojpeg >output2.jpeg", options = {poUsePath, poEvalCommand})
stream = p.inputStream()
image.writePPM(stream)
p.close()