Output of drawings to files

Is it possible to output a drawing made with the draw module or Metapict or the turtle graphics library to a file such as png or jpeg?

The save-pict function from metapict can be used to save picts as png files (or jpeg for that matter).

Do you mean something like that:

#lang racket

(require pict)
(require racket/draw)
(require pict-abbrevs)

(define points '((100 . 100) (0 . 0)))

(define (connect-points dc dx dy join)
  (define a-pen
    (send the-pen-list find-or-create-pen "black" 10 'solid 'round join))
  (send dc set-pen a-pen)
  (send dc draw-lines points dx dy))

(define a-dc
  (dc
   (λ (dc dx dy)
     (define old-pen (send dc get-pen))
     (connect-points dc 20 50 'round)
     (send dc set-pen old-pen))
   460 170))

(save-pict "foo.png" a-dc 'png)

No. If I learn how to use turtle graphics in Racket, will I find that I can save the output from it to a file, either as a raster image or vector graphics?

There are several turtle libraries.

This example implements the turtle commands, then draws the Peano curve.

If you use

then use save-turtle-bitmap .

If you use

then you get a pict, which you can then save in different formats.

Essentially all drawing libraries in Racket are higher-level layers over racket/draw, which can save a drawing context as SVG, PDF, PostScript, PNG, JPG, and a few miscellaneous raster formats.

Some higher-level libraries for things like turtle graphics may make these things more convenient than others, but the underlying functionality is definitely always there.

The turtle example they showed is more about implementing the turtle interface itself — you'd still need to hook it into something that can actually write to a file. Looking at that peano.rkt example, the turtle state is building up a list of line segments, but there's no saving step. You'd want to either render those lines to a pict (which you can then save with save-pict) or draw them to a dc like in the second reply. The dc approach is probably more direct if you're going the turtle route.

The last expression turns the curves into a pict.
If run in DrRacket the image will be displayed in the repl.

;;; Produce image
(set-curve-pict-size 400 400)
(with-window (window -1 81 -1 82)
  (peano 6 90 3)
  (draw* c))