Saving

saveImage(paths, **options)

Save or export the canvas to a specified format. The path argument is a single destination path to save the current drawing actions.

The file extension is important because it will determine the format in which the image will be exported.

All supported file extensions: pdf, png, jpg, jpeg, tif, tiff, svg, gif, bmp, mp4, icns, *, PIL, NSImage. (* will print out all actions.)

When exporting an animation or movie, each page represents a frame and the framerate is set by calling frameDuration() after each newPage().

# set the canvas size
size(150, 100)

# draw a background
rect(10, 10, width()-20, height()-20)

# set a fill
fill(1)
# draw some text
text("Hello World!", (20, 40))
# save it as a png and pdf on the current users desktop
saveImage("~/Desktop/firstImage.png")
saveImage("~/Desktop/firstImage.pdf")

saveImage() options can be set by adding keyword arguments. Which options are recognized depends on the output format.

pdf options:

  • multipage: If False, only the last page in the document will be saved into the output PDF. This value is ignored if it is None (default).

png options:

  • imageResolution: The resolution of the output image in PPI. Default is 72.
  • antiAliasing: Indicate if a the image should be rendedered with anti-aliasing. Default is True.
  • multipage: Output a numbered image for each page or frame in the document.
  • imagePNGGamma: The gamma value for the image. It is a floating-point number between 0.0 and 1.0, with 0.0 being black and 1.0 being the maximum color.
  • imagePNGInterlaced: Boolean value that indicates whether the image should be interlaced.
  • imageColorSyncProfileData: A bytes or NSData object containing the ColorSync profile data.

jpg, jpeg options:

  • imageResolution: The resolution of the output image in PPI. Default is 72.
  • antiAliasing: Indicate if a the image should be rendedered with anti-aliasing. Default is True.
  • multipage: Output a numbered image for each page or frame in the document.
  • imageJPEGCompressionFactor: A float between 0.0 and 1.0, with 1.0 resulting in no compression and 0.0 resulting in the maximum compression possible
  • imageJPEGProgressive: Boolean that indicates whether the image should use progressive encoding.
  • imageFallbackBackgroundColor: The background color to use when writing to an image format (such as JPEG) that doesn’t support alpha. The color’s alpha value is ignored. The default background color, when this property is not specified, is white. The value of the property should be an NSColor object or a DrawBot RGB color tuple.
  • imageColorSyncProfileData: A bytes or NSData object containing the ColorSync profile data.

tif, tiff options:

  • imageResolution: The resolution of the output image in PPI. Default is 72.
  • antiAliasing: Indicate if a the image should be rendedered with anti-aliasing. Default is True.
  • multipage: Output a numbered image for each page or frame in the document.
  • imageTIFFCompressionMethod: None, or ‘lzw’ or ‘packbits’, or an NSTIFFCompression constant
  • imageColorSyncProfileData: A bytes or NSData object containing the ColorSync profile data.

svg options:

  • multipage: Output a numbered svg file for each page or frame in the document.

gif options:

  • imageResolution: The resolution of the output image in PPI. Default is 72.
  • antiAliasing: Indicate if a the image should be rendedered with anti-aliasing. Default is True.
  • multipage: Output a numbered image for each page or frame in the document.
  • imageGIFDitherTransparency: Boolean that indicates whether the image is dithered
  • imageGIFRGBColorTable: A bytes or NSData object containing the RGB color table.
  • imageColorSyncProfileData: A bytes or NSData object containing the ColorSync profile data.
  • imageGIFLoop: Boolean that indicates whether the animated gif should loop

bmp options:

  • imageResolution: The resolution of the output image in PPI. Default is 72.
  • antiAliasing: Indicate if a the image should be rendedered with anti-aliasing. Default is True.
  • multipage: Output a numbered image for each page or frame in the document.

mp4 options:

  • ffmpegCodec: The codec to be used by ffmpeg. By default it is ‘libx264’ (for H.264). The ‘mpeg4’ codec gives better results when importing the movie into After Effects, at the expense of a larger file size.
  • imageResolution: The resolution of the output image in PPI. Default is 72.
  • antiAliasing: Indicate if a the image should be rendedered with anti-aliasing. Default is True.
  • imagePNGGamma: The gamma value for the image. It is a floating-point number between 0.0 and 1.0, with 0.0 being black and 1.0 being the maximum color.
  • imagePNGInterlaced: Boolean value that indicates whether the image should be interlaced.
  • imageColorSyncProfileData: A bytes or NSData object containing the ColorSync profile data.

icns options:

  • imageResolution: The resolution of the output image in PPI. Default is 72.
  • antiAliasing: Indicate if a the image should be rendedered with anti-aliasing. Default is True.
  • multipage: Output a numbered image for each page or frame in the document.

PIL options:

  • imageResolution: The resolution of the output image in PPI. Default is 72.
  • antiAliasing: Indicate if a the image should be rendedered with anti-aliasing. Default is True.
  • multipage: Output a numbered image for each page or frame in the document.

NSImage options:

  • imageResolution: The resolution of the output image in PPI. Default is 72.
  • antiAliasing: Indicate if a the image should be rendedered with anti-aliasing. Default is True.
  • multipage: Output a numbered image for each page or frame in the document.
# same example but we just change the image resolution
size(150, 100)
rect(10, 10, width()-20, height()-20)
fill(1)
text("Hello World!", (20, 40))
# save it with an option that controls the resolution (300 PPI)
saveImage("~/Desktop/firstImage300.png", imageResolution=300)
printImage(pdf=None)

Export the canvas to a printing dialog, ready to print.

Optionally a pdf object can be provided.

# set A4 page size
size(595, 842)
# draw something
oval(0, 0, width(), height())
# send it to the printer
printImage()
pdfImage()

Return the image as a pdf document object.